Skip to content
Permalink
Browse files

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

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

Conflicts:
	src/attributes/val.js
  • Loading branch information
johnhoven authored and dmethvin committed Mar 6, 2014
1 parent e547a27 commit 541e7349b6533eb533c15d17e3e9e432e4a719ea
Showing with 24 additions and 0 deletions.
  1. +1 −0 AUTHORS.txt
  2. +10 −0 src/attributes/val.js
  3. +13 −0 test/unit/attributes.js
@@ -219,4 +219,5 @@ S. Andrew Sheppard <andrew@wq.io>
Roman Reiß <me@silverwind.io>
Benjy Cui <benjytrys@gmail.com>
Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>
John Hoven <hovenj@gmail.com>

@@ -71,6 +71,16 @@ jQuery.fn.extend({

jQuery.extend({
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)

This comment has been minimized.

Copy link
@jdalton

jdalton Apr 8, 2014

Member

Instead of this could you check element.firstChild ? element.text : '' that would avoid the poisoned property and the extra calls for trim/text. Maybe closer to the original.

This comment has been minimized.

Copy link
@dmethvin

dmethvin Apr 10, 2014

Member

Seems like that might work, I'm meh on it because I doubt this is perf-critical code and it will probably be just a few bytes shorter. If you want to do a pull request with a tested fix though, it would be great!

This comment has been minimized.

Copy link
@jdalton

jdalton Apr 10, 2014

Member

If you want to do a pull request with a tested fix though, it would be great!

Ok. No worries, I'll see what I can do.

jQuery.trim( jQuery.text( elem ) );
}
},
select: {
get: function( elem ) {
var value, option,
@@ -1459,3 +1459,16 @@ test( "should not throw at $(option).val() (#14686)", 1, function() {
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" );
});

0 comments on commit 541e734

Please sign in to comment.
You can’t perform that action at this time.