Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selector matching syntax update, closes issue #4 #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# How to run tests #

In the source directory install the npm deps by typing `npm install`. Then load `tests/index.html` in a browser.

# How to compile #

See [http://www.typescriptlang.org/](http://www.typescriptlang.org/)
31 changes: 31 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "brucek-mutation-summary",
"description": "Makes observing the DOM fast and easy",
"main": [
"src/mutation-summary.js"
],
"moduleType": [
"globals"
],
"keywords": [
"dom",
"mutation",
"observer"
],
"authors": [
""
],
"license": "Apache 2.0",
"ignore": [
"docs/",
"examples/",
"node_modules/",
"tests/",
"util/"
],
"repository": {
"type": "git",
"url": "git://github.com/brucek/mutation-summary.git"
},
"private": true
}
88 changes: 77 additions & 11 deletions src/mutation-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var TreeChanges = (function (_super) {
return TreeChanges;
})(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 @@ -730,16 +730,34 @@ var validNameNonInitialChar = /[a-zA-Z0-9_\-]+/;
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 = Modifier.none;
}
Qualifier.prototype.matches = function (oldValue) {
if (oldValue === null)
return false;
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 @@ -748,12 +766,20 @@ var Qualifier = (function () {
return false;
};
Qualifier.prototype.toString = function () {
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) + ']';
return '[' + this.attrName + ']';
Expand Down Expand Up @@ -874,7 +900,7 @@ var Selector = (function () {
newQualifier();
currentSelector.tagName = '*';
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -905,7 +931,7 @@ var Selector = (function () {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -934,7 +960,7 @@ var Selector = (function () {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -974,7 +1000,7 @@ var Selector = (function () {
if (c == '.') {
newQualifier();
currentQualifier.attrName = 'class';
currentQualifier.contains = true;
currentQualifier.modifier = Modifier.containsToken;
state = QUALIFIER_NAME_FIRST_CHAR;
break;
}
Expand Down Expand Up @@ -1017,7 +1043,27 @@ var Selector = (function () {
break;
}
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 @@ -1033,7 +1079,27 @@ var Selector = (function () {
throw Error(SYNTAX_ERROR);
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