Skip to content

Commit

Permalink
Adding new selector syntax elements, closes github issue rafaelw#4
Browse files Browse the repository at this point in the history
  • Loading branch information
brucek committed Apr 13, 2015
1 parent 4d5faab commit 19b383d
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 83 deletions.
107 changes: 95 additions & 12 deletions src/mutation-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var NodeMap = (function () {
this.values = [];
}
NodeMap.prototype.isIndex = function (s) {
return +s === s >>> 0;
return +s === +s >>> 0;
};

NodeMap.prototype.nodeId = function (node) {
Expand Down Expand Up @@ -301,7 +301,7 @@ var TreeChanges = (function (_super) {
})(NodeMap);

var MutationProjection = (function () {
// TOOD(any)
// TODO(any)
function MutationProjection(rootNode, mutations, selectors, calcReordered, calcOldPreviousSibling) {
this.rootNode = rootNode;
this.mutations = mutations;
Expand Down Expand Up @@ -847,8 +847,19 @@ function escapeQuotes(value) {
return '"' + value.replace(/"/, '\\\"') + '"';
}

var Modifier;
(function (Modifier) {
Modifier[Modifier["none"] = 0] = "none";
Modifier[Modifier["containsToken"] = 1] = "containsToken";
Modifier[Modifier["startsWith"] = 2] = "startsWith";
Modifier[Modifier["endsWith"] = 3] = "endsWith";
Modifier[Modifier["containsAny"] = 4] = "containsAny";
Modifier[Modifier["startsHyphen"] = 5] = "startsHyphen";
})(Modifier || (Modifier = {}));

var Qualifier = (function () {
function Qualifier() {
this.modifier = 0 /* none */;
}
Qualifier.prototype.matches = function (oldValue) {
if (oldValue === null)
Expand All @@ -857,9 +868,21 @@ var Qualifier = (function () {
if (this.attrValue === undefined)
return true;

if (!this.contains)
if (this.modifier == 0 /* none */)
return this.attrValue == oldValue;

if (this.modifier == 2 /* startsWith */)
return this.attrValue == oldValue.slice(0, this.attrValue.length);

if (this.modifier == 3 /* endsWith */)
return this.attrValue == oldValue.slice(-this.attrValue.length);

if (this.modifier == 4 /* containsAny */)
return oldValue.indexOf(this.attrValue) != -1;

if (this.modifier == 5 /* startsHyphen */)
return this.attrValue == oldValue || (this.attrValue + '-') == oldValue.slice(0, this.attrValue.length + 1);

var tokens = oldValue.split(' ');
for (var i = 0; i < tokens.length; i++) {
if (this.attrValue === tokens[i])
Expand All @@ -870,15 +893,27 @@ var Qualifier = (function () {
};

Qualifier.prototype.toString = function () {
if (this.attrName === 'class' && this.contains)
if (this.attrName === 'class' && this.modifier == 1 /* containsToken */)
return '.' + this.attrValue;

if (this.attrName === 'id' && !this.contains)
if (this.attrName === 'id' && this.modifier == 0 /* none */)
return '#' + this.attrValue;

if (this.contains)
if (this.modifier == 1 /* containsToken */)
return '[' + this.attrName + '~=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == 2 /* startsWith */)
return '[' + this.attrName + '^=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == 3 /* endsWith */)
return '[' + this.attrName + '$=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == 4 /* containsAny */)
return '[' + this.attrName + '*=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == 5 /* startsHyphen */)
return '[' + this.attrName + '|=' + escapeQuotes(this.attrValue) + ']';

if ('attrValue' in this)
return '[' + this.attrName + '=' + escapeQuotes(this.attrValue) + ']';

Expand Down Expand Up @@ -1021,7 +1056,7 @@ var Selector = (function () {
newQualifier();
currentSelector.tagName = '*';
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = 1 /* containsToken */;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1056,7 +1091,7 @@ var Selector = (function () {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = 1 /* containsToken */;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1089,7 +1124,7 @@ var Selector = (function () {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = 1 /* containsToken */;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1136,7 +1171,7 @@ var Selector = (function () {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = 1 /* containsToken */;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1187,7 +1222,31 @@ var Selector = (function () {
}

if (c == '~') {
currentQualifier.contains = true;
currentQualifier.modifier = 1 /* containsToken */;
state = EQUAL;
break;
}

if (c == '^') {
currentQualifier.modifier = 2 /* startsWith */;
state = EQUAL;
break;
}

if (c == '$') {
currentQualifier.modifier = 3 /* endsWith */;
state = EQUAL;
break;
}

if (c == '*') {
currentQualifier.modifier = 4 /* containsAny */;
state = EQUAL;
break;
}

if (c == '|') {
currentQualifier.modifier = 5 /* startsHyphen */;
state = EQUAL;
break;
}
Expand All @@ -1207,7 +1266,31 @@ var Selector = (function () {

case EQUIV_OR_ATTR_QUAL_END:
if (c == '~') {
currentQualifier.contains = true;
currentQualifier.modifier = 1 /* containsToken */;
state = EQUAL;
break;
}

if (c == '^') {
currentQualifier.modifier = 2 /* startsWith */;
state = EQUAL;
break;
}

if (c == '$') {
currentQualifier.modifier = 3 /* endsWith */;
state = EQUAL;
break;
}

if (c == '*') {
currentQualifier.modifier = 4 /* containsAny */;
state = EQUAL;
break;
}

if (c == '|') {
currentQualifier.modifier = 5 /* startsHyphen */;
state = EQUAL;
break;
}
Expand Down
100 changes: 87 additions & 13 deletions src/mutation-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class NodeMap<T> {
}

private isIndex(s:string):boolean {
return +s === s >>> 0;
return +s === +s >>> 0;
}

private nodeId(node:Node) {
Expand Down Expand Up @@ -335,7 +335,7 @@ class MutationProjection {
private characterDataOnly:boolean;
private matchCache:NumberMap<NodeMap<Movement>>;

// TOOD(any)
// TODO(any)
constructor(public rootNode:Node,
public mutations:MutationRecord[],
public selectors:Selector[],
Expand Down Expand Up @@ -901,10 +901,12 @@ function escapeQuotes(value:string):string {
return '"' + value.replace(/"/, '\\\"') + '"';
}

enum Modifier { none, containsToken, startsWith, endsWith, containsAny, startsHyphen }

class Qualifier {
public attrName:string;
public attrValue:string;
public contains:boolean;
public modifier:Modifier = Modifier.none;

constructor() {}

Expand All @@ -915,9 +917,21 @@ class Qualifier {
if (this.attrValue === undefined)
return true;

if (!this.contains)
if (this.modifier == Modifier.none)
return this.attrValue == oldValue;

if (this.modifier == Modifier.startsWith)
return this.attrValue == oldValue.slice(0, this.attrValue.length);

if (this.modifier == Modifier.endsWith)
return this.attrValue == oldValue.slice(-this.attrValue.length);

if (this.modifier == Modifier.containsAny)
return oldValue.indexOf(this.attrValue) != -1;

if (this.modifier == Modifier.startsHyphen)
return this.attrValue == oldValue || (this.attrValue + '-') == oldValue.slice(0, this.attrValue.length + 1);

var tokens = oldValue.split(' ');
for (var i = 0; i < tokens.length; i++) {
if (this.attrValue === tokens[i])
Expand All @@ -928,15 +942,27 @@ class Qualifier {
}

public toString():string {
if (this.attrName === 'class' && this.contains)
if (this.attrName === 'class' && this.modifier == Modifier.containsToken)
return '.' + this.attrValue;

if (this.attrName === 'id' && !this.contains)
if (this.attrName === 'id' && this.modifier == Modifier.none)
return '#' + this.attrValue;

if (this.contains)
if (this.modifier == Modifier.containsToken)
return '[' + this.attrName + '~=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == Modifier.startsWith)
return '[' + this.attrName + '^=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == Modifier.endsWith)
return '[' + this.attrName + '$=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == Modifier.containsAny)
return '[' + this.attrName + '*=' + escapeQuotes(this.attrValue) + ']';

if (this.modifier == Modifier.startsHyphen)
return '[' + this.attrName + '|=' + escapeQuotes(this.attrValue) + ']';

if ('attrValue' in this)
return '[' + this.attrName + '=' + escapeQuotes(this.attrValue) + ']';

Expand Down Expand Up @@ -1088,7 +1114,7 @@ class Selector {
newQualifier();
currentSelector.tagName = '*';
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1123,7 +1149,7 @@ class Selector {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1156,7 +1182,7 @@ class Selector {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1203,7 +1229,7 @@ class Selector {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1254,7 +1280,31 @@ class Selector {
}

if (c == '~') {
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = EQUAL;
break;
}

if (c == '^') {
currentQualifier.modifier = Modifier.startsWith;
state = EQUAL;
break;
}

if (c == '$') {
currentQualifier.modifier = Modifier.endsWith;
state = EQUAL;
break;
}

if (c == '*') {
currentQualifier.modifier = Modifier.containsAny;
state = EQUAL;
break;
}

if (c == '|') {
currentQualifier.modifier = Modifier.startsHyphen;
state = EQUAL;
break;
}
Expand All @@ -1274,7 +1324,31 @@ class Selector {

case EQUIV_OR_ATTR_QUAL_END:
if (c == '~') {
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = EQUAL;
break;
}

if (c == '^') {
currentQualifier.modifier = Modifier.startsWith;
state = EQUAL;
break;
}

if (c == '$') {
currentQualifier.modifier = Modifier.endsWith;
state = EQUAL;
break;
}

if (c == '*') {
currentQualifier.modifier = Modifier.containsAny;
state = EQUAL;
break;
}

if (c == '|') {
currentQualifier.modifier = Modifier.startsHyphen;
state = EQUAL;
break;
}
Expand Down
Loading

0 comments on commit 19b383d

Please sign in to comment.