Skip to content

Commit 831b15b

Browse files
committed
Merging a bunch of changes back from Sizzle, by LosTFx.
1 parent 9aa0c69 commit 831b15b

File tree

1 file changed

+76
-65
lines changed

1 file changed

+76
-65
lines changed

src/selector.js

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,19 @@ var Expr = Sizzle.selectors = {
249249
}
250250
},
251251
relative: {
252-
"+": function(checkSet, part){
253-
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
254-
var elem = checkSet[i];
255-
if ( elem ) {
256-
var cur = elem.previousSibling;
257-
while ( cur && cur.nodeType !== 1 ) {
258-
cur = cur.previousSibling;
259-
}
260-
checkSet[i] = typeof part === "string" ?
261-
cur || false :
262-
cur === part;
252+
"+": function(checkSet, part, isXML){
253+
var isPartStr = typeof part === "string",
254+
isTag = isPartStr && !/\W/.test(part),
255+
isPartStrNotTag = isPartStr && !isTag;
256+
if ( isTag && !isXML ) part = part.toUpperCase();
257+
for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
258+
if ( elem = checkSet[i] ) {
259+
while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {};
260+
checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
261+
elem : elem === part;
263262
}
264263
}
265-
266-
if ( typeof part === "string" ) {
264+
if (isPartStrNotTag) {
267265
Sizzle.filter( part, checkSet, true );
268266
}
269267
},
@@ -294,7 +292,7 @@ var Expr = Sizzle.selectors = {
294292
}
295293
},
296294
"": function(checkSet, part, isXML){
297-
var doneName = "done" + (done++), checkFn = dirCheck;
295+
var doneName = done++, checkFn = dirCheck;
298296

299297
if ( !part.match(/\W/) ) {
300298
var nodeCheck = part = isXML ? part : part.toUpperCase();
@@ -304,7 +302,7 @@ var Expr = Sizzle.selectors = {
304302
checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
305303
},
306304
"~": function(checkSet, part, isXML){
307-
var doneName = "done" + (done++), checkFn = dirCheck;
305+
var doneName = done++, checkFn = dirCheck;
308306

309307
if ( typeof part === "string" && !part.match(/\W/) ) {
310308
var nodeCheck = part = isXML ? part : part.toUpperCase();
@@ -334,10 +332,9 @@ var Expr = Sizzle.selectors = {
334332
CLASS: function(match, curLoop, inplace, result, not){
335333
match = " " + match[1].replace(/\\/g, "") + " ";
336334

337-
var elem;
338-
for ( var i = 0; (elem = curLoop[i]) != null; i++ ) {
335+
for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
339336
if ( elem ) {
340-
if ( not ^ (" " + elem.className + " ").indexOf(match) >= 0 ) {
337+
if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
341338
if ( !inplace )
342339
result.push( elem );
343340
} else if ( inplace ) {
@@ -368,7 +365,7 @@ var Expr = Sizzle.selectors = {
368365
}
369366

370367
// TODO: Move to normal caching system
371-
match[0] = "done" + (done++);
368+
match[0] = done++;
372369

373370
return match;
374371
},
@@ -495,44 +492,46 @@ var Expr = Sizzle.selectors = {
495492
},
496493
filter: {
497494
CHILD: function(elem, match){
498-
var type = match[1], parent = elem.parentNode;
499-
500-
var doneName = match[0];
501-
502-
if ( parent && (!parent[ doneName ] || !elem.nodeIndex) ) {
503-
var count = 1;
504-
505-
for ( var node = parent.firstChild; node; node = node.nextSibling ) {
506-
if ( node.nodeType == 1 ) {
507-
node.nodeIndex = count++;
495+
var type = match[1], node = elem;
496+
switch (type) {
497+
case 'only':
498+
case 'first':
499+
while (node = node.previousSibling) {
500+
if ( node.nodeType === 1 ) return false;
501+
}
502+
if ( type == 'first') return true;
503+
node = elem;
504+
case 'last':
505+
while (node = node.nextSibling) {
506+
if ( node.nodeType === 1 ) return false;
508507
}
509-
}
510-
511-
parent[ doneName ] = count - 1;
512-
}
513-
514-
if ( type == "first" ) {
515-
return elem.nodeIndex == 1;
516-
} else if ( type == "last" ) {
517-
return elem.nodeIndex == parent[ doneName ];
518-
} else if ( type == "only" ) {
519-
return parent[ doneName ] == 1;
520-
} else if ( type == "nth" ) {
521-
var add = false, first = match[2], last = match[3];
522-
523-
if ( first == 1 && last == 0 ) {
524508
return true;
525-
}
509+
case 'nth':
510+
var first = match[2], last = match[3];
526511

527-
if ( first == 0 ) {
528-
if ( elem.nodeIndex == last ) {
529-
add = true;
512+
if ( first == 1 && last == 0 ) {
513+
return true;
514+
}
515+
516+
var doneName = match[0],
517+
parent = elem.parentNode;
518+
519+
if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
520+
var count = 0;
521+
for ( node = parent.firstChild; node; node = node.nextSibling ) {
522+
if ( node.nodeType === 1 ) {
523+
node.nodeIndex = ++count;
524+
}
525+
}
526+
parent.sizcache = doneName;
527+
}
528+
529+
var diff = elem.nodeIndex - last;
530+
if ( first == 0 ) {
531+
return diff == 0;
532+
} else {
533+
return ( diff % first == 0 && diff / first >= 0 );
530534
}
531-
} else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) {
532-
add = true;
533-
}
534-
535-
return add;
536535
}
537536
},
538537
PSEUDO: function(elem, match, i, array){
@@ -771,21 +770,27 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
771770
})();
772771

773772
function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
773+
var sibDir = dir == "previousSibling" && !isXML;
774774
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
775775
var elem = checkSet[i];
776776
if ( elem ) {
777+
if ( sibDir && elem.nodeType === 1 ){
778+
elem.sizcache = doneName;
779+
elem.sizset = i;
780+
}
777781
elem = elem[dir];
778782
var match = false;
779783

780-
while ( elem && elem.nodeType ) {
781-
var done = elem[doneName];
782-
if ( done ) {
783-
match = checkSet[ done ];
784+
while ( elem ) {
785+
if ( elem.sizcache === doneName ) {
786+
match = checkSet[elem.sizset];
784787
break;
785788
}
786789

787-
if ( elem.nodeType === 1 && !isXML )
788-
elem[doneName] = i;
790+
if ( elem.nodeType === 1 && !isXML ){
791+
elem.sizcache = doneName;
792+
elem.sizset = i;
793+
}
789794

790795
if ( elem.nodeName === cur ) {
791796
match = elem;
@@ -801,22 +806,28 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
801806
}
802807

803808
function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
809+
var sibDir = dir == "previousSibling" && !isXML;
804810
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
805811
var elem = checkSet[i];
806812
if ( elem ) {
813+
if ( sibDir && elem.nodeType === 1 ) {
814+
elem.sizcache = doneName;
815+
elem.sizset = i;
816+
}
807817
elem = elem[dir];
808818
var match = false;
809819

810-
while ( elem && elem.nodeType ) {
811-
if ( elem[doneName] ) {
812-
match = checkSet[ elem[doneName] ];
820+
while ( elem ) {
821+
if ( elem.sizcache === doneName ) {
822+
match = checkSet[elem.sizset];
813823
break;
814824
}
815825

816826
if ( elem.nodeType === 1 ) {
817-
if ( !isXML )
818-
elem[doneName] = i;
819-
827+
if ( !isXML ) {
828+
elem.sizcache = doneName;
829+
elem.sizset = i;
830+
}
820831
if ( typeof cur !== "string" ) {
821832
if ( elem === cur ) {
822833
match = true;

0 commit comments

Comments
 (0)