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