From 432a9422d6beabaa6b4b5b652efe25e1715a8ad2 Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sun, 22 Feb 2009 15:59:01 -0600 Subject: [PATCH] Fix issue where a Selector query rooted on a node that had not been attached to the document failed in IE. [#464 state:resolved] (jddalton, kangax, Douglas Fraser, Andrew Dupont) --- CHANGELOG | 2 ++ src/dom/selector.js | 19 +++++++++++++++++-- test/unit/selector_test.js | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 11c52ca..c4a3071 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Fix issue where a Selector query rooted on a node that had not been attached to the document failed in IE. [#464 state:resolved] (jddalton, kangax, Douglas Fraser, Andrew Dupont) + * Fix Selector to match elements with attributes containing hyphens. [#285 state:resolved] (leiyou, jddalton, kangax) * Make sure Form.reset always returns a reference to the receiver element. [#309 state:resolved] (Phil, kangax) diff --git a/src/dom/selector.js b/src/dom/selector.js index 158877c..3dc4791 100644 --- a/src/dom/selector.js +++ b/src/dom/selector.js @@ -485,8 +485,23 @@ Object.extend(Selector, { id: function(nodes, root, id, combinator) { var targetNode = $(id), h = Selector.handlers; - if (!targetNode) return []; - if (!nodes && root == document) return [targetNode]; + + if (root == document) { + // We don't have to deal with orphan nodes. Easy. + if (!targetNode) return []; + if (!nodes) return [targetNode]; + } else { + // In IE, we can check sourceIndex to see if root is attached + // to the document. If not (or if sourceIndex is not present), + // we do it the hard way. + if (!root.sourceIndex || root.sourceIndex < 1) { + var nodes = root.getElementsByTagName('*'); + for (var j = 0, node; node = nodes[j]; j++) { + if (node.id === id) return [node]; + } + } + } + if (nodes) { if (combinator) { if (combinator == 'child') { diff --git a/test/unit/selector_test.js b/test/unit/selector_test.js index c756fbe..4fb7bf2 100644 --- a/test/unit/selector_test.js +++ b/test/unit/selector_test.js @@ -371,6 +371,21 @@ new Test.Unit.Runner({ 'div.is_counted' ); }, + + testSelectorNotInsertedNodes: function() { + window.debug = true; + var wrapper = new Element("div"); + wrapper.update("
"); + this.assertNotNullOrUndefined(wrapper.select('[id=myTD]')[0], + 'selecting: [id=myTD]'); + this.assertNotNullOrUndefined(wrapper.select('#myTD')[0], + 'selecting: #myTD'); + this.assertNotNullOrUndefined(wrapper.select('td')[0], + 'selecting: td'); + this.assert($$('#myTD').length == 0, + 'should not turn up in document-rooted search'); + window.debug = false; + }, testElementDown: function() { var a = $('dupL4');