Skip to main content

Bug Tracker

Side navigation

Ticket #1727: 1727.diff


File 1727.diff, 1.4 KB (added by davidserduke, November 06, 2007 07:27PM UTC)

patch

Index: src/selector.js
===================================================================
--- src/selector.js	(revision 3790)
+++ src/selector.js	(working copy)
@@ -349,11 +349,14 @@
 			// 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 @@
 
 					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 )

Download in other formats:

Original Format