1 import { jQuery } from "../core.js";
2 import { document } from "../var/document.js";
3 import { sort } from "../var/sort.js";
4 import { splice } from "../var/splice.js";
5 import { slice } from "../var/slice.js";
9 // Document order sorting
10 function sortOrder( a, b ) {
12 // Flag for duplicate removal
18 // Sort on method existence if only one input has compareDocumentPosition
19 var compare = !a.compareDocumentPosition - !b.compareDocumentPosition;
24 // Calculate position if both inputs belong to the same document
26 // IE sometimes throws a "Permission denied" error when strict-comparing
27 // two documents; shallow comparisons work.
28 // eslint-disable-next-line eqeqeq
29 compare = ( a.ownerDocument || a ) == ( b.ownerDocument || b ) ?
30 a.compareDocumentPosition( b ) :
32 // Otherwise we know they are disconnected
38 // Choose the first element that is related to the document
40 // IE sometimes throws a "Permission denied" error when strict-comparing
41 // two documents; shallow comparisons work.
42 // eslint-disable-next-line eqeqeq
43 if ( a == document || a.ownerDocument == document &&
44 jQuery.contains( document, a ) ) {
49 // IE sometimes throws a "Permission denied" error when strict-comparing
50 // two documents; shallow comparisons work.
51 // eslint-disable-next-line eqeqeq
52 if ( b == document || b.ownerDocument == document &&
53 jQuery.contains( document, b ) ) {
57 // Maintain original order
61 return compare & 4 ? -1 : 1;
65 * Document sorting and removing duplicates
66 * @param {ArrayLike} results
68 jQuery.uniqueSort = function( results ) {
76 sort.call( results, sortOrder );
79 while ( ( elem = results[ i++ ] ) ) {
80 if ( elem === results[ i ] ) {
81 j = duplicates.push( i );
85 splice.call( results, duplicates[ j ], 1 );
92 jQuery.fn.uniqueSort = function() {
93 return this.pushStack( jQuery.uniqueSort( slice.apply( this ) ) );