@@ -249,21 +249,19 @@ var Expr = Sizzle.selectors = {
249
249
}
250
250
} ,
251
251
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 ;
263
262
}
264
263
}
265
-
266
- if ( typeof part === "string" ) {
264
+ if ( isPartStrNotTag ) {
267
265
Sizzle . filter ( part , checkSet , true ) ;
268
266
}
269
267
} ,
@@ -294,7 +292,7 @@ var Expr = Sizzle.selectors = {
294
292
}
295
293
} ,
296
294
"" : function ( checkSet , part , isXML ) {
297
- var doneName = " done" + ( done ++ ) , checkFn = dirCheck ;
295
+ var doneName = done ++ , checkFn = dirCheck ;
298
296
299
297
if ( ! part . match ( / \W / ) ) {
300
298
var nodeCheck = part = isXML ? part : part . toUpperCase ( ) ;
@@ -304,7 +302,7 @@ var Expr = Sizzle.selectors = {
304
302
checkFn ( "parentNode" , part , doneName , checkSet , nodeCheck , isXML ) ;
305
303
} ,
306
304
"~" : function ( checkSet , part , isXML ) {
307
- var doneName = " done" + ( done ++ ) , checkFn = dirCheck ;
305
+ var doneName = done ++ , checkFn = dirCheck ;
308
306
309
307
if ( typeof part === "string" && ! part . match ( / \W / ) ) {
310
308
var nodeCheck = part = isXML ? part : part . toUpperCase ( ) ;
@@ -334,10 +332,9 @@ var Expr = Sizzle.selectors = {
334
332
CLASS : function ( match , curLoop , inplace , result , not ) {
335
333
match = " " + match [ 1 ] . replace ( / \\ / g, "" ) + " " ;
336
334
337
- var elem ;
338
- for ( var i = 0 ; ( elem = curLoop [ i ] ) != null ; i ++ ) {
335
+ for ( var i = 0 , elem ; ( elem = curLoop [ i ] ) != null ; i ++ ) {
339
336
if ( elem ) {
340
- if ( not ^ ( " " + elem . className + " " ) . indexOf ( match ) >= 0 ) {
337
+ if ( not ^ ( elem . className && ( " " + elem . className + " " ) . indexOf ( match ) >= 0 ) ) {
341
338
if ( ! inplace )
342
339
result . push ( elem ) ;
343
340
} else if ( inplace ) {
@@ -368,7 +365,7 @@ var Expr = Sizzle.selectors = {
368
365
}
369
366
370
367
// TODO: Move to normal caching system
371
- match [ 0 ] = " done" + ( done ++ ) ;
368
+ match [ 0 ] = done ++ ;
372
369
373
370
return match ;
374
371
} ,
@@ -495,44 +492,46 @@ var Expr = Sizzle.selectors = {
495
492
} ,
496
493
filter : {
497
494
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 ;
508
507
}
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 ) {
524
508
return true ;
525
- }
509
+ case 'nth' :
510
+ var first = match [ 2 ] , last = match [ 3 ] ;
526
511
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 ) ;
530
534
}
531
- } else if ( ( elem . nodeIndex - last ) % first == 0 && ( elem . nodeIndex - last ) / first >= 0 ) {
532
- add = true ;
533
- }
534
-
535
- return add ;
536
535
}
537
536
} ,
538
537
PSEUDO : function ( elem , match , i , array ) {
@@ -771,21 +770,27 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
771
770
} ) ( ) ;
772
771
773
772
function dirNodeCheck ( dir , cur , doneName , checkSet , nodeCheck , isXML ) {
773
+ var sibDir = dir == "previousSibling" && ! isXML ;
774
774
for ( var i = 0 , l = checkSet . length ; i < l ; i ++ ) {
775
775
var elem = checkSet [ i ] ;
776
776
if ( elem ) {
777
+ if ( sibDir && elem . nodeType === 1 ) {
778
+ elem . sizcache = doneName ;
779
+ elem . sizset = i ;
780
+ }
777
781
elem = elem [ dir ] ;
778
782
var match = false ;
779
783
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 ] ;
784
787
break ;
785
788
}
786
789
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
+ }
789
794
790
795
if ( elem . nodeName === cur ) {
791
796
match = elem ;
@@ -801,22 +806,28 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
801
806
}
802
807
803
808
function dirCheck ( dir , cur , doneName , checkSet , nodeCheck , isXML ) {
809
+ var sibDir = dir == "previousSibling" && ! isXML ;
804
810
for ( var i = 0 , l = checkSet . length ; i < l ; i ++ ) {
805
811
var elem = checkSet [ i ] ;
806
812
if ( elem ) {
813
+ if ( sibDir && elem . nodeType === 1 ) {
814
+ elem . sizcache = doneName ;
815
+ elem . sizset = i ;
816
+ }
807
817
elem = elem [ dir ] ;
808
818
var match = false ;
809
819
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 ] ;
813
823
break ;
814
824
}
815
825
816
826
if ( elem . nodeType === 1 ) {
817
- if ( ! isXML )
818
- elem [ doneName ] = i ;
819
-
827
+ if ( ! isXML ) {
828
+ elem . sizcache = doneName ;
829
+ elem . sizset = i ;
830
+ }
820
831
if ( typeof cur !== "string" ) {
821
832
if ( elem === cur ) {
822
833
match = true ;
0 commit comments