Blame view
app/bower_components/jquery/src/attributes/val.js
4.09 KB
87c93a029
|
1 |
define( [ |
f986e111b
|
2 |
"../core", |
87c93a029
|
3 |
"../core/stripAndCollapse", |
f986e111b
|
4 5 |
"./support", "../core/init" |
87c93a029
|
6 7 8 |
], function( jQuery, stripAndCollapse, support ) { "use strict"; |
f986e111b
|
9 10 |
var rreturn = /\r/g; |
87c93a029
|
11 |
jQuery.fn.extend( { |
f986e111b
|
12 13 |
val: function( value ) { var hooks, ret, isFunction, |
87c93a029
|
14 |
elem = this[ 0 ]; |
f986e111b
|
15 16 17 |
if ( !arguments.length ) { if ( elem ) { |
87c93a029
|
18 19 |
hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ]; |
f986e111b
|
20 |
|
87c93a029
|
21 22 23 24 |
if ( hooks && "get" in hooks && ( ret = hooks.get( elem, "value" ) ) !== undefined ) { |
f986e111b
|
25 26 27 28 |
return ret; } ret = elem.value; |
87c93a029
|
29 30 31 32 33 34 35 |
// 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; |
f986e111b
|
36 37 38 39 40 41 |
} return; } isFunction = jQuery.isFunction( value ); |
87c93a029
|
42 |
return this.each( function( i ) { |
f986e111b
|
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
var val; if ( this.nodeType !== 1 ) { return; } if ( isFunction ) { val = value.call( this, i, jQuery( this ).val() ); } else { val = value; } // Treat null/undefined as ""; convert numbers to string if ( val == null ) { val = ""; |
87c93a029
|
58 |
|
f986e111b
|
59 60 |
} else if ( typeof val === "number" ) { val += ""; |
87c93a029
|
61 |
|
f986e111b
|
62 63 64 |
} else if ( jQuery.isArray( val ) ) { val = jQuery.map( val, function( value ) { return value == null ? "" : value + ""; |
87c93a029
|
65 |
} ); |
f986e111b
|
66 67 68 69 70 |
} hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ]; // If set returns undefined, fall back to normal setting |
87c93a029
|
71 |
if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) { |
f986e111b
|
72 73 |
this.value = val; } |
87c93a029
|
74 |
} ); |
f986e111b
|
75 |
} |
87c93a029
|
76 |
} ); |
f986e111b
|
77 |
|
87c93a029
|
78 |
jQuery.extend( { |
f986e111b
|
79 80 81 |
valHooks: { option: { get: function( elem ) { |
87c93a029
|
82 |
|
f986e111b
|
83 84 85 |
var val = jQuery.find.attr( elem, "value" ); return val != null ? val : |
87c93a029
|
86 87 |
// Support: IE <=10 - 11 only |
f986e111b
|
88 |
// option.text throws exceptions (#14686, #14858) |
87c93a029
|
89 90 91 |
// Strip and collapse whitespace // https://html.spec.whatwg.org/#strip-and-collapse-whitespace stripAndCollapse( jQuery.text( elem ) ); |
f986e111b
|
92 93 94 95 |
} }, select: { get: function( elem ) { |
87c93a029
|
96 |
var value, option, i, |
f986e111b
|
97 98 |
options = elem.options, index = elem.selectedIndex, |
87c93a029
|
99 |
one = elem.type === "select-one", |
f986e111b
|
100 |
values = one ? null : [], |
87c93a029
|
101 102 103 104 105 106 107 108 |
max = one ? index + 1 : options.length; if ( index < 0 ) { i = max; } else { i = one ? index : 0; } |
f986e111b
|
109 110 111 112 |
// Loop through all the selected options for ( ; i < max; i++ ) { option = options[ i ]; |
87c93a029
|
113 114 |
// Support: IE <=9 only // IE8-9 doesn't update selected after form reset (#2551) |
f986e111b
|
115 |
if ( ( option.selected || i === index ) && |
87c93a029
|
116 |
|
f986e111b
|
117 |
// Don't return options that are disabled or in a disabled optgroup |
87c93a029
|
118 119 120 |
!option.disabled && ( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) { |
f986e111b
|
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
// 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 ]; |
87c93a029
|
146 |
/* eslint-disable no-cond-assign */ |
f986e111b
|
147 |
|
87c93a029
|
148 149 150 151 |
if ( option.selected = jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1 ) { optionSet = true; |
f986e111b
|
152 |
} |
87c93a029
|
153 154 |
/* eslint-enable no-cond-assign */ |
f986e111b
|
155 156 157 158 159 160 |
} // Force browsers to behave consistently when non-matching value is set if ( !optionSet ) { elem.selectedIndex = -1; } |
87c93a029
|
161 |
return values; |
f986e111b
|
162 163 164 |
} } } |
87c93a029
|
165 |
} ); |
f986e111b
|
166 167 |
// Radios and checkboxes getter/setter |
87c93a029
|
168 |
jQuery.each( [ "radio", "checkbox" ], function() { |
f986e111b
|
169 170 171 |
jQuery.valHooks[ this ] = { set: function( elem, value ) { if ( jQuery.isArray( value ) ) { |
87c93a029
|
172 |
return ( elem.checked = jQuery.inArray( jQuery( elem ).val(), value ) > -1 ); |
f986e111b
|
173 174 175 176 177 |
} } }; if ( !support.checkOn ) { jQuery.valHooks[ this ].get = function( elem ) { |
87c93a029
|
178 |
return elem.getAttribute( "value" ) === null ? "on" : elem.value; |
f986e111b
|
179 180 |
}; } |
87c93a029
|
181 |
} ); |
f986e111b
|
182 |
|
87c93a029
|
183 |
} ); |