Skip to content

Commit 9e44c3e

Browse files
authored
Core: Warn & fill jQuery.trim
Fixes jquerygh-325 Closes jquerygh-331
1 parent 7b1ff32 commit 9e44c3e

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

src/core.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
var oldInit = jQuery.fn.init,
33
oldFind = jQuery.find,
44
rattrHashTest = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/,
5-
rattrHashGlob = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/g;
5+
rattrHashGlob = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/g,
6+
7+
// Support: Android <=4.0 only
8+
// Make sure we trim BOM and NBSP
9+
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
610

711
jQuery.fn.init = function( arg1 ) {
812
var args = Array.prototype.slice.call( arguments );
@@ -60,15 +64,15 @@ for ( findProp in oldFind ) {
6064
}
6165

6266
// The number of elements contained in the matched element set
63-
jQuery.fn.size = function() {
64-
migrateWarn( "jQuery.fn.size() is deprecated and removed; use the .length property" );
67+
migrateWarnFunc( jQuery.fn, "size", function() {
6568
return this.length;
66-
};
69+
},
70+
"jQuery.fn.size() is deprecated and removed; use the .length property" );
6771

68-
jQuery.parseJSON = function() {
69-
migrateWarn( "jQuery.parseJSON is deprecated; use JSON.parse" );
72+
migrateWarnFunc( jQuery, "parseJSON", function() {
7073
return JSON.parse.apply( null, arguments );
71-
};
74+
},
75+
"jQuery.parseJSON is deprecated; use JSON.parse" );
7276

7377
migrateWarnFunc( jQuery, "holdReady", jQuery.holdReady,
7478
"jQuery.holdReady is deprecated" );
@@ -82,6 +86,16 @@ migrateWarnProp( jQuery.expr, "filters", jQuery.expr.pseudos,
8286
migrateWarnProp( jQuery.expr, ":", jQuery.expr.pseudos,
8387
"jQuery.expr[':'] is deprecated; use jQuery.expr.pseudos" );
8488

89+
// Prior to jQuery 3.1.1 there were internal refs so we don't warn there
90+
if ( jQueryVersionSince( "3.1.1" ) ) {
91+
migrateWarnFunc( jQuery, "trim", function( text ) {
92+
return text == null ?
93+
"" :
94+
( text + "" ).replace( rtrim, "" );
95+
},
96+
"jQuery.trim is deprecated; use String.prototype.trim" );
97+
}
98+
8599
// Prior to jQuery 3.2 there were internal refs so we don't warn there
86100
if ( jQueryVersionSince( "3.2.0" ) ) {
87101
migrateWarnFunc( jQuery, "nodeName", function( elem, name ) {

test/core.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,63 @@ QUnit.test( "jQuery.holdReady (warn only)", function( assert ) {
370370
} );
371371
} );
372372

373+
QUnit[ jQueryVersionSince( "3.1.1" ) ? "test" : "skip" ]( "jQuery.trim", function( assert ) {
374+
assert.expect( 26 );
375+
376+
var nbsp = String.fromCharCode( 160 );
377+
378+
expectWarning( assert, "jQuery.trim", 1, function() {
379+
assert.equal( jQuery.trim( "hello " ), "hello", "trailing space" );
380+
} );
381+
expectWarning( assert, "jQuery.trim", 1, function() {
382+
assert.equal( jQuery.trim( " hello" ), "hello", "leading space" );
383+
} );
384+
expectWarning( assert, "jQuery.trim", 1, function() {
385+
assert.equal( jQuery.trim( " hello " ), "hello", "space on both sides" );
386+
} );
387+
expectWarning( assert, "jQuery.trim", 1, function() {
388+
assert.equal( jQuery.trim( " " + nbsp + "hello " + nbsp + " " ), "hello", "&nbsp;" );
389+
} );
390+
391+
expectWarning( assert, "jQuery.trim", 1, function() {
392+
assert.equal( jQuery.trim(), "", "Nothing in." );
393+
} );
394+
expectWarning( assert, "jQuery.trim", 1, function() {
395+
assert.equal( jQuery.trim( undefined ), "", "Undefined" );
396+
} );
397+
expectWarning( assert, "jQuery.trim", 1, function() {
398+
assert.equal( jQuery.trim( null ), "", "Null" );
399+
} );
400+
expectWarning( assert, "jQuery.trim", 1, function() {
401+
assert.equal( jQuery.trim( 5 ), "5", "Number" );
402+
} );
403+
expectWarning( assert, "jQuery.trim", 1, function() {
404+
assert.equal( jQuery.trim( false ), "false", "Boolean" );
405+
} );
406+
407+
expectWarning( assert, "jQuery.trim", 1, function() {
408+
assert.equal( jQuery.trim( " " ), "", "space should be trimmed" );
409+
} );
410+
expectWarning( assert, "jQuery.trim", 1, function() {
411+
assert.equal( jQuery.trim( "ipad\xA0" ), "ipad", "nbsp should be trimmed" );
412+
} );
413+
expectWarning( assert, "jQuery.trim", 1, function() {
414+
assert.equal( jQuery.trim( "\uFEFF" ), "", "zwsp should be trimmed" );
415+
} );
416+
expectWarning( assert, "jQuery.trim", 1, function() {
417+
assert.equal( jQuery.trim( "\uFEFF \xA0! | \uFEFF" ), "! |", "leading/trailing should be trimmed" );
418+
} );
419+
} );
420+
373421
QUnit[ jQueryVersionSince( "3.2.0" ) ? "test" : "skip" ]( "jQuery.nodeName", function( assert ) {
374422
assert.expect( 2 );
375423

376424
expectWarning( assert, "jQuery.nodeName", function() {
377425
var div = document.createElement( "div" );
378426

379427
assert.equal( jQuery.nodeName( div, "div" ), true, "it's a div" );
380-
})
381-
});
428+
} );
429+
} );
382430

383431
QUnit[ jQueryVersionSince( "3.3.0" ) ? "test" : "skip" ]( "jQuery.isFunction", function( assert ) {
384432
assert.expect( 4 );

warnings.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
243243
**Solution:** Remove any uses of `jQuery.cssProps` in application code.
244244

245245
#### JQMIGRATE: jQuery.isArray is deprecated; use Array.isArray
246+
246247
**Cause:** Older versions of JavaScript made it difficult to determine if a particular object was a true Array, so jQuery provided a cross-browser function to do the work. The browsers supported by jQuery 3.0 all provide a standard method for this purpose.
247248

248249
**Solution:** Replace any calls to `jQuery.isArray` with `Array.isArray`.
250+
251+
#### JQMIGRATE: jQuery.trim is deprecated; use String.prototype.trim
252+
253+
**Cause:** Older versions of IE & Android Browser didn't implement a method to `trim` strings so jQuery provided a cross-browser implementation. The browsers supported by jQuery 3.0 all provide a standard method for this purpose.
254+
255+
**Solution:** Replace any calls to `jQuery.trim( text )` with `text.trim()` if you know `text` is a string; otherwise, you can replace it with `text == null ? "" : text.trim()`.

0 commit comments

Comments
 (0)