Skip to content

Commit e16c7fe

Browse files
committed
Make sure that elements are returned in document order - and that the results are unique.
1 parent 848c45e commit e16c7fe

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/selector.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ var Sizzle = function(selector, context, results, seed) {
111111

112112
if ( extra ) {
113113
Sizzle( extra, context, results, seed );
114+
115+
if ( sortOrder ) {
116+
hasDuplicate = false;
117+
results.sort(sortOrder);
118+
119+
if ( hasDuplicate ) {
120+
for ( var i = 1; i < results.length; i++ ) {
121+
if ( results[i] === results[i-1] ) {
122+
results.splice(i--, 1);
123+
}
124+
}
125+
}
126+
}
114127
}
115128

116129
return results;
@@ -648,6 +661,26 @@ try {
648661
};
649662
}
650663

664+
var sortOrder;
665+
666+
if ( document.documentElement.compareDocumentPosition ) {
667+
sortOrder = function( a, b ) {
668+
var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
669+
if ( ret === 0 ) {
670+
hasDuplicate = true;
671+
}
672+
return ret;
673+
};
674+
} else if ( document.documentElement.sourceIndex === 0 ) {
675+
sortOrder = function( a, b ) {
676+
var ret = a.sourceIndex - b.sourceIndex;
677+
if ( ret === 0 ) {
678+
hasDuplicate = true;
679+
}
680+
return ret;
681+
};
682+
}
683+
651684
// Check to see if the browser returns elements by name when
652685
// querying by getElementById (and provide a workaround)
653686
(function(){

test/unit/selector.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module("selector");
22

33
test("element", function() {
4-
expect(9);
4+
expect(13);
55
reset();
66

77
ok( jQuery("*").size() >= 30, "Select all" );
@@ -18,6 +18,13 @@ test("element", function() {
1818

1919
ok( jQuery("#length").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
2020
ok( jQuery("#lengthtest input").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
21+
22+
// Check for unique-ness and sort order
23+
isSet( jQuery("*"), jQuery("*, *"), "Check for duplicates: *, *" );
24+
isSet( jQuery("p"), jQuery("p, div p"), "Check for duplicates: p, div p" );
25+
26+
t( "Checking sort order", "h2, h1", ["header", "banner", "userAgent"] );
27+
t( "Checking sort order", "p, p a", ["firstp", "simon1", "ap", "google", "groups", "anchor1", "mark", "sndp", "en", "yahoo", "sap", "anchor2", "simon", "first"] );
2128
});
2229

2330
if ( location.protocol != "file:" ) {
@@ -333,7 +340,7 @@ test("pseudo (:) selectors", function() {
333340
t( "Form element :text", "#form :text", ["text1", "text2", "hidden2", "name"] );
334341
t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] );
335342
t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] );
336-
t( "Form element :checkbox:checked, :radio:checked", "#form :checkbox:checked, #form :radio:checked", ["check1", "radio2"] );
343+
t( "Form element :radio:checked, :checkbox:checked", "#form :radio:checked, #form :checkbox:checked", ["radio2", "check1"] );
337344

338345
t( "Headers", ":header", ["header", "banner", "userAgent"] );
339346
t( "Has Children - :has()", "p:has(a)", ["firstp","ap","en","sap"] );

0 commit comments

Comments
 (0)