mirror of
https://github.com/jquery/jquery.git
synced 2025-01-10 18:24:24 +00:00
Attributes: Refactor val(): don't strip carriage return, isolate IE workarounds
Before this change, `val()` was stripping out carriage return characters from the returned value. No test has relied on that. The logic was different for option elements as its custom defined hook was omitting this stripping logic. This commit gets rid of the carriage return removal and isolates the IE-only select val getter to be skipped in other browsers. Closes gh-4585
This commit is contained in:
parent
eb35be528f
commit
ff2819911d
@ -1,11 +1,10 @@
|
|||||||
import jQuery from "../core.js";
|
import jQuery from "../core.js";
|
||||||
|
import isIE from "../var/isIE.js";
|
||||||
import stripAndCollapse from "../core/stripAndCollapse.js";
|
import stripAndCollapse from "../core/stripAndCollapse.js";
|
||||||
import nodeName from "../core/nodeName.js";
|
import nodeName from "../core/nodeName.js";
|
||||||
|
|
||||||
import "../core/init.js";
|
import "../core/init.js";
|
||||||
|
|
||||||
var rreturn = /\r/g;
|
|
||||||
|
|
||||||
jQuery.fn.extend( {
|
jQuery.fn.extend( {
|
||||||
val: function( value ) {
|
val: function( value ) {
|
||||||
var hooks, ret, valueIsFunction,
|
var hooks, ret, valueIsFunction,
|
||||||
@ -25,11 +24,6 @@ jQuery.fn.extend( {
|
|||||||
|
|
||||||
ret = elem.value;
|
ret = elem.value;
|
||||||
|
|
||||||
// Handle most common string cases
|
|
||||||
if ( typeof ret === "string" ) {
|
|
||||||
return ret.replace( rreturn, "" );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle cases where value is null/undef or number
|
// Handle cases where value is null/undef or number
|
||||||
return ret == null ? "" : ret;
|
return ret == null ? "" : ret;
|
||||||
}
|
}
|
||||||
@ -77,20 +71,6 @@ jQuery.fn.extend( {
|
|||||||
|
|
||||||
jQuery.extend( {
|
jQuery.extend( {
|
||||||
valHooks: {
|
valHooks: {
|
||||||
option: {
|
|
||||||
get: function( elem ) {
|
|
||||||
|
|
||||||
var val = elem.getAttribute( "value" );
|
|
||||||
return val != null ?
|
|
||||||
val :
|
|
||||||
|
|
||||||
// Support: IE <=10 - 11+
|
|
||||||
// option.text throws exceptions (#14686, #14858)
|
|
||||||
// Strip and collapse whitespace
|
|
||||||
// https://html.spec.whatwg.org/#strip-and-collapse-whitespace
|
|
||||||
stripAndCollapse( jQuery.text( elem ) );
|
|
||||||
}
|
|
||||||
},
|
|
||||||
select: {
|
select: {
|
||||||
get: function( elem ) {
|
get: function( elem ) {
|
||||||
var value, option, i,
|
var value, option, i,
|
||||||
@ -144,7 +124,7 @@ jQuery.extend( {
|
|||||||
option = options[ i ];
|
option = options[ i ];
|
||||||
|
|
||||||
if ( ( option.selected =
|
if ( ( option.selected =
|
||||||
jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
|
jQuery.inArray( jQuery( option ).val(), values ) > -1
|
||||||
) ) {
|
) ) {
|
||||||
optionSet = true;
|
optionSet = true;
|
||||||
}
|
}
|
||||||
@ -160,6 +140,23 @@ jQuery.extend( {
|
|||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
if ( isIE ) {
|
||||||
|
jQuery.valHooks.option = {
|
||||||
|
get: function( elem ) {
|
||||||
|
|
||||||
|
var val = elem.getAttribute( "value" );
|
||||||
|
return val != null ?
|
||||||
|
val :
|
||||||
|
|
||||||
|
// Support: IE <=10 - 11+
|
||||||
|
// option.text throws exceptions (#14686, #14858)
|
||||||
|
// Strip and collapse whitespace
|
||||||
|
// https://html.spec.whatwg.org/#strip-and-collapse-whitespace
|
||||||
|
stripAndCollapse( jQuery.text( elem ) );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Radios and checkboxes getter/setter
|
// Radios and checkboxes getter/setter
|
||||||
jQuery.each( [ "radio", "checkbox" ], function() {
|
jQuery.each( [ "radio", "checkbox" ], function() {
|
||||||
jQuery.valHooks[ this ] = {
|
jQuery.valHooks[ this ] = {
|
||||||
|
@ -1196,6 +1196,57 @@ QUnit.test( "select.val(space characters) (gh-2978)", function( assert ) {
|
|||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
QUnit.test( "radio.val(space characters)", function( assert ) {
|
||||||
|
assert.expect( 42 );
|
||||||
|
|
||||||
|
var radio = jQuery( "<input type='radio'/>" ).appendTo( "#qunit-fixture" ),
|
||||||
|
spaces = {
|
||||||
|
"\\t": {
|
||||||
|
html: "	",
|
||||||
|
val: "\t"
|
||||||
|
},
|
||||||
|
"\\n": {
|
||||||
|
html: " ",
|
||||||
|
val: "\n"
|
||||||
|
},
|
||||||
|
"\\r": {
|
||||||
|
html: " ",
|
||||||
|
val: "\r"
|
||||||
|
},
|
||||||
|
"\\f": "\f",
|
||||||
|
"space": " ",
|
||||||
|
"\\u00a0": "\u00a0",
|
||||||
|
"\\u1680": "\u1680"
|
||||||
|
};
|
||||||
|
|
||||||
|
jQuery.each( spaces, function( key, obj ) {
|
||||||
|
var val = obj.val || obj;
|
||||||
|
|
||||||
|
radio.val( "attr" + val );
|
||||||
|
assert.equal( radio.val(), "attr" + val, "Value ending with space character (" + key + ") returned (set via val())" );
|
||||||
|
|
||||||
|
radio.val( "at" + val + "tr" );
|
||||||
|
assert.equal( radio.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle returned (set via val())" );
|
||||||
|
|
||||||
|
radio.val( val + "attr" );
|
||||||
|
assert.equal( radio.val(), val + "attr", "Value starting with space character (" + key + ") returned (set via val())" );
|
||||||
|
} );
|
||||||
|
|
||||||
|
jQuery.each( spaces, function( key, obj ) {
|
||||||
|
var val = obj.val || obj,
|
||||||
|
htmlVal = obj.html || obj;
|
||||||
|
|
||||||
|
radio = jQuery( "<input type='radio' value='attr" + htmlVal + "'/>" ).appendTo( "#qunit-fixture" );
|
||||||
|
assert.equal( radio.val(), "attr" + val, "Value ending with space character (" + key + ") returned (set via HTML)" );
|
||||||
|
|
||||||
|
radio = jQuery( "<input type='radio' value='at" + htmlVal + "tr'/>" ).appendTo( "#qunit-fixture" );
|
||||||
|
assert.equal( radio.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle returned (set via HTML)" );
|
||||||
|
|
||||||
|
radio = jQuery( "<input type='radio' value='" + htmlVal + "attr'/>" ).appendTo( "#qunit-fixture" );
|
||||||
|
assert.equal( radio.val(), val + "attr", "Value starting with space character (" + key + ") returned (set via HTML)" );
|
||||||
|
} );
|
||||||
|
} );
|
||||||
|
|
||||||
var testAddClass = function( valueObj, assert ) {
|
var testAddClass = function( valueObj, assert ) {
|
||||||
assert.expect( 9 );
|
assert.expect( 9 );
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user