Account for context to assure Element#down, et al., work properly with Selectors API.

This commit is contained in:
Andrew Dupont 2008-03-31 12:51:44 -05:00
parent ee52460014
commit ab8cc48b8d
3 changed files with 26 additions and 6 deletions

View File

@ -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); },

View File

@ -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]);
},

View File

@ -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);
}
});