ffffng/app/bower_components/jquery/src/attributes/val.js

192 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-05-13 13:25:33 +02:00
define( [
2014-05-12 20:08:19 +02:00
"../core",
2017-05-13 13:25:33 +02:00
"../core/stripAndCollapse",
2014-05-12 20:08:19 +02:00
"./support",
2017-05-13 13:25:33 +02:00
"../core/nodeName",
2019-03-29 22:00:08 +01:00
"../var/isFunction",
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
"../core/init"
2019-03-29 22:00:08 +01:00
], function( jQuery, stripAndCollapse, support, nodeName, isFunction ) {
2017-05-13 13:25:33 +02:00
"use strict";
2014-05-12 20:08:19 +02:00
var rreturn = /\r/g;
2017-05-13 13:25:33 +02:00
jQuery.fn.extend( {
2014-05-12 20:08:19 +02:00
val: function( value ) {
2019-03-29 22:00:08 +01:00
var hooks, ret, valueIsFunction,
2017-05-13 13:25:33 +02:00
elem = this[ 0 ];
2014-05-12 20:08:19 +02:00
if ( !arguments.length ) {
if ( elem ) {
2017-05-13 13:25:33 +02:00
hooks = jQuery.valHooks[ elem.type ] ||
jQuery.valHooks[ elem.nodeName.toLowerCase() ];
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
if ( hooks &&
"get" in hooks &&
( ret = hooks.get( elem, "value" ) ) !== undefined
) {
2014-05-12 20:08:19 +02:00
return ret;
}
ret = elem.value;
2017-05-13 13:25:33 +02:00
// Handle most common string cases
if ( typeof ret === "string" ) {
return ret.replace( rreturn, "" );
}
// Handle cases where value is null/undef or number
return ret == null ? "" : ret;
2014-05-12 20:08:19 +02:00
}
return;
}
2019-03-29 22:00:08 +01:00
valueIsFunction = isFunction( value );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
return this.each( function( i ) {
2014-05-12 20:08:19 +02:00
var val;
if ( this.nodeType !== 1 ) {
return;
}
2019-03-29 22:00:08 +01:00
if ( valueIsFunction ) {
2014-05-12 20:08:19 +02:00
val = value.call( this, i, jQuery( this ).val() );
} else {
val = value;
}
// Treat null/undefined as ""; convert numbers to string
if ( val == null ) {
val = "";
} else if ( typeof val === "number" ) {
val += "";
2017-05-13 13:25:33 +02:00
} else if ( Array.isArray( val ) ) {
2014-05-12 20:08:19 +02:00
val = jQuery.map( val, function( value ) {
return value == null ? "" : value + "";
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
}
hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
// If set returns undefined, fall back to normal setting
2017-05-13 13:25:33 +02:00
if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) {
2014-05-12 20:08:19 +02:00
this.value = val;
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
jQuery.extend( {
2014-05-12 20:08:19 +02:00
valHooks: {
option: {
get: function( elem ) {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
var val = jQuery.find.attr( elem, "value" );
return val != null ?
val :
2017-05-13 13:25:33 +02:00
// Support: IE <=10 - 11 only
2014-05-12 20:08:19 +02:00
// option.text throws exceptions (#14686, #14858)
2017-05-13 13:25:33 +02:00
// Strip and collapse whitespace
// https://html.spec.whatwg.org/#strip-and-collapse-whitespace
stripAndCollapse( jQuery.text( elem ) );
2014-05-12 20:08:19 +02:00
}
},
select: {
get: function( elem ) {
2017-05-13 13:25:33 +02:00
var value, option, i,
2014-05-12 20:08:19 +02:00
options = elem.options,
index = elem.selectedIndex,
2017-05-13 13:25:33 +02:00
one = elem.type === "select-one",
2014-05-12 20:08:19 +02:00
values = one ? null : [],
2017-05-13 13:25:33 +02:00
max = one ? index + 1 : options.length;
if ( index < 0 ) {
i = max;
} else {
i = one ? index : 0;
}
2014-05-12 20:08:19 +02:00
// Loop through all the selected options
for ( ; i < max; i++ ) {
option = options[ i ];
2017-05-13 13:25:33 +02:00
// Support: IE <=9 only
// IE8-9 doesn't update selected after form reset (#2551)
2014-05-12 20:08:19 +02:00
if ( ( option.selected || i === index ) &&
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// Don't return options that are disabled or in a disabled optgroup
2017-05-13 13:25:33 +02:00
!option.disabled &&
( !option.parentNode.disabled ||
!nodeName( option.parentNode, "optgroup" ) ) ) {
2014-05-12 20:08:19 +02:00
// Get the specific value for the option
value = jQuery( option ).val();
// We don't need an array for one selects
if ( one ) {
return value;
}
// Multi-Selects return an array
values.push( value );
}
}
return values;
},
set: function( elem, value ) {
var optionSet, option,
options = elem.options,
values = jQuery.makeArray( value ),
i = options.length;
while ( i-- ) {
option = options[ i ];
2017-05-13 13:25:33 +02:00
/* eslint-disable no-cond-assign */
if ( option.selected =
jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
) {
2014-05-12 20:08:19 +02:00
optionSet = true;
}
2017-05-13 13:25:33 +02:00
/* eslint-enable no-cond-assign */
2014-05-12 20:08:19 +02:00
}
2016-05-16 13:33:49 +02:00
// Force browsers to behave consistently when non-matching value is set
2014-05-12 20:08:19 +02:00
if ( !optionSet ) {
elem.selectedIndex = -1;
}
return values;
}
}
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
// Radios and checkboxes getter/setter
2017-05-13 13:25:33 +02:00
jQuery.each( [ "radio", "checkbox" ], function() {
2014-05-12 20:08:19 +02:00
jQuery.valHooks[ this ] = {
set: function( elem, value ) {
2017-05-13 13:25:33 +02:00
if ( Array.isArray( value ) ) {
return ( elem.checked = jQuery.inArray( jQuery( elem ).val(), value ) > -1 );
2014-05-12 20:08:19 +02:00
}
}
};
if ( !support.checkOn ) {
jQuery.valHooks[ this ].get = function( elem ) {
2017-05-13 13:25:33 +02:00
return elem.getAttribute( "value" ) === null ? "on" : elem.value;
2014-05-12 20:08:19 +02:00
};
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
} );