]> source.dussan.org Git - jquery.git/commitdiff
Core: Don't rely on splice being present on input
authorBruno PIERRE <brunopierre4@yahoo.fr>
Mon, 24 Jan 2022 17:55:16 +0000 (18:55 +0100)
committerGitHub <noreply@github.com>
Mon, 24 Jan 2022 17:55:16 +0000 (18:55 +0100)
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

src/selector/uniqueSort.js
src/var/splice.js [new file with mode: 0644]
test/unit/selector.js

index 127cc70683fc62ce96fb8f15176e8f64fe4bb3ff..e9438485c4cfc4b22423ffd9f8b4fef2caca8248 100644 (file)
@@ -1,6 +1,7 @@
 import jQuery from "../core.js";
 import document from "../var/document.js";
 import sort from "../var/sort.js";
+import splice from "../var/splice.js";
 
 var hasDuplicate;
 
@@ -80,7 +81,7 @@ jQuery.uniqueSort = function( results ) {
                        }
                }
                while ( j-- ) {
-                       results.splice( duplicates[ j ], 1 );
+                       splice.call( results, duplicates[ j ], 1 );
                }
        }
 
diff --git a/src/var/splice.js b/src/var/splice.js
new file mode 100644 (file)
index 0000000..7b3661c
--- /dev/null
@@ -0,0 +1,3 @@
+import arr from "./arr.js";
+
+export default arr.splice;
index 6cf288c19b8eb17de0b3819d37fc5dd4cc141768..2b0c251cf64943adff2825fc9699512af5b7c083 100644 (file)
@@ -1895,9 +1895,7 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
                }
        }
        Arrayish.prototype = {
-               slice: [].slice,
-               sort: [].sort,
-               splice: [].splice
+               sliceForTestOnly: [].slice
        };
 
        var i, tests,
@@ -1959,8 +1957,12 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
 
        jQuery.each( tests, function( label, test ) {
                var length = test.length || test.input.length;
-               assert.deepEqual( jQuery.uniqueSort( test.input ).slice( 0, length ), test.expected, label + " (array)" );
-               assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).slice( 0, length ), test.expected, label + " (quasi-array)" );
+               // We duplicate `test.input` because otherwise it is modified by `uniqueSort`
+               // 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)" );
        } );
 } );