Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ var i,
getText,
isXML,
compile,
hasDuplicate,
outermostContext,
hasDuplicate,
sortInput,
sortInputOriginal,

// Local document vars
setDocument,
Expand Down Expand Up @@ -588,26 +590,45 @@ setDocument = Sizzle.setDocument = function( node ) {
// Document order sorting
sortOrder = docElem.compareDocumentPosition ?
function( a, b ) {
var compare;

// Short-circuit on node equality
if ( a === b ) {
hasDuplicate = true;
return 0;
}

if ( (compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b )) ) {
if ( compare & 1 || a.parentNode && a.parentNode.nodeType === 11 ) {
var compare = b.compareDocumentPosition && a.compareDocumentPosition &&
a.compareDocumentPosition( b );

if ( compare ) {
// Detect disconnected nodes
if ( compare & 1 ||
// Support: Opera, Firefox
// Results can't be trusted for document fragment children
a.parentNode && a.parentNode.nodeType === 11 ||
// Support: Webkit
// Detached nodes confoundingly follow *each other*
compare === b.compareDocumentPosition( a ) ) {

// Prioritize our document
if ( a === doc || contains( preferredDoc, a ) ) {
return -1;
}
if ( b === doc || contains( preferredDoc, b ) ) {
return 1;
}
return 0;

// Maintain relative order if neither node is in our document (jQuery #13331)
if ( sortInputOriginal ) {
sortInput = slice.call( sortInput, 0 );
sortInputOriginal = false;
}
return indexOf.call( sortInput, a ) - indexOf.call( sortInput, b );
}

return compare & 4 ? -1 : 1;
}

// Not directly comparable; sort on existence of the method
return a.compareDocumentPosition ? -1 : 1;
} :
function( a, b ) {
Expand All @@ -618,7 +639,7 @@ setDocument = Sizzle.setDocument = function( node ) {
ap = [ a ],
bp = [ b ];

// Exit early if the nodes are identical
// Short-circuit on node equality
if ( a === b ) {
hasDuplicate = true;
return 0;
Expand Down Expand Up @@ -744,6 +765,7 @@ Sizzle.uniqueSort = function( results ) {

// Unless we *know* we can detect duplicates, assume their presence
hasDuplicate = !support.detectDuplicates;
sortInput = sortInputOriginal = results;
results.sort( sortOrder );

if ( hasDuplicate ) {
Expand Down
15 changes: 11 additions & 4 deletions test/unit/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if ( jQuery("<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='1' wi
}

test("Sizzle.uniqueSort", function() {
expect( 12 );
expect( 14 );

function Arrayish( arr ) {
var i = this.length = arr.length;
Expand All @@ -59,15 +59,18 @@ test("Sizzle.uniqueSort", function() {
};

var i, tests,
detached = [],
body = document.body,
fixture = document.getElementById("qunit-fixture"),
detached1 = document.createElement("p"),
detached2 = document.createElement("ul"),
detachedChild = detached1.appendChild( document.createElement("a") ),
detachedGrandchild = detachedChild.appendChild( document.createElement("b") );

for ( i = 0; i < 4; i++ ) {
detached2.appendChild( document.createElement("li") ).id = "i" + i;
for ( i = 0; i < 12; i++ ) {
detached.push( document.createElement("li") );
detached[i].id = "detached" + i;
detached2.appendChild( document.createElement("li") ).id = "detachedChild" + i;
}

tests = {
Expand All @@ -87,7 +90,11 @@ test("Sizzle.uniqueSort", function() {
input: [ body, fixture, fixture, body ],
expected: [ body, fixture ]
},
"Detached elements": {
"Detached": {
input: detached.slice( 0 ),
expected: detached.slice( 0 )
},
"Detached children": {
input: [
detached2.childNodes[0],
detached2.childNodes[1],
Expand Down