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
56 changes: 30 additions & 26 deletions dist/sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2013-09-12
* Date: 2013-10-02
*/
(function( window ) {

Expand Down Expand Up @@ -709,48 +709,52 @@ setDocument = Sizzle.setDocument = function( node ) {
return 0;
}

var compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b );

// Sort on method existence if only one input has compareDocumentPosition
var compare = !a.compareDocumentPosition - !b.compareDocumentPosition;
if ( compare ) {
// Disconnected nodes
if ( compare & 1 ||
(!support.sortDetached && b.compareDocumentPosition( a ) === compare) ) {
return compare;
}

// Choose the first element that is related to our preferred document
if ( a === doc || contains(preferredDoc, a) ) {
return -1;
}
if ( b === doc || contains(preferredDoc, b) ) {
return 1;
}
// Calculate position if both inputs share a document
compare = ( a.ownerDocument || a ) !== ( b.ownerDocument || b ) ? 1 :
a.compareDocumentPosition( b );

// Disconnected nodes
if ( compare & 1 ||
(!support.sortDetached && b.compareDocumentPosition( a ) === compare) ) {

// Maintain original order
return sortInput ?
( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) :
0;
// Choose the first element that is related to our preferred document
if ( a === doc || a.ownerDocument === preferredDoc && contains(preferredDoc, a) ) {
return -1;
}
if ( b === doc || b.ownerDocument === preferredDoc && contains(preferredDoc, b) ) {
return 1;
}

return compare & 4 ? -1 : 1;
// Maintain original order
return sortInput ?
( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) :
0;
}

// Not directly comparable, sort on existence of method
return a.compareDocumentPosition ? -1 : 1;
return compare & 4 ? -1 : 1;
} :
function( a, b ) {
// Exit early if the nodes are identical
if ( a === b ) {
hasDuplicate = true;
return 0;
}

var cur,
i = 0,
aup = a.parentNode,
bup = b.parentNode,
ap = [ a ],
bp = [ b ];

// Exit early if the nodes are identical
if ( a === b ) {
hasDuplicate = true;
return 0;

// Parentless nodes are either documents or disconnected
} else if ( !aup || !bup ) {
if ( !aup || !bup ) {
return a === doc ? -1 :
b === doc ? 1 :
aup ? -1 :
Expand Down
2 changes: 1 addition & 1 deletion dist/sizzle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sizzle.min.map

Large diffs are not rendered by default.

57 changes: 32 additions & 25 deletions src/sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,48 +709,55 @@ setDocument = Sizzle.setDocument = function( node ) {
return 0;
}

var compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b );

// Sort on method existence if only one input has compareDocumentPosition
var compare = !a.compareDocumentPosition - !b.compareDocumentPosition;
if ( compare ) {
// Disconnected nodes
if ( compare & 1 ||
(!support.sortDetached && b.compareDocumentPosition( a ) === compare) ) {
return compare;
}

// Choose the first element that is related to our preferred document
if ( a === doc || contains(preferredDoc, a) ) {
return -1;
}
if ( b === doc || contains(preferredDoc, b) ) {
return 1;
}
// Calculate position if both inputs belong to the same document
compare = ( a.ownerDocument || a ) === ( b.ownerDocument || b ) ?
a.compareDocumentPosition( b ) :

// Otherwise we know they are disconnected
1;

// Disconnected nodes
if ( compare & 1 ||
(!support.sortDetached && b.compareDocumentPosition( a ) === compare) ) {

// Maintain original order
return sortInput ?
( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) :
0;
// Choose the first element that is related to our preferred document
if ( a === doc || a.ownerDocument === preferredDoc && contains(preferredDoc, a) ) {
return -1;
}
if ( b === doc || b.ownerDocument === preferredDoc && contains(preferredDoc, b) ) {
return 1;
}

return compare & 4 ? -1 : 1;
// Maintain original order
return sortInput ?
( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) :
0;
}

// Not directly comparable, sort on existence of method
return a.compareDocumentPosition ? -1 : 1;
return compare & 4 ? -1 : 1;
} :
function( a, b ) {
// Exit early if the nodes are identical
if ( a === b ) {
hasDuplicate = true;
return 0;
}

var cur,
i = 0,
aup = a.parentNode,
bup = b.parentNode,
ap = [ a ],
bp = [ b ];

// Exit early if the nodes are identical
if ( a === b ) {
hasDuplicate = true;
return 0;

// Parentless nodes are either documents or disconnected
} else if ( !aup || !bup ) {
if ( !aup || !bup ) {
return a === doc ? -1 :
b === doc ? 1 :
aup ? -1 :
Expand Down
52 changes: 28 additions & 24 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,32 @@
"wsh": true,

"globals": {
"QUnit": true,
"jQuery": true,
"q": true,
"t": true,
"url": true,
"createWithFriesXML": true,
"Sizzle": true,
"module": true,
"test": true,
"asyncTest": true,
"expect": true,
"stop": true,
"start": true,
"ok": true,
"equal": true,
"notEqual": true,
"deepEqual": true,
"notDeepEqual": true,
"strictEqual": true,
"notStrictEqual": true,
"raises": true,
"fireNative": true,
"moduleTeardown": true
"QUnit": false,
"module": false,
"test": false,
"asyncTest": false,
"expect": false,
"stop": false,
"start": false,
"ok": false,
"equal": false,
"notEqual": false,
"deepEqual": false,
"notDeepEqual": false,
"strictEqual": false,
"notStrictEqual": false,
"raises": false,

"createWithFriesXML": false,
"fireNative": false,
"moduleTeardown": false,
"q": false,
"t": false,
"testIframeWithCallback": false,
"url": false,

"jQuery": false,

"Sizzle": false
}
}
}
20 changes: 20 additions & 0 deletions test/data/mixed_sort.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../dist/sizzle.js"></script>
</head>
<body>
<script>
var doc = parent.document,
unframed = [ doc.getElementById( "qunit-fixture" ), doc.body, doc.documentElement ],
framed = Sizzle( "*" );

window.parent.iframeCallback(
Sizzle.uniqueSort( unframed.concat( framed ) ),
framed.concat( unframed.reverse() ),
"Mixed array was sorted correctly"
);
</script>
</body>
</html>
23 changes: 23 additions & 0 deletions test/data/testinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,27 @@ fireNative = document.createEvent ?
node.fireEvent( "on" + type, event );
};

function testIframeWithCallback( title, fileName, func ) {
test( title, function() {
var iframe;

stop();
window.iframeCallback = function() {
var self = this,
args = arguments;
setTimeout(function() {
window.iframeCallback = undefined;
iframe.remove();
func.apply( self, args );
func = function() {};
start();
}, 0 );
};
iframe = jQuery( "<div/>" ).css({ position: "absolute", width: "500px", left: "-600px" })
.append( jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) ) )
.appendTo( "#qunit-fixture" );
});
};
window.iframeCallback = undefined;

function moduleTeardown() {}
2 changes: 2 additions & 0 deletions test/unit/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,5 @@ test("Sizzle.uniqueSort", function() {
deepEqual( Sizzle.uniqueSort( new Arrayish(test.input) ).slice( 0, length ), test.expected, label + " (quasi-array)" );
});
});

testIframeWithCallback( "Sizzle.uniqueSort works cross-window (jQuery #14381)", "mixed_sort.html", deepEqual );