mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Core: 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 Closes gh-4986
This commit is contained in:
parent
eb9ceb2fac
commit
9c6f64c7b5
@ -1,6 +1,7 @@
|
|||||||
import jQuery from "../core.js";
|
import jQuery from "../core.js";
|
||||||
import document from "../var/document.js";
|
import document from "../var/document.js";
|
||||||
import sort from "../var/sort.js";
|
import sort from "../var/sort.js";
|
||||||
|
import splice from "../var/splice.js";
|
||||||
|
|
||||||
var hasDuplicate;
|
var hasDuplicate;
|
||||||
|
|
||||||
@ -80,7 +81,7 @@ jQuery.uniqueSort = function( results ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
while ( j-- ) {
|
while ( j-- ) {
|
||||||
results.splice( duplicates[ j ], 1 );
|
splice.call( results, duplicates[ j ], 1 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
src/var/splice.js
Normal file
3
src/var/splice.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import arr from "./arr.js";
|
||||||
|
|
||||||
|
export default arr.splice;
|
@ -1895,9 +1895,7 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Arrayish.prototype = {
|
Arrayish.prototype = {
|
||||||
slice: [].slice,
|
sliceForTestOnly: [].slice
|
||||||
sort: [].sort,
|
|
||||||
splice: [].splice
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var i, tests,
|
var i, tests,
|
||||||
@ -1959,8 +1957,12 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
|
|||||||
|
|
||||||
jQuery.each( tests, function( label, test ) {
|
jQuery.each( tests, function( label, test ) {
|
||||||
var length = test.length || test.input.length;
|
var length = test.length || test.input.length;
|
||||||
assert.deepEqual( jQuery.uniqueSort( test.input ).slice( 0, length ), test.expected, label + " (array)" );
|
// We duplicate `test.input` because otherwise it is modified by `uniqueSort`
|
||||||
assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).slice( 0, length ), test.expected, label + " (quasi-array)" );
|
// and the second test becomes worthless.
|
||||||
|
assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ),
|
||||||
|
test.expected, label + " (array)" );
|
||||||
|
assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).sliceForTestOnly( 0, length ),
|
||||||
|
test.expected, label + " (quasi-array)" );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user