Debian Codegraph @

vendor/static/jquery/src/selector/uniqueSort.js

raw
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";
6
7 var hasDuplicate;
8
9 // Document order sorting
10 function sortOrder( a, b ) {
11
12 // Flag for duplicate removal
13 if ( a === b ) {
14 hasDuplicate = true;
15 return 0;
16 }
17
18 // Sort on method existence if only one input has compareDocumentPosition
19 var compare = !a.compareDocumentPosition - !b.compareDocumentPosition;
20 if ( compare ) {
21 return compare;
22 }
23
24 // Calculate position if both inputs belong to the same document
25 // Support: IE 11+
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 ) :
31
32 // Otherwise we know they are disconnected
33 1;
34
35 // Disconnected nodes
36 if ( compare & 1 ) {
37
38 // Choose the first element that is related to the document
39 // Support: IE 11+
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 ) ) {
45 return -1;
46 }
47
48 // Support: IE 11+
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 ) ) {
54 return 1;
55 }
56
57 // Maintain original order
58 return 0;
59 }
60
61 return compare & 4 ? -1 : 1;
62 }
63
64 /**
65 * Document sorting and removing duplicates
66 * @param {ArrayLike} results
67 */
68 jQuery.uniqueSort = function( results ) {
69 var elem,
70 duplicates = [],
71 j = 0,
72 i = 0;
73
74 hasDuplicate = false;
75
76 sort.call( results, sortOrder );
77
78 if ( hasDuplicate ) {
79 while ( ( elem = results[ i++ ] ) ) {
80 if ( elem === results[ i ] ) {
81 j = duplicates.push( i );
82 }
83 }
84 while ( j-- ) {
85 splice.call( results, duplicates[ j ], 1 );
86 }
87 }
88
89 return results;
90 };
91
92 jQuery.fn.uniqueSort = function() {
93 return this.pushStack( jQuery.uniqueSort( slice.apply( this ) ) );
94 };