Skip to content

Commit c1c9926

Browse files
committed
Core: Patch push, sort & splice in jQuery >=3.7.0
Ref jquery/jquery#5653
1 parent ced329a commit c1c9926

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/jquery/core.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { jQueryVersionSince } from "../compareVersions.js";
22
import { migratePatchAndWarnFunc } from "../main.js";
33
import "../disablePatches.js";
44

5-
var class2type = {},
5+
var arr = [],
6+
push = arr.push,
7+
sort = arr.sort,
8+
splice = arr.splice,
9+
class2type = {},
610

711
// Require that the "whitespace run" starts from a non-whitespace
812
// to avoid O(N^2) behavior when the engine would try matching "\s+$" at each space position.
@@ -118,3 +122,12 @@ if ( jQueryVersionSince( "3.3.0" ) ) {
118122
"proxy", "DEPRECATED: jQuery.proxy()" );
119123

120124
}
125+
126+
if ( jQueryVersionSince( "3.7.0" ) ) {
127+
migratePatchAndWarnFunc( jQuery.fn, "push", push, "push",
128+
"jQuery.fn.push() is deprecated; use .add() or convert to an array" );
129+
migratePatchAndWarnFunc( jQuery.fn, "sort", sort, "sort",
130+
"jQuery.fn.sort() is deprecated; convert to an array before sorting" );
131+
migratePatchAndWarnFunc( jQuery.fn, "splice", splice, "splice",
132+
"jQuery.fn.splice() is deprecated; use .slice() or .not() with .eq()" );
133+
}

test/unit/jquery/core.js

+57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
QUnit.module( "core" );
33

4+
function getTagNames( elem ) {
5+
return elem.toArray().map( function( node ) {
6+
return node.tagName.toLowerCase();
7+
} );
8+
}
9+
410
QUnit.test( "jQuery(html, props)", function( assert ) {
511
assert.expect( 2 );
612

@@ -318,6 +324,57 @@ TestManager.runIframeTest( "old pre-3.0 jQuery", "core-jquery2.html",
318324
assert.ok( /jQuery 3/.test( log ), "logged: " + log );
319325
} );
320326

327+
QUnit[ jQueryVersionSince( "3.7.0" ) ? "test" : "skip" ]( "jQuery.fn.push", function( assert ) {
328+
assert.expect( 2 );
329+
330+
expectWarning( assert, "jQuery.fn.push", 1, function() {
331+
var node = jQuery( "<div></div>" )[ 0 ],
332+
elem = jQuery( "<p></p><span></span>" );
333+
334+
elem.push( node );
335+
336+
assert.deepEqual( getTagNames( elem ), [ "p", "span", "div" ],
337+
"div added in-place" );
338+
} );
339+
} );
340+
341+
QUnit[ jQueryVersionSince( "3.7.0" ) ? "test" : "skip" ]( "jQuery.fn.sort", function( assert ) {
342+
assert.expect( 2 );
343+
344+
expectWarning( assert, "jQuery.fn.sort", 1, function() {
345+
var elem = jQuery( "<span></span><div></div><p></p>" );
346+
347+
elem.sort( function( node1, node2 ) {
348+
var tag1 = node1.tagName.toLowerCase(),
349+
tag2 = node2.tagName.toLowerCase();
350+
if ( tag1 < tag2 ) {
351+
return -1;
352+
}
353+
if ( tag1 > tag2 ) {
354+
return 1;
355+
}
356+
return 0;
357+
} );
358+
359+
assert.deepEqual( getTagNames( elem ), [ "div", "p", "span" ],
360+
"element sorted in-place" );
361+
} );
362+
} );
363+
364+
QUnit[ jQueryVersionSince( "3.7.0" ) ? "test" : "skip" ]( "jQuery.fn.splice", function( assert ) {
365+
assert.expect( 2 );
366+
367+
expectWarning( assert, "jQuery.fn.splice", 1, function() {
368+
var elem = jQuery( "<span></span><div></div><p></p>" );
369+
370+
elem.splice( 1, 1, jQuery( "<i></i>" )[ 0 ], jQuery( "<b></b>" )[ 0 ] );
371+
372+
assert.deepEqual( getTagNames( elem ), [ "span", "i", "b", "p" ],
373+
"splice removed & added in-place" );
374+
} );
375+
} );
376+
377+
321378
QUnit[ jQueryVersionSince( "3.3.0" ) ? "test" : "skip" ]( "jQuery.proxy", function( assert ) {
322379
assert.expect( 10 );
323380

warnings.md

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ This is _not_ a warning, but a console log message the plugin shows when it firs
4646

4747
**Solution:** Put a [valid doctype](http://www.w3.org/QA/2002/04/valid-dtd-list.html) in the document and ensure that the document is rendering in standards mode. The simplest valid doctype is the HTML5 one, which we highly recommend: `<!doctype html>` . The jQuery Migrate plugin does not attempt to fix issues related to quirks mode.
4848

49+
### \[push\] JQMIGRATE: jQuery.fn.push() is deprecated; use .add or convert to an array
50+
### \[sort\] JQMIGRATE: jQuery.fn.sort() is deprecated; convert to an array before sorting
51+
### \[splice\] JQMIGRATE: jQuery.fn.splice() is deprecated; use .slice() or .not() with .eq()
52+
53+
**Cause**: jQuery adds the Array `push`, `sort` & `splice` methods to the jQuery prototype. They behave differently to other jQuery APIs - they modify the jQuery collections in place, they don't play nice with APIs like `.end()`, they were also never documented.
54+
55+
**Solution**: Replace `.push( node )` with `.add( node )`, `.splice( index )` with `.not( elem.eq( index ) )`. In more complex cases, call `.toArray()` first, manipulate the resulting array and convert back to the jQuery object by passing the resulting array to `$()`.
56+
4957
### \[jqXHR-methods\] JQMIGRATE: jQXHR.success is deprecated and removed
5058
### \[jqXHR-methods\] JQMIGRATE: jQXHR.error is deprecated and removed
5159
### \[jqXHR-methods\] JQMIGRATE: jQXHR.complete is deprecated and removed

0 commit comments

Comments
 (0)