Skip to content

Commit

Permalink
made all pseudo-class names to match case-insensitive, allowed spaces…
Browse files Browse the repository at this point in the history
… in pseudo-class parameters, fixed 'reSplitGroup'
  • Loading branch information
dperini committed Nov 29, 2012
1 parent 91db52f commit 129a989
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
26 changes: 13 additions & 13 deletions src/modules/nwmatcher-pseudos.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

NW.Dom.registerSelector(
'nwmatcher:spseudos',
/^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\((even|odd|(?:[+-]{0,1}\d*n)?[+-]{0,1}\d*)\))?(.*)/,
/^\:(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,
function(match, source) {

var a, n, b, status = true, test, type;
Expand Down Expand Up @@ -121,31 +121,31 @@ NW.Dom.registerSelector(
b = 1;
} else {
b = ((n = match[2].match(/(-?\d+)$/)) ? parseInt(n[1], 10) : 0);
a = ((n = match[2].match(/(-?\d*)n/)) ? parseInt(n[1], 10) : 0);
a = ((n = match[2].match(/(-?\d*)n/i)) ? parseInt(n[1], 10) : 0);
if (n && n[1] == '-') a = -1;
}
test = a > 1 ?
(/last/.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
'n>=' + b + '&&(n-(' + b + '))%' + a + '==0' : a < -1 ?
(/last/.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
'n<=' + b + '&&(n-(' + b + '))%' + a + '==0' : a=== 0 ?
'n==' + b :
(/last/.test(match[1])) ?
(/last/i.test(match[1])) ?
a == -1 ? 'n>=' + b : 'n<=' + b :
a == -1 ? 'n<=' + b : 'n>=' + b;
source =
'if(e!==h){' +
'n=s[' + (/-of-type/.test(match[1]) ? '"nthOfType"' : '"nthElement"') + ']' +
'(e,' + (/last/.test(match[1]) ? 'true' : 'false') + ');' +
'n=s[' + (/-of-type/i.test(match[1]) ? '"nthOfType"' : '"nthElement"') + ']' +
'(e,' + (/last/i.test(match[1]) ? 'true' : 'false') + ');' +
'if(' + test + '){' + source + '}' +
'}';

} else if (match[1]) {

a = /first/.test(match[1]) ? 'previous' : 'next';
n = /only/.test(match[1]) ? 'previous' : 'next';
b = /first|last/.test(match[1]);
type = /-of-type/.test(match[1]) ? '&&n.nodeName!==e.nodeName' : '&&n.nodeName<"@"';
a = /first/i.test(match[1]) ? 'previous' : 'next';
n = /only/i.test(match[1]) ? 'previous' : 'next';
b = /first|last/i.test(match[1]);
type = /-of-type/i.test(match[1]) ? '&&n.nodeName!==e.nodeName' : '&&n.nodeName<"@"';
source = 'if(e!==h){' +
( 'n=e;while((n=n.' + a + 'Sibling)' + type + ');if(!n){' + (b ? source :
'n=e;while((n=n.' + n + 'Sibling)' + type + ');if(!n){' + source + '}') + '}' ) + '}';
Expand All @@ -167,7 +167,7 @@ NW.Dom.registerSelector(

NW.Dom.registerSelector(
'nwmatcher:dpseudos',
/^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(([^()]*|.*)\))?(.*)/,
/^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(([^()]*|.*)\))?(.*)/i,
(function() {

var doc = document,
Expand All @@ -194,7 +194,7 @@ NW.Dom.registerSelector(
NW.Dom.emit('Negation pseudo-class only accepts simple selectors "' + match.join('') + '"');
} else {
if ('compatMode' in doc) {
source = 'if(!' + NW.Dom.compile([ expr ], '', false) + '(e,s,r,d,h,g)){' + source + '}';
source = 'if(!' + NW.Dom.compile(expr, '', false) + '(e,s,r,d,h,g)){' + source + '}';
} else {
source = 'if(!s.match(e, "' + expr.replace(/\x22/g, '\\"') + '",g)){' + source +'}';
}
Expand Down
4 changes: 2 additions & 2 deletions src/nwmatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
'|\\[' + attributes + '\\]' +
'|\\(' + pseudoclass + '\\)' +
'|\\{' + extensions + '\\}' +
'|,' +
'|(?:,|' + whitespace + ')' +
')+',

extendedValidator = standardValidator.replace(pseudoclass, '.*'),
Expand All @@ -104,7 +104,7 @@
whitespace + '|' + whitespace + '$', 'g'),

reSplitGroup = RegExp('(' +
'[^,\\\\\\[\\]]+' +
'[^,\\\\()[\\]]+' +
'|\\[[^[\\]]*\\]|\\[.*\\]' +
'|\\([^()]+\\)|\\(.*\\)' +
'|\\{[^{}]+\\}|\\{.*\\}' +
Expand Down
28 changes: 14 additions & 14 deletions src/nwmatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
// dom properties selector (extension)
'|\\{' + extensions + '\\}' +
// selector group separator (comma)
'|,' +
'|(?:,|' + whitespace + ')' +
// close match group
')+',

Expand All @@ -174,7 +174,7 @@
// split comma groups, exclude commas from
// quotes '' "" and from brackets () [] {}
reSplitGroup = new RegExp('(' +
'[^,\\\\\\[\\]]+' +
'[^,\\\\()[\\]]+' +
'|' + skipsquare +
'|' + skipround +
'|' + skipcurly +
Expand Down Expand Up @@ -473,9 +473,9 @@
// precompiled Regular Expressions
Patterns = {
// structural pseudo-classes and child selectors
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\((even|odd|(?:[+-]{0,1}\d*n)?[+-]{0,1}\d*)\))?(.*)/,
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\(([^()]*|.*)\))?(.*)/,
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(([^()]*|.*)\))?(.*)/i,
// element attribute matcher
attribute: new RegExp('^\\[' + attrmatcher + '\\](.*)'),
// E > F
Expand Down Expand Up @@ -1077,37 +1077,37 @@
} else {
// assumes correct "an+b" format, "b" before "a" to keep "n" values
b = ((n = match[2].match(/(-?\d+)$/)) ? parseInt(n[1], 10) : 0);
a = ((n = match[2].match(/(-?\d*)n/)) ? parseInt(n[1], 10) : 0);
a = ((n = match[2].match(/(-?\d*)n/i)) ? parseInt(n[1], 10) : 0);
if (n && n[1] == '-') a = -1;
}

// build test expression out of structural pseudo (an+b) parameters
// see here: http://www.w3.org/TR/css3-selectors/#nth-child-pseudo
test = a > 1 ?
(/last/.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
'n>=' + b + '&&(n-(' + b + '))%' + a + '==0' : a < -1 ?
(/last/.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
'n<=' + b + '&&(n-(' + b + '))%' + a + '==0' : a=== 0 ?
'n==' + b :
(/last/.test(match[1])) ?
(/last/i.test(match[1])) ?
a == -1 ? 'n>=' + b : 'n<=' + b :
a == -1 ? 'n<=' + b : 'n>=' + b;

// 4 cases: 1 (nth) x 4 (child, of-type, last-child, last-of-type)
source =
'if(e!==h){' +
'n=s[' + (/-of-type/.test(match[1]) ? '"nthOfType"' : '"nthElement"') + ']' +
'(e,' + (/last/.test(match[1]) ? 'true' : 'false') + ');' +
'n=s[' + (/-of-type/i.test(match[1]) ? '"nthOfType"' : '"nthElement"') + ']' +
'(e,' + (/last/i.test(match[1]) ? 'true' : 'false') + ');' +
'if(' + test + '){' + source + '}' +
'}';

} else {
// 6 cases: 3 (first, last, only) x 1 (child) x 2 (-of-type)
a = /first/.test(match[1]) ? 'previous' : 'next';
n = /only/.test(match[1]) ? 'previous' : 'next';
b = /first|last/.test(match[1]);
a = /first/i.test(match[1]) ? 'previous' : 'next';
n = /only/i.test(match[1]) ? 'previous' : 'next';
b = /first|last/i.test(match[1]);

type = /of-type/.test(match[1]) ? '&&n.nodeName!=e.nodeName' : '&&n.nodeName<"@"';
type = /of-type/i.test(match[1]) ? '&&n.nodeName!=e.nodeName' : '&&n.nodeName<"@"';

source = 'if(e!==h){' +
( 'n=e;while((n=n.' + a + 'Sibling)' + type + ');if(!n){' + (b ? source :
Expand Down

0 comments on commit 129a989

Please sign in to comment.