Skip to content

Commit 3bbcce6

Browse files
committed
Core: rnotwhite -> rhtmlnotwhite and jQuery.trim -> stripAndCollapse
- Renames and changes rnotwhite to focus on HTML whitespace chars - Change internal use of jQuery.trim to more accurate strip and collapse - Adds tests to ensure HTML space characters are retained where valid - Doesn't add tests where the difference is inconsequential and existing tests are adequate. Fixes gh-3003 Fixes gh-3072 Close gh-3316
1 parent 2d4f534 commit 3bbcce6

File tree

15 files changed

+121
-51
lines changed

15 files changed

+121
-51
lines changed

src/ajax.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
define( [
22
"./core",
33
"./var/document",
4-
"./var/rnotwhite",
4+
"./var/rnothtmlwhite",
55
"./ajax/var/location",
66
"./ajax/var/nonce",
77
"./ajax/var/rquery",
@@ -11,7 +11,7 @@ define( [
1111
"./event/trigger",
1212
"./deferred",
1313
"./serialize" // jQuery.param
14-
], function( jQuery, document, rnotwhite, location, nonce, rquery ) {
14+
], function( jQuery, document, rnothtmlwhite, location, nonce, rquery ) {
1515

1616
"use strict";
1717

@@ -64,7 +64,7 @@ function addToPrefiltersOrTransports( structure ) {
6464

6565
var dataType,
6666
i = 0,
67-
dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || [];
67+
dataTypes = dataTypeExpression.toLowerCase().match( rnothtmlwhite ) || [];
6868

6969
if ( jQuery.isFunction( func ) ) {
7070

@@ -532,7 +532,7 @@ jQuery.extend( {
532532
s.type = options.method || options.type || s.method || s.type;
533533

534534
// Extract dataTypes list
535-
s.dataTypes = ( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
535+
s.dataTypes = ( s.dataType || "*" ).toLowerCase().match( rnothtmlwhite ) || [ "" ];
536536

537537
// A cross-domain request is in order when the origin doesn't match the current origin.
538538
if ( s.crossDomain == null ) {

src/ajax/load.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
define( [
22
"../core",
3+
"../core/stripAndCollapse",
34
"../core/parseHTML",
45
"../ajax",
56
"../traversing",
67
"../manipulation",
78
"../selector"
8-
], function( jQuery ) {
9+
], function( jQuery, stripAndCollapse ) {
910

1011
"use strict";
1112

@@ -18,7 +19,7 @@ jQuery.fn.load = function( url, params, callback ) {
1819
off = url.indexOf( " " );
1920

2021
if ( off > -1 ) {
21-
selector = jQuery.trim( url.slice( off ) );
22+
selector = stripAndCollapse( url.slice( off ) );
2223
url = url.slice( 0, off );
2324
}
2425

src/attributes/attr.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ define( [
22
"../core",
33
"../core/access",
44
"./support",
5-
"../var/rnotwhite",
5+
"../var/rnothtmlwhite",
66
"../selector"
7-
], function( jQuery, access, support, rnotwhite ) {
7+
], function( jQuery, access, support, rnothtmlwhite ) {
88

99
"use strict";
1010

@@ -89,7 +89,10 @@ jQuery.extend( {
8989
removeAttr: function( elem, value ) {
9090
var name,
9191
i = 0,
92-
attrNames = value && value.match( rnotwhite );
92+
93+
// Attribute names can contain non-HTML whitespace characters
94+
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
95+
attrNames = value && value.match( rnothtmlwhite );
9396

9497
if ( attrNames && elem.nodeType === 1 ) {
9598
while ( ( name = attrNames[ i++ ] ) ) {

src/attributes/classes.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
define( [
22
"../core",
3-
"../var/rnotwhite",
3+
"../core/stripAndCollapse",
4+
"../var/rnothtmlwhite",
45
"../data/var/dataPriv",
56
"../core/init"
6-
], function( jQuery, rnotwhite, dataPriv ) {
7+
], function( jQuery, stripAndCollapse, rnothtmlwhite, dataPriv ) {
78

89
"use strict";
910

10-
var rclass = /[\t\r\n\f]/g;
11-
1211
function getClass( elem ) {
1312
return elem.getAttribute && elem.getAttribute( "class" ) || "";
1413
}
@@ -25,12 +24,11 @@ jQuery.fn.extend( {
2524
}
2625

2726
if ( typeof value === "string" && value ) {
28-
classes = value.match( rnotwhite ) || [];
27+
classes = value.match( rnothtmlwhite ) || [];
2928

3029
while ( ( elem = this[ i++ ] ) ) {
3130
curValue = getClass( elem );
32-
cur = elem.nodeType === 1 &&
33-
( " " + curValue + " " ).replace( rclass, " " );
31+
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
3432

3533
if ( cur ) {
3634
j = 0;
@@ -41,7 +39,7 @@ jQuery.fn.extend( {
4139
}
4240

4341
// Only assign if different to avoid unneeded rendering.
44-
finalValue = jQuery.trim( cur );
42+
finalValue = stripAndCollapse( cur );
4543
if ( curValue !== finalValue ) {
4644
elem.setAttribute( "class", finalValue );
4745
}
@@ -67,14 +65,13 @@ jQuery.fn.extend( {
6765
}
6866

6967
if ( typeof value === "string" && value ) {
70-
classes = value.match( rnotwhite ) || [];
68+
classes = value.match( rnothtmlwhite ) || [];
7169

7270
while ( ( elem = this[ i++ ] ) ) {
7371
curValue = getClass( elem );
7472

7573
// This expression is here for better compressibility (see addClass)
76-
cur = elem.nodeType === 1 &&
77-
( " " + curValue + " " ).replace( rclass, " " );
74+
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
7875

7976
if ( cur ) {
8077
j = 0;
@@ -87,7 +84,7 @@ jQuery.fn.extend( {
8784
}
8885

8986
// Only assign if different to avoid unneeded rendering.
90-
finalValue = jQuery.trim( cur );
87+
finalValue = stripAndCollapse( cur );
9188
if ( curValue !== finalValue ) {
9289
elem.setAttribute( "class", finalValue );
9390
}
@@ -122,7 +119,7 @@ jQuery.fn.extend( {
122119
// Toggle individual class names
123120
i = 0;
124121
self = jQuery( this );
125-
classNames = value.match( rnotwhite ) || [];
122+
classNames = value.match( rnothtmlwhite ) || [];
126123

127124
while ( ( className = classNames[ i++ ] ) ) {
128125

@@ -165,10 +162,8 @@ jQuery.fn.extend( {
165162
className = " " + selector + " ";
166163
while ( ( elem = this[ i++ ] ) ) {
167164
if ( elem.nodeType === 1 &&
168-
( " " + getClass( elem ) + " " ).replace( rclass, " " )
169-
.indexOf( className ) > -1
170-
) {
171-
return true;
165+
( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {
166+
return true;
172167
}
173168
}
174169

src/attributes/val.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
define( [
22
"../core",
3+
"../core/stripAndCollapse",
34
"./support",
45
"../core/init"
5-
], function( jQuery, support ) {
6+
], function( jQuery, stripAndCollapse, support ) {
67

78
"use strict";
89

9-
var rreturn = /\r/g,
10-
rspaces = /[\x20\t\r\n\f]+/g;
10+
var rreturn = /\r/g;
1111

1212
jQuery.fn.extend( {
1313
val: function( value ) {
@@ -91,7 +91,7 @@ jQuery.extend( {
9191
// option.text throws exceptions (#14686, #14858)
9292
// Strip and collapse whitespace
9393
// https://html.spec.whatwg.org/#strip-and-collapse-whitespace
94-
jQuery.trim( jQuery.text( elem ) ).replace( rspaces, " " );
94+
stripAndCollapse( jQuery.text( elem ) );
9595
}
9696
},
9797
select: {

src/callbacks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
define( [
22
"./core",
3-
"./var/rnotwhite"
4-
], function( jQuery, rnotwhite ) {
3+
"./var/rnothtmlwhite"
4+
], function( jQuery, rnothtmlwhite ) {
55

66
"use strict";
77

88
// Convert String-formatted options into Object-formatted ones
99
function createOptions( options ) {
1010
var object = {};
11-
jQuery.each( options.match( rnotwhite ) || [], function( _, flag ) {
11+
jQuery.each( options.match( rnothtmlwhite ) || [], function( _, flag ) {
1212
object[ flag ] = true;
1313
} );
1414
return object;

src/core/stripAndCollapse.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
define( function() {
2+
"use strict";
3+
4+
// Strip and collapse whitespace according to HTML spec
5+
// https://html.spec.whatwg.org/multipage/infrastructure.html#strip-and-collapse-whitespace
6+
var rhtmlSpace = /[\x20\t\r\n\f]+/g,
7+
stripAndCollapse = function( value ) {
8+
return ( " " + value + " " ).replace( rhtmlSpace, " " ).slice( 1, -1 );
9+
};
10+
11+
return stripAndCollapse;
12+
} );

src/data/Data.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
define( [
22
"../core",
3-
"../var/rnotwhite",
3+
"../var/rnothtmlwhite",
44
"./var/acceptData"
5-
], function( jQuery, rnotwhite, acceptData ) {
5+
], function( jQuery, rnothtmlwhite, acceptData ) {
66

77
"use strict";
88

@@ -127,7 +127,7 @@ Data.prototype = {
127127
// Otherwise, create an array by matching non-whitespace
128128
key = key in cache ?
129129
[ key ] :
130-
( key.match( rnotwhite ) || [] );
130+
( key.match( rnothtmlwhite ) || [] );
131131
}
132132

133133
i = key.length;

src/effects.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ define( [
22
"./core",
33
"./var/document",
44
"./var/rcssNum",
5-
"./var/rnotwhite",
5+
"./var/rnothtmlwhite",
66
"./css/var/cssExpand",
77
"./css/var/isHiddenWithinTree",
88
"./css/var/swap",
@@ -17,7 +17,7 @@ define( [
1717
"./manipulation",
1818
"./css",
1919
"./effects/Tween"
20-
], function( jQuery, document, rcssNum, rnotwhite, cssExpand, isHiddenWithinTree, swap,
20+
], function( jQuery, document, rcssNum, rnothtmlwhite, cssExpand, isHiddenWithinTree, swap,
2121
adjustCSS, dataPriv, showHide ) {
2222

2323
"use strict";
@@ -415,7 +415,7 @@ jQuery.Animation = jQuery.extend( Animation, {
415415
callback = props;
416416
props = [ "*" ];
417417
} else {
418-
props = props.match( rnotwhite );
418+
props = props.match( rnothtmlwhite );
419419
}
420420

421421
var prop,

src/event.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ define( [
22
"./core",
33
"./var/document",
44
"./var/documentElement",
5-
"./var/rnotwhite",
5+
"./var/rnothtmlwhite",
66
"./var/slice",
77
"./data/var/dataPriv",
88

99
"./core/init",
1010
"./selector"
11-
], function( jQuery, document, documentElement, rnotwhite, slice, dataPriv ) {
11+
], function( jQuery, document, documentElement, rnothtmlwhite, slice, dataPriv ) {
1212

1313
"use strict";
1414

@@ -147,7 +147,7 @@ jQuery.event = {
147147
}
148148

149149
// Handle multiple events separated by a space
150-
types = ( types || "" ).match( rnotwhite ) || [ "" ];
150+
types = ( types || "" ).match( rnothtmlwhite ) || [ "" ];
151151
t = types.length;
152152
while ( t-- ) {
153153
tmp = rtypenamespace.exec( types[ t ] ) || [];
@@ -229,7 +229,7 @@ jQuery.event = {
229229
}
230230

231231
// Once for each type.namespace in types; type may be omitted
232-
types = ( types || "" ).match( rnotwhite ) || [ "" ];
232+
types = ( types || "" ).match( rnothtmlwhite ) || [ "" ];
233233
t = types.length;
234234
while ( t-- ) {
235235
tmp = rtypenamespace.exec( types[ t ] ) || [];

0 commit comments

Comments
 (0)