Skip to content

Commit da015d9

Browse files
committed
Attributes: strip/collapse whitespace for set values on selects
Fixes jquerygh-2978 Close jquerygh-3002
1 parent 88b91af commit da015d9

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

src/attributes/val.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ define( [
44
"../core/init"
55
], function( jQuery, support ) {
66

7-
var rreturn = /\r/g;
7+
var rreturn = /\r/g,
8+
rspaces = /[\x20\t\r\n\f]+/g;
89

910
jQuery.fn.extend( {
1011
val: function( value ) {
@@ -84,7 +85,9 @@ jQuery.extend( {
8485

8586
// Support: IE10-11+
8687
// option.text throws exceptions (#14686, #14858)
87-
jQuery.trim( jQuery.text( elem ) );
88+
// Strip and collapse whitespace
89+
// https://html.spec.whatwg.org/#strip-and-collapse-whitespace
90+
jQuery.trim( jQuery.text( elem ) ).replace( rspaces, " " );
8891
}
8992
},
9093
select: {
@@ -138,7 +141,7 @@ jQuery.extend( {
138141
while ( i-- ) {
139142
option = options[ i ];
140143

141-
if ( jQuery.inArray( jQuery.valHooks.option.get( option ), values ) >= 0 ) {
144+
if ( jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1 ) {
142145

143146
// Support: IE6
144147
// When new option element is added to select box we need to

test/unit/attributes.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,71 @@ QUnit.test( "val(select) after form.reset() (Bug #2551)", function( assert ) {
10981098
jQuery( "#kk" ).remove();
10991099
} );
11001100

1101+
QUnit.test( "select.val(space characters) (gh-2978)", function( assert ) {
1102+
assert.expect( 35 );
1103+
1104+
var $select = jQuery( "<select/>" ).appendTo( "#qunit-fixture" ),
1105+
spaces = {
1106+
"\\t": {
1107+
html: "&#09;",
1108+
val: "\t"
1109+
},
1110+
"\\n": {
1111+
html: "&#10;",
1112+
val: "\n"
1113+
},
1114+
"\\r": {
1115+
html: "&#13;",
1116+
val: "\r"
1117+
},
1118+
"\\f": "\f",
1119+
"space": " ",
1120+
"\\u00a0": "\u00a0",
1121+
"\\u1680": "\u1680"
1122+
},
1123+
html = "";
1124+
jQuery.each( spaces, function( key, obj ) {
1125+
var value = obj.html || obj;
1126+
html += "<option value='attr" + value + "'></option>";
1127+
html += "<option value='at" + value + "tr'></option>";
1128+
html += "<option value='" + value + "attr'></option>";
1129+
} );
1130+
$select.html( html );
1131+
1132+
jQuery.each( spaces, function( key, obj ) {
1133+
var val = obj.val || obj;
1134+
$select.val( "attr" + val );
1135+
assert.equal( $select.val(), "attr" + val, "Value ending with space character (" + key + ") selected (attr)" );
1136+
1137+
$select.val( "at" + val + "tr" );
1138+
assert.equal( $select.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle selected (attr)" );
1139+
1140+
$select.val( val + "attr" );
1141+
assert.equal( $select.val(), val + "attr", "Value starting with space character (" + key + ") selected (attr)" );
1142+
} );
1143+
1144+
jQuery.each( spaces, function( key, obj ) {
1145+
var value = obj.html || obj,
1146+
val = obj.val || obj;
1147+
html = "";
1148+
html += "<option>text" + value + "</option>";
1149+
html += "<option>te" + value + "xt</option>";
1150+
html += "<option>" + value + "text</option>";
1151+
$select.html( html );
1152+
1153+
$select.val( "text" );
1154+
assert.equal( $select.val(), "text", "Value with space character at beginning or end is stripped (" + key + ") selected (text)" );
1155+
1156+
if ( /^\\u/.test( key ) ) {
1157+
$select.val( "te" + val + "xt" );
1158+
assert.equal( $select.val(), "te" + val + "xt", "Value with non-space whitespace character (" + key + ") in the middle selected (text)" );
1159+
} else {
1160+
$select.val( "te xt" );
1161+
assert.equal( $select.val(), "te xt", "Value with space character (" + key + ") in the middle selected (text)" );
1162+
}
1163+
} );
1164+
} );
1165+
11011166
var testAddClass = function( valueObj, assert ) {
11021167
assert.expect( 9 );
11031168

@@ -1515,17 +1580,22 @@ QUnit.test( "option value not trimmed when setting via parent select", function(
15151580
assert.equal( jQuery( "<select><option> 2</option></select>" ).val( "2" ).val(), "2" );
15161581
} );
15171582

1518-
QUnit.test( "Insignificant white space returned for $(option).val() (#14858)", function( assert ) {
1519-
assert.expect( 3 );
1583+
QUnit.test( "Insignificant white space returned for $(option).val() (#14858, gh-2978)", function( assert ) {
1584+
assert.expect( 16 );
15201585

15211586
var val = jQuery( "<option></option>" ).val();
15221587
assert.equal( val.length, 0, "Empty option should have no value" );
15231588

1524-
val = jQuery( "<option> </option>" ).val();
1525-
assert.equal( val.length, 0, "insignificant white-space returned for value" );
1589+
jQuery.each( [ " ", "\n", "\t", "\f", "\r" ], function( i, character ) {
1590+
var val = jQuery( "<option>" + character + "</option>" ).val();
1591+
assert.equal( val.length, 0, "insignificant white-space returned for value" );
1592+
1593+
val = jQuery( "<option>" + character + "test" + character + "</option>" ).val();
1594+
assert.equal( val.length, 4, "insignificant white-space returned for value" );
15261595

1527-
val = jQuery( "<option> test </option>" ).val();
1528-
assert.equal( val.length, 4, "insignificant white-space returned for value" );
1596+
val = jQuery( "<option>te" + character + "st</option>" ).val();
1597+
assert.equal( val, "te st", "Whitespace is collapsed in values" );
1598+
} );
15291599
} );
15301600

15311601
QUnit.test( "SVG class manipulation (gh-2199)", function( assert ) {

0 commit comments

Comments
 (0)