Ticket #7873: final_selectors_patch.diff
| File final_selectors_patch.diff, 3.8 kB (added by savetheclocktower, 2 years ago) |
|---|
-
src/selector.js
old new 162 162 'only-of-type': function(m) { 163 163 var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m); 164 164 }, 165 nth: function( predicate, m) {166 var mm, formula = m[6] ;165 nth: function(fragment, m) { 166 var mm, formula = m[6], predicate; 167 167 if (formula == 'even') formula = '2n+0'; 168 168 if (formula == 'odd') formula = '2n+1'; 169 169 if (mm = formula.match(/^(\d+)$/)) // digit only 170 predicate += "= " + mm[1]; 171 if (mm = formula.match(/^(\d+)?n(\+(\d+))?/)) { // an+b 170 return '[' + fragment + "= " + mm[1] + ']'; 171 if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b 172 if (mm[1] == "-") mm[1] = -1; 172 173 var a = mm[1] ? Number(mm[1]) : 1; 173 var b = mm[3] ? Number(mm[3]) : 0; 174 predicate += "mod " + a + " = " + b; 174 var b = mm[2] ? Number(mm[2]) : 0; 175 predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + 176 "((#{fragment} - #{b}) div #{a} >= 0)]"; 177 return new Template(predicate).evaluate({ 178 fragment: fragment, a: a, b: b }); 175 179 } 176 return "[" + predicate + "]";177 180 } 178 181 } 179 182 }, … … 265 268 descendant: function(nodes) { 266 269 var h = Selector.handlers; 267 270 for (var i = 0, results = [], node; node = nodes[i]; i++) 268 h.concat(results, Element.descendants(node));271 h.concat(results, node.getElementsByTagName('*')); 269 272 return results; 270 273 }, 271 274 272 275 child: function(nodes) { 273 276 var h = Selector.handlers; 274 for (var i = 0, results = [], node; node = nodes[i]; i++) 275 h.concat(results, Element.immediateDescendants(node)); 277 for (var i = 0, results = [], node; node = nodes[i]; i++) { 278 for (var j = 0, children = [], child; child = node.childNodes[j]; j++) 279 if (child.nodeType == 1 && child.tagName != '!') results.push(child); 280 } 276 281 return results; 277 282 }, 278 283 … … 432 437 return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root); 433 438 }, 434 439 440 // handles the an+b logic 441 getIndices: function(a, b, total) { 442 if (a == 0) return b > 0 ? [b] : []; 443 return $R(1, total).inject([], function(memo, i) { 444 if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i); 445 return memo; 446 }); 447 }, 448 435 449 // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type 436 450 nth: function(nodes, formula, root, reverse, ofType) { 451 if (nodes.length == 0) return []; 437 452 if (formula == 'even') formula = '2n+0'; 438 453 if (formula == 'odd') formula = '2n+1'; 439 454 var h = Selector.handlers, results = [], indexed = [], m; … … 448 463 formula = Number(formula); 449 464 for (var i = 0, node; node = nodes[i]; i++) 450 465 if (node.nodeIndex == formula) results.push(node); 451 } else if (m = formula.match(/^(\d+)?n(\+(\d+))?$/)) { // an+b 466 } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b 467 if (m[1] == "-") m[1] = -1; 452 468 var a = m[1] ? Number(m[1]) : 1; 453 var b = m[3] ? Number(m[3]) : 0; 454 for (var i = 0, node; node = nodes[i]; i++) 455 if (node.nodeIndex % a == b) results.push(node); 469 var b = m[2] ? Number(m[2]) : 0; 470 var indices = Selector.pseudos.getIndices(a, b, nodes.length); 471 for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { 472 for (var j = 0; j < l; j++) 473 if (node.nodeIndex == indices[j]) results.push(node); 474 } 456 475 } 457 476 h.unmark(nodes); 458 477 h.unmark(indexed);