Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6477

Show
Ignore:
Timestamp:
03/27/07 18:20:35 (1 year ago)
Author:
madrobby
Message:

Fix Selector issues with :not and attribute selector double quotes, fixes #7901. [Andrew Dupont]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/trunk/CHANGELOG

    r6476 r6477  
    11*SVN* 
    22 
    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] 
    44 
    55* 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  
    4545  compileXPathMatcher: function() { 
    4646    var e = this.expression, ps = Selector.patterns, 
    47         x = Selector.xpath, le, p, m; 
     47        x = Selector.xpath, le, m; 
    4848 
    4949    if (Selector._cache[e]) { 
     
    131131      'enabled':     "[not(@disabled)]", 
    132132      '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            } 
    140146          } 
    141         } 
    142         return "[not(" + m[6] + ")]"; 
     147        }         
     148        return "[not(" + exclusion.join(" and ") + ")]"; 
    143149      }, 
    144150      'nth-child':      function(m) {  
     
    188194    attrPresence: 'n = h.attrPresence(n, r, "#{1}"); c = false;', 
    189195    attr: function(m) { 
    190       m[3] = m[5] || m[6]
     196      m[3] = (m[5] || m[6])
    191197      return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}"); c = false;').evaluate(m); 
    192198    }, 
    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    }, 
    194203    descendant:   'c = "descendant";', 
    195204    child:        'c = "child";', 
     
    210219    id:           /^#([\w\-\*]+)(\b|$)/, 
    211220    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)/, 
    213222    attrPresence: /^\[([\w]+)\]/, 
    214223    attr:         /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\]]*?)\4|([^'"][^\]]*?)))?\]/ 
     
    378387     
    379388    attr: function(nodes, root, attr, value, operator) { 
     389      if (!nodes) nodes = root.getElementsByTagName("*"); 
    380390      var handler = Selector.operators[operator], results = []; 
    381391      for (var i = 0, node; node = nodes[i]; i++) { 
     
    388398                 
    389399    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("*"); 
    391402      return Selector.pseudos[name](nodes, value, root); 
    392403    } 
     
    489500     
    490501    '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); 
    505504      h.mark(exclusions); 
    506505      for (var i = 0, results = [], node; node = nodes[i]; i++) 
  • spinoffs/prototype/trunk/test/unit/selector.html

    r6476 r6477  
    2626  <p id="p" class="first summary"> 
    2727    <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> or  
     28    <a id="link_1" class="first internal" rel="external nofollow" href="#">with a <em id="em2">link</em></a> or  
    2929    <a id="link_2" class="internal highlight" href="#"><em id="em">two</em></a>. 
    3030    Or <cite id="with_title" title="hello world!">a citation</cite>. 
     
    145145     
    146146    test$$MatchesAncestryWithTokensSeparatedByWhitespace: function() {with(this) { 
    147       assertEnumEqual($('em', 'span'), $$('#fixtures a *')); 
     147      assertEnumEqual($('em2', 'em', 'span'), $$('#fixtures a *')); 
    148148      assertEnumEqual([$('p')], $$('div#fixtures p')); 
    149149    }}, 
     
    317317     
    318318    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') 
    326327    }}, 
    327328