diff --git a/src/dom/selector.js b/src/dom/selector.js index 60b201c..ee7b308 100644 --- a/src/dom/selector.js +++ b/src/dom/selector.js @@ -77,7 +77,14 @@ **/ findElement: function(elements, expression, index) { if (Object.isUndefined(index)) index = 0; - return Element.extend(Sizzle.matches(expression, elements)[index]); + var selector = new Selector(expression), length = elements.length, matchIndex = 0, i; + + // Match each element individually, since Sizzle.matches does not preserve order + for (i = 0; i < length; i++) { + if (selector.match(elements[i]) && index == matchIndex++) { + return Element.extend(elements[i]); + } + } }, /** diff --git a/test/unit/fixtures/selector.html b/test/unit/fixtures/selector.html index 42abcd4..4cfa29f 100644 --- a/test/unit/fixtures/selector.html +++ b/test/unit/fixtures/selector.html @@ -79,4 +79,10 @@