Skip to content

Commit e7af951

Browse files
committed
Attributes: strip/collapse whitespace for set values on selects
Fixes jquerygh-2978 Close jquerygh-3002
1 parent 0ef97b5 commit e7af951

File tree

2 files changed

+88
-11
lines changed

2 files changed

+88
-11
lines changed

src/attributes/val.js

Lines changed: 12 additions & 5 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 ) {
@@ -80,9 +81,15 @@ jQuery.extend( {
8081
option: {
8182
get: function( elem ) {
8283

83-
// Support: IE<11
84-
// option.value not trimmed (#14858)
85-
return jQuery.trim( elem.value );
84+
var val = jQuery.find.attr( elem, "value" );
85+
return val != null ?
86+
val :
87+
88+
// Support: IE10-11+
89+
// option.text throws exceptions (#14686, #14858)
90+
// Strip and collapse whitespace
91+
// https://html.spec.whatwg.org/#strip-and-collapse-whitespace
92+
jQuery.trim( jQuery.text( elem ) ).replace( rspaces, " " );
8693
}
8794
},
8895
select: {
@@ -135,7 +142,7 @@ jQuery.extend( {
135142
while ( i-- ) {
136143
option = options[ i ];
137144
if ( option.selected =
138-
jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
145+
jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
139146
) {
140147
optionSet = true;
141148
}

test/unit/attributes.js

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

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

@@ -1511,17 +1576,22 @@ QUnit.test( "option value not trimmed when setting via parent select", function(
15111576
assert.equal( jQuery( "<select><option> 2</option></select>" ).val( "2" ).val(), "2" );
15121577
} );
15131578

1514-
QUnit.test( "Insignificant white space returned for $(option).val() (#14858)", function( assert ) {
1515-
assert.expect( 3 );
1579+
QUnit.test( "Insignificant white space returned for $(option).val() (#14858, gh-2978)", function( assert ) {
1580+
assert.expect( 16 );
15161581

15171582
var val = jQuery( "<option></option>" ).val();
15181583
assert.equal( val.length, 0, "Empty option should have no value" );
15191584

1520-
val = jQuery( "<option> </option>" ).val();
1521-
assert.equal( val.length, 0, "insignificant white-space returned for value" );
1585+
jQuery.each( [ " ", "\n", "\t", "\f", "\r" ], function( i, character ) {
1586+
var val = jQuery( "<option>" + character + "</option>" ).val();
1587+
assert.equal( val.length, 0, "insignificant white-space returned for value" );
1588+
1589+
val = jQuery( "<option>" + character + "test" + character + "</option>" ).val();
1590+
assert.equal( val.length, 4, "insignificant white-space returned for value" );
15221591

1523-
val = jQuery( "<option> test </option>" ).val();
1524-
assert.equal( val.length, 4, "insignificant white-space returned for value" );
1592+
val = jQuery( "<option>te" + character + "st</option>" ).val();
1593+
assert.equal( val, "te st", "Whitespace is collapsed in values" );
1594+
} );
15251595
} );
15261596

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

0 commit comments

Comments
 (0)