Changeset 6477
- Timestamp:
- 03/27/07 18:20:35 (1 year ago)
- Files:
-
- spinoffs/prototype/trunk/CHANGELOG (modified) (1 diff)
- spinoffs/prototype/trunk/src/selector.js (modified) (7 diffs)
- spinoffs/prototype/trunk/test/unit/selector.html (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
spinoffs/prototype/trunk/CHANGELOG
r6476 r6477 1 1 *SVN* 2 2 3 * Fix issues with Selector an+b logic, performance improvements. Closes #7873. [Andrew Dupont]3 * Fix issues with Selector an+b logic, :not support, attribute selector double quotes, plus performance improvements. Closes #7873, #7901. [Andrew Dupont] 4 4 5 5 * Fix an issue with Element.getDimensions with some element types on non IE-browsers. Closes #7683. [Andrew Dupont] spinoffs/prototype/trunk/src/selector.js
r6476 r6477 45 45 compileXPathMatcher: function() { 46 46 var e = this.expression, ps = Selector.patterns, 47 x = Selector.xpath, le, p,m;47 x = Selector.xpath, le, m; 48 48 49 49 if (Selector._cache[e]) { … … 131 131 'enabled': "[not(@disabled)]", 132 132 'not': function(m) { 133 if (!m[6]) return ''; 134 var p = Selector.patterns, x = Selector.xpath; 135 for (var i in p) { 136 if (mm = m[6].match(p[i])) { 137 var ss = typeof x[i] == 'function' ? x[i](mm) : new Template(x[i]).evaluate(mm); 138 m[6] = ss.substring(1, ss.length - 1); 139 break; 133 var e = m[6], p = Selector.patterns, 134 x = Selector.xpath, le, m, v; 135 136 var exclusion = []; 137 while (e && le != e && (/\S/).test(e)) { 138 le = e; 139 for (var i in p) { 140 if (m = e.match(p[i])) { 141 v = typeof x[i] == 'function' ? x[i](m) : new Template(x[i]).evaluate(m); 142 exclusion.push("(" + v.substring(1, v.length - 1) + ")"); 143 e = e.replace(m[0], ''); 144 break; 145 } 140 146 } 141 } 142 return "[not(" + m[6]+ ")]";147 } 148 return "[not(" + exclusion.join(" and ") + ")]"; 143 149 }, 144 150 'nth-child': function(m) { … … 188 194 attrPresence: 'n = h.attrPresence(n, r, "#{1}"); c = false;', 189 195 attr: function(m) { 190 m[3] = m[5] || m[6];196 m[3] = (m[5] || m[6]); 191 197 return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}"); c = false;').evaluate(m); 192 198 }, 193 pseudo: 'n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;', 199 pseudo: function(m) { 200 if (m[6]) m[6] = m[6].replace(/"/g, '\\"'); 201 return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m); 202 }, 194 203 descendant: 'c = "descendant";', 195 204 child: 'c = "child";', … … 210 219 id: /^#([\w\-\*]+)(\b|$)/, 211 220 className: /^\.([\w\-\*]+)(\b|$)/, 212 pseudo: /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$ )/,221 pseudo: /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|\s)/, 213 222 attrPresence: /^\[([\w]+)\]/, 214 223 attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\]]*?)\4|([^'"][^\]]*?)))?\]/ … … 378 387 379 388 attr: function(nodes, root, attr, value, operator) { 389 if (!nodes) nodes = root.getElementsByTagName("*"); 380 390 var handler = Selector.operators[operator], results = []; 381 391 for (var i = 0, node; node = nodes[i]; i++) { … … 388 398 389 399 pseudo: function(nodes, name, value, root, combinator) { 390 if (combinator) nodes = this[combinator](nodes); 400 if (nodes && combinator) nodes = this[combinator](nodes); 401 if (!nodes) nodes = root.getElementsByTagName("*"); 391 402 return Selector.pseudos[name](nodes, value, root); 392 403 } … … 489 500 490 501 'not': function(nodes, selector, root) { 491 var h = Selector.handlers, exclusions = $A(nodes), selectorType, m; 492 for (var i in Selector.patterns) { 493 if (m = selector.match(Selector.patterns[i])) { 494 selectorType = i; break; 495 } 496 } 497 switch(selectorType) { 498 case 'className': case 'tagName': case 'id': // fallthroughs 499 case 'attrPresence': exclusions = h[selectorType](exclusions, root, m[1], false); break; 500 case 'attr': m[3] = m[5] || m[6]; exclusions = h.attr(exclusions, root, m[1], m[3], m[2]); break; 501 case 'pseudo': exclusions = h.pseudo(exclusions, m[1], m[6], root, false); break; 502 // only 'simple selectors' (one token) allowed in a :not clause 503 default: throw 'Illegal selector in :not clause.'; 504 } 502 var h = Selector.handlers, selectorType, m; 503 var exclusions = new Selector(selector).findElements(root); 505 504 h.mark(exclusions); 506 505 for (var i = 0, results = [], node; node = nodes[i]; i++) spinoffs/prototype/trunk/test/unit/selector.html
r6476 r6477 26 26 <p id="p" class="first summary"> 27 27 <strong id="strong">This</strong> is a short blurb 28 <a id="link_1" class="first internal" rel="external nofollow" href="#">with a link</a> or28 <a id="link_1" class="first internal" rel="external nofollow" href="#">with a <em id="em2">link</em></a> or 29 29 <a id="link_2" class="internal highlight" href="#"><em id="em">two</em></a>. 30 30 Or <cite id="with_title" title="hello world!">a citation</cite>. … … 145 145 146 146 test$$MatchesAncestryWithTokensSeparatedByWhitespace: function() {with(this) { 147 assertEnumEqual($('em ', 'span'), $$('#fixtures a *'));147 assertEnumEqual($('em2', 'em', 'span'), $$('#fixtures a *')); 148 148 assertEnumEqual([$('p')], $$('div#fixtures p')); 149 149 }}, … … 317 317 318 318 testSelectorWithNot: function() {with(this) { 319 assertEnumEqual([$('link_2')], $$('#p a:not(:first-of-type)')); 320 assertEnumEqual([$('link_1')], $$('#p a:not(:last-of-type)')); 321 assertEnumEqual([$('link_2')], $$('#p a:not(:nth-of-type(1))')); 322 assertEnumEqual([$('link_1')], $$('#p a:not(:nth-last-of-type(1))')); 323 assertEnumEqual([$('link_2')], $$('#p a:not([rel~=nofollow])')); 324 assertEnumEqual([$('link_2')], $$('#p a:not([rel^=external])')); 325 assertEnumEqual([$('link_2')], $$('#p a:not([rel$=nofollow])')); 319 assertEnumEqual([$('link_2')], $$('#p a:not(a:first-of-type)'), 'first-of-type'); 320 assertEnumEqual([$('link_1')], $$('#p a:not(a:last-of-type)'), 'last-of-type'); 321 assertEnumEqual([$('link_2')], $$('#p a:not(a:nth-of-type(1))'), 'nth-of-type'); 322 assertEnumEqual([$('link_1')], $$('#p a:not(a:nth-last-of-type(1))'), 'nth-last-of-type'); 323 assertEnumEqual([$('link_2')], $$('#p a:not([rel~=nofollow])'), 'attribute 1'); 324 assertEnumEqual([$('link_2')], $$('#p a:not(a[rel^=external])'), 'attribute 2'); 325 assertEnumEqual([$('link_2')], $$('#p a:not(a[rel$=nofollow])'), 'attribute 3'); 326 assertEnumEqual([$('em')], $$('#p a:not(a[rel$="nofollow"]) > em'), 'attribute 4') 326 327 }}, 327 328