From 084079d2fd1c7ed3582612cc32b01346b43fb415 Mon Sep 17 00:00:00 2001 From: David Serduke Date: Fri, 16 Nov 2007 19:01:53 +0000 Subject: [PATCH 1/1] Fixed #1727 bug where :nth-child() was non-standard with CSS3 plus two minor white space changes in selector.js. --- src/selector.js | 21 ++++++++++++--------- test/unit/selector.js | 21 ++++++++++++++++++++- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/selector.js b/src/selector.js index 82627e1..b272acd 100644 --- a/src/selector.js +++ b/src/selector.js @@ -192,7 +192,7 @@ jQuery.extend({ // 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 @@ -297,7 +297,7 @@ jQuery.extend({ var last; // Look for common filter expressions - while ( t && t != last ) { + while ( t && t != last ) { last = t; var p = jQuery.parse, m; @@ -349,11 +349,14 @@ jQuery.extend({ // 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); @@ -369,10 +372,10 @@ jQuery.extend({ 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 ) diff --git a/test/unit/selector.js b/test/unit/selector.js index d6121f2..2c80d79 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -107,7 +107,7 @@ test("multiple", function() { }); 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"] ); @@ -130,6 +130,25 @@ test("child and adjacent", function() { 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() { -- 1.7.10.4