🌐 AI搜索 & 代理 主页
Skip to content

Commit 9ac9a76

Browse files
committed
Selector: Don't rely on splice being present on input
Without this fix calling `jQuery.uniqueSort` on an array-like can result in: TypeError: results.splice is not a function at Function.jQuery.uniqueSort (https://code.jquery.com/jquery-git.js:664:12) at jQuery.fn.init.find (https://code.jquery.com/jquery-git.js:2394:27) at gocusihafe.js:3:4 Ref jquerygh-4986
1 parent 1495f99 commit 9ac9a76

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/selector.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ define( [
77
"./var/pop",
88
"./var/push",
99
"./var/sort",
10+
"./var/splice",
1011
"./var/whitespace",
1112
"./var/rtrimCSS",
1213
"./var/support",
@@ -15,7 +16,7 @@ define( [
1516
"./selector/contains",
1617
"./selector/escapeSelector"
1718
], function( jQuery, nodeName, document, indexOf, hasOwn, pop, push, sort,
18-
whitespace, rtrimCSS, support ) {
19+
splice, whitespace, rtrimCSS, support ) {
1920

2021
"use strict";
2122

@@ -785,7 +786,7 @@ jQuery.uniqueSort = function( results ) {
785786
}
786787
}
787788
while ( j-- ) {
788-
results.splice( duplicates[ j ], 1 );
789+
splice.call( results, duplicates[ j ], 1 );
789790
}
790791
}
791792

src/var/splice.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define( [
2+
"./arr"
3+
], function( arr ) {
4+
5+
"use strict";
6+
7+
return arr.splice;
8+
9+
} );

test/unit/selector.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ QUnit.test( "jQuery.contains in SVG (jQuery trac-10832)", function( assert ) {
18871887
} );
18881888

18891889
QUnit.test( "jQuery.uniqueSort", function( assert ) {
1890-
assert.expect( 15 );
1890+
assert.expect( 14 );
18911891

18921892
function Arrayish( arr ) {
18931893
var i = this.length = arr.length;
@@ -1896,9 +1896,7 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
18961896
}
18971897
}
18981898
Arrayish.prototype = {
1899-
slice: [].slice,
1900-
sort: [].sort,
1901-
splice: [].splice
1899+
sliceForTestOnly: [].slice
19021900
};
19031901

19041902
var i, tests,
@@ -1960,11 +1958,13 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
19601958

19611959
jQuery.each( tests, function( label, test ) {
19621960
var length = test.length || test.input.length;
1963-
assert.deepEqual( jQuery.uniqueSort( test.input ).slice( 0, length ), test.expected, label + " (array)" );
1964-
assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).slice( 0, length ), test.expected, label + " (quasi-array)" );
1961+
// We duplicate `test.input` because otherwise it is modified by `uniqueSort`
1962+
// and the second test becomes worthless.
1963+
assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ),
1964+
test.expected, label + " (array)" );
1965+
assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).sliceForTestOnly( 0, length ),
1966+
test.expected, label + " (quasi-array)" );
19651967
} );
1966-
1967-
assert.strictEqual( jQuery.unique, jQuery.uniqueSort, "jQuery.unique() is an alias for jQuery.uniqueSort()" );
19681968
} );
19691969

19701970
testIframe(

0 commit comments

Comments
 (0)