// Re-organize the results, so that they're consistent
if ( m ) {
- m = [ 0, m[2], m[3], m[1] ];
+ m = [ 0, m[2], m[3], m[1] ];
} else {
// Otherwise, do a traditional filter check for
var last;
// Look for common filter expressions
- while ( t && t != last ) {
+ while ( t && t != last ) {
last = t;
var p = jQuery.parse, m;
// We can get a speed boost by handling nth-child here
} else if ( m[1] == ":" && m[2] == "nth-child" ) {
var merge = {}, tmp = [],
- test = /(\d*)n\+?(\d*)/.exec(
+ // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
+ test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" ||
- !/\D/.test(m[3]) && "n+" + m[3] || m[3]),
- first = (test[1] || 1) - 0, last = test[2] - 0;
-
+ !/\D/.test(m[3]) && "0n+" + m[3] || m[3]),
+ // calculate the numbers (first)n+(last) including if they are negative
+ first = (test[1] + (test[2] || 1)) - 0, last = test[3] - 0;
+
+ // loop through all the elements left in the jQuery object
for ( var i = 0, rl = r.length; i < rl; i++ ) {
var node = r[i], parentNode = node.parentNode, id = jQuery.data(parentNode);
var add = false;
- if ( first == 1 ) {
- if ( last == 0 || node.nodeIndex == last )
+ if ( first == 0 ) {
+ if ( node.nodeIndex == last )
add = true;
- } else if ( (node.nodeIndex + last) % first == 0 )
+ } else if ( (node.nodeIndex - last) % first == 0 && (node.nodeIndex - last) / first >= 0 )
add = true;
if ( add ^ not )
});
test("child and adjacent", function() {
- expect(19);
+ expect(37);
t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
t( "Nth-child", "#main form#form > *:nth-child(2)", ["text2"] );
t( "Nth-child", "#main form#form > :nth-child(2)", ["text2"] );
+
+ t( "Nth-child", "#form select:first option:nth-child(3)", ["option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(0n+3)", ["option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(1n)", ["option1a", "option1b", "option1c", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(n)", ["option1a", "option1b", "option1c", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(even)", ["option1b", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(odd)", ["option1a", "option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(2n)", ["option1b", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(2n+1)", ["option1a", "option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n)", ["option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n+1)", ["option1a", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n+2)", ["option1b"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n+3)", ["option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n-1)", ["option1b"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n-2)", ["option1a", "option1d"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n-3)", ["option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(3n+0)", ["option1c"] );
+ t( "Nth-child", "#form select:first option:nth-child(-n+3)", ["option1a", "option1b", "option1c"] );
});
test("attributes", function() {