diff --git a/src/selector.js b/src/selector.js index e03e0ab..a02c3b6 100644 --- a/src/selector.js +++ b/src/selector.js @@ -42,8 +42,7 @@ var Selector = Class.create({ if (!Selector._div) Selector._div = new Element('div'); // Make sure the browser treats the selector as valid. Test on an - // isolated element to minimize cost of this check. - + // isolated element to minimize cost of this check. try { Selector._div.querySelector(this.expression); } catch(e) { @@ -110,11 +109,22 @@ var Selector = Class.create({ findElements: function(root) { root = root || document; - var results; - + var e = this.expression, results; + switch (this.mode) { case 'selectorsAPI': - return $A(root.querySelectorAll(this.expression)); + // querySelectorAll queries document-wide, then filters to children + // of the context element. That's not what we want. + // Add an explicit context to the selector if necessary. + if (root !== document) { + var oldId = root.id, id = $(root.identify()); + e = "#" + id + " " + e; + } + + results = $A(root.querySelectorAll(e)).map(Element.extend); + root.id = oldId; + + return results; case 'xpath': return document._getElementsByXPath(this.xpath, root); default: @@ -644,6 +654,7 @@ Object.extend(Selector, { }, operators: { + '=': function(nv, v) { return nv == v; }, '^=': function(nv, v) { return nv == v || nv && nv.startsWith(v); }, '$=': function(nv, v) { return nv == v || nv && nv.endsWith(v); }, '*=': function(nv, v) { return nv == v || nv && nv.include(v); }, diff --git a/test/lib/unittest.js b/test/lib/unittest.js index e4dd197..afe7eeb 100644 --- a/test/lib/unittest.js +++ b/test/lib/unittest.js @@ -117,11 +117,13 @@ Test.Unit.Logger = Class.create({ }, getLastLogLine: function() { + //return this.element.descendants('tr').last(); var trs = this.element.getElementsByTagName('tr'); return $(trs[trs.length - 1]); }, getMessageCell: function() { + return this.getLastLogLine().down('td', 2); var tds = this.getLastLogLine().getElementsByTagName('td'); return $(tds[2]); }, diff --git a/test/unit/selector.html b/test/unit/selector.html index 3c00d51..3a8c9ac 100644 --- a/test/unit/selector.html +++ b/test/unit/selector.html @@ -188,7 +188,7 @@ }, testSelectorWithTagNameAndNegatedAttributeValue: function() { - this.assertEnumEqual([], $$('a[href!=#]')); + this.assertEnumEqual([], $$('a[href!="#"]')); }, testSelectorWithBracketAttributeValue: function() { @@ -461,6 +461,13 @@ Selector.matchElements($('counted_container').descendants(), 'div'), 'div.is_counted', 'div.is_counted' ); + }, + + testElementDown: function() { + var a = $('dupL4'); + var b = $('dupContainer').down('#dupL4'); + + this.assertEqual(a, b); } });