Skip to content

Commit

Permalink
added popular pseudo-element selectors '::after', '::before', '::firs…
Browse files Browse the repository at this point in the history
…t-letter', '::first-line', '::selection', '::backdrop' and '::placeholder' to avoid runtime errors while parsing valid CSS syntax. These elements generate dynamic content without inserting new nodes and will not modify the DOM tree, thus for these selectors the 'select()' method will always return an empty array while the 'match' method will always return false.
  • Loading branch information
dperini committed May 18, 2017
1 parent c9b3563 commit d02f630
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/modules/nwmatcher-pseudos.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,38 @@ NW.Dom.registerSelector(
};

})(this));

NW.Dom.registerSelector(
'nwmatcher:epseudos',
/^((?:[:]{1,2}(?:after|before|first-letter|first-line))|(?:[:]{2,2}(?:selection|backdrop|placeholder)))?(.*)/i,
(function(global) {

return function(match, source) {

var status = true;

switch (match[1].match(/(\w+)$/)[0]) {

case 'after':
case 'before':
case 'first-letter':
case 'first-line':
case 'selection':
case 'backdrop':
case 'placeholder':
source = 'if(!(/1|11/).test(e.nodeType)){' + source + '}';
break;

default:
status = false;
break;
}

return {
'source': source,
'status': status
};

};

})(this));
2 changes: 1 addition & 1 deletion src/nwmatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
lastPartsMatch,
lastPartsSelect,

prefixes = '[#.:]?',
prefixes = '(?:[#.:]|::)?',
operators = '([~*^$|!]?={1})',
whitespace = '[\\x20\\t\\n\\r\\f]',
combinators = '\\x20|[>+~](?=[^>+~])',
Expand Down
7 changes: 6 additions & 1 deletion src/nwmatcher-noqsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
lastPartsMatch,
lastPartsSelect,

prefixes = '[#.:]?',
prefixes = '(?:[#.:]|::)?',
operators = '([~*^$|!]?={1})',
whitespace = '[\\x20\\t\\n\\r\\f]',
combinators = '\\x20|[>+~](?=[^>+~])',
Expand Down Expand Up @@ -78,6 +78,7 @@
Patterns = {
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s*(even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\))?(.*)/i,
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(\s*(:nth(?:-last)?(?:-child|-of-type)\(\s*(?:even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\)|[^()]*)\s*\))?(.*)/i,
epseudos: /^((?:[:]{1,2}(?:after|before|first-letter|first-line))|(?:[:]{2,2}(?:selection|backdrop|placeholder)))?(.*)/i,
children: RegExp('^' + whitespace + '*\\>' + whitespace + '*(.*)'),
adjacent: RegExp('^' + whitespace + '*\\+' + whitespace + '*(.*)'),
relative: RegExp('^' + whitespace + '*\\~' + whitespace + '*(.*)'),
Expand Down Expand Up @@ -688,6 +689,10 @@
}
}

else if ((match = selector.match(Patterns.epseudos)) && match[1]) {
source = 'if(!(/1|11/).test(e.nodeType)){' + source + '}';
}

else {

expr = false;
Expand Down
8 changes: 7 additions & 1 deletion src/nwmatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

// accepted prefix identifiers
// (id, class & pseudo-class)
prefixes = '[#.:]?',
prefixes = '(?:[#.:]|::)?',

// accepted attribute operators
operators = '([~*^$|!]?={1})',
Expand Down Expand Up @@ -124,6 +124,8 @@
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s*(even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\))?(.*)/i,
// uistates + dynamic + negation pseudo-classes
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(\s*(:nth(?:-last)?(?:-child|-of-type)\(\s*(?:even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\)|[^()]*)\s*\))?(.*)/i,
// pseudo-elements selectors
epseudos: /^((?:[:]{1,2}(?:after|before|first-letter|first-line))|(?:[:]{2,2}(?:selection|backdrop|placeholder)))?(.*)/i,
// E > F
children: RegExp('^' + whitespace + '*\\>' + whitespace + '*(.*)'),
// E + F
Expand Down Expand Up @@ -1337,6 +1339,10 @@

}

else if ((match = selector.match(Patterns.epseudos)) && match[1]) {
source = 'if(!(/1|11/).test(e.nodeType)){' + source + '}';
}

else {

// this is where external extensions are
Expand Down

0 comments on commit d02f630

Please sign in to comment.