Commit 541e7349 authored by John Hoven's avatar John Hoven Committed by Dave Methvin

Attributes: Trim whitespace from option text when returned as a value

Fixes #14858
Ref #14686
Closes gh-1531
(cherry picked from commit 9ec429cf)

Conflicts:
	src/attributes/val.js
parent e547a277
...@@ -219,4 +219,5 @@ S. Andrew Sheppard <andrew@wq.io> ...@@ -219,4 +219,5 @@ S. Andrew Sheppard <andrew@wq.io>
Roman Reiß <me@silverwind.io> Roman Reiß <me@silverwind.io>
Benjy Cui <benjytrys@gmail.com> Benjy Cui <benjytrys@gmail.com>
Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com> Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>
John Hoven <hovenj@gmail.com>
...@@ -71,6 +71,16 @@ jQuery.fn.extend({ ...@@ -71,6 +71,16 @@ jQuery.fn.extend({
jQuery.extend({ jQuery.extend({
valHooks: { valHooks: {
option: {
get: function( elem ) {
var val = jQuery.find.attr( elem, "value" );
return val != null ?
val :
// Support: IE10-11+
// option.text throws exceptions (#14686, #14858)
jQuery.trim( jQuery.text( elem ) );
}
},
select: { select: {
get: function( elem ) { get: function( elem ) {
var value, option, var value, option,
......
...@@ -1459,3 +1459,16 @@ test( "should not throw at $(option).val() (#14686)", 1, function() { ...@@ -1459,3 +1459,16 @@ test( "should not throw at $(option).val() (#14686)", 1, function() {
ok( false ); ok( false );
} }
}); });
test( "Insignificant white space returned for $(option).val() (#14858)", function() {
expect ( 3 );
var val = jQuery( "<option></option>" ).val();
equal( val.length, 0, "Empty option should have no value" );
val = jQuery( "<option> </option>" ).val();
equal( val.length, 0, "insignificant white-space returned for value" );
val = jQuery( "<option> test </option>" ).val();
equal( val.length, 4, "insignificant white-space returned for value" );
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment