From 70526981916945dc4093e116a3de61b1777d4718 Mon Sep 17 00:00:00 2001 From: Timmy Willison Date: Tue, 15 Mar 2016 18:15:02 -0400 Subject: [PATCH] Attributes: strip/collapse whitespace for set values on selects Fixes gh-2978 Close gh-3002 --- src/attributes/val.js | 17 ++++++--- test/unit/attributes.js | 82 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/attributes/val.js b/src/attributes/val.js index 5f0b73e00..a8a2e12fb 100644 --- a/src/attributes/val.js +++ b/src/attributes/val.js @@ -4,7 +4,8 @@ define( [ "../core/init" ], function( jQuery, support ) { -var rreturn = /\r/g; +var rreturn = /\r/g, + rspaces = /[\x20\t\r\n\f]+/g; jQuery.fn.extend( { val: function( value ) { @@ -80,9 +81,15 @@ jQuery.extend( { option: { get: function( elem ) { - // Support: IE<11 - // option.value not trimmed (#14858) - return jQuery.trim( elem.value ); + var val = jQuery.find.attr( elem, "value" ); + return val != null ? + val : + + // Support: IE10-11+ + // option.text throws exceptions (#14686, #14858) + // Strip and collapse whitespace + // https://html.spec.whatwg.org/#strip-and-collapse-whitespace + jQuery.trim( jQuery.text( elem ) ).replace( rspaces, " " ); } }, select: { @@ -134,7 +141,7 @@ jQuery.extend( { while ( i-- ) { option = options[ i ]; if ( option.selected = - jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1 + jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1 ) { optionSet = true; } diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 23c13a7cd..d9a036476 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -1107,6 +1107,71 @@ QUnit.test( "val(select) after form.reset() (Bug #2551)", function( assert ) { jQuery( "#kk" ).remove(); } ); +QUnit.test( "select.val(space characters) (gh-2978)", function( assert ) { + assert.expect( 35 ); + + var $select = jQuery( "" ).val( "2" ).val(), "2" ); } ); -QUnit.test( "Insignificant white space returned for $(option).val() (#14858)", function( assert ) { - assert.expect( 3 ); +QUnit.test( "Insignificant white space returned for $(option).val() (#14858, gh-2978)", function( assert ) { + assert.expect( 16 ); var val = jQuery( "" ).val(); assert.equal( val.length, 0, "Empty option should have no value" ); - val = jQuery( "" ).val(); - assert.equal( val.length, 0, "insignificant white-space returned for value" ); + jQuery.each( [ " ", "\n", "\t", "\f", "\r" ], function( i, character ) { + var val = jQuery( "" ).val(); + assert.equal( val.length, 0, "insignificant white-space returned for value" ); + + val = jQuery( "" ).val(); + assert.equal( val.length, 4, "insignificant white-space returned for value" ); - val = jQuery( "" ).val(); - assert.equal( val.length, 4, "insignificant white-space returned for value" ); + val = jQuery( "" ).val(); + assert.equal( val, "te st", "Whitespace is collapsed in values" ); + } ); } ); QUnit.test( "SVG class manipulation (gh-2199)", function( assert ) { -- 2.39.5