Project

General

Profile

1
define([
2
	"../core",
3
	"./support",
4
	"../core/init"
5
], function( jQuery, support ) {
6

    
7
var rreturn = /\r/g;
8

    
9
jQuery.fn.extend({
10
	val: function( value ) {
11
		var hooks, ret, isFunction,
12
			elem = this[0];
13

    
14
		if ( !arguments.length ) {
15
			if ( elem ) {
16
				hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ];
17

    
18
				if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) {
19
					return ret;
20
				}
21

    
22
				ret = elem.value;
23

    
24
				return typeof ret === "string" ?
25
					// Handle most common string cases
26
					ret.replace(rreturn, "") :
27
					// Handle cases where value is null/undef or number
28
					ret == null ? "" : ret;
29
			}
30

    
31
			return;
32
		}
33

    
34
		isFunction = jQuery.isFunction( value );
35

    
36
		return this.each(function( i ) {
37
			var val;
38

    
39
			if ( this.nodeType !== 1 ) {
40
				return;
41
			}
42

    
43
			if ( isFunction ) {
44
				val = value.call( this, i, jQuery( this ).val() );
45
			} else {
46
				val = value;
47
			}
48

    
49
			// Treat null/undefined as ""; convert numbers to string
50
			if ( val == null ) {
51
				val = "";
52

    
53
			} else if ( typeof val === "number" ) {
54
				val += "";
55

    
56
			} else if ( jQuery.isArray( val ) ) {
57
				val = jQuery.map( val, function( value ) {
58
					return value == null ? "" : value + "";
59
				});
60
			}
61

    
62
			hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
63

    
64
			// If set returns undefined, fall back to normal setting
65
			if ( !hooks || !("set" in hooks) || hooks.set( this, val, "value" ) === undefined ) {
66
				this.value = val;
67
			}
68
		});
69
	}
70
});
71

    
72
jQuery.extend({
73
	valHooks: {
74
		option: {
75
			get: function( elem ) {
76
				var val = jQuery.find.attr( elem, "value" );
77
				return val != null ?
78
					val :
79
					// Support: IE10-11+
80
					// option.text throws exceptions (#14686, #14858)
81
					jQuery.trim( jQuery.text( elem ) );
82
			}
83
		},
84
		select: {
85
			get: function( elem ) {
86
				var value, option,
87
					options = elem.options,
88
					index = elem.selectedIndex,
89
					one = elem.type === "select-one" || index < 0,
90
					values = one ? null : [],
91
					max = one ? index + 1 : options.length,
92
					i = index < 0 ?
93
						max :
94
						one ? index : 0;
95

    
96
				// Loop through all the selected options
97
				for ( ; i < max; i++ ) {
98
					option = options[ i ];
99

    
100
					// IE6-9 doesn't update selected after form reset (#2551)
101
					if ( ( option.selected || i === index ) &&
102
							// Don't return options that are disabled or in a disabled optgroup
103
							( support.optDisabled ? !option.disabled : option.getAttribute( "disabled" ) === null ) &&
104
							( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {
105

    
106
						// Get the specific value for the option
107
						value = jQuery( option ).val();
108

    
109
						// We don't need an array for one selects
110
						if ( one ) {
111
							return value;
112
						}
113

    
114
						// Multi-Selects return an array
115
						values.push( value );
116
					}
117
				}
118

    
119
				return values;
120
			},
121

    
122
			set: function( elem, value ) {
123
				var optionSet, option,
124
					options = elem.options,
125
					values = jQuery.makeArray( value ),
126
					i = options.length;
127

    
128
				while ( i-- ) {
129
					option = options[ i ];
130
					if ( (option.selected = jQuery.inArray( option.value, values ) >= 0) ) {
131
						optionSet = true;
132
					}
133
				}
134

    
135
				// Force browsers to behave consistently when non-matching value is set
136
				if ( !optionSet ) {
137
					elem.selectedIndex = -1;
138
				}
139
				return values;
140
			}
141
		}
142
	}
143
});
144

    
145
// Radios and checkboxes getter/setter
146
jQuery.each([ "radio", "checkbox" ], function() {
147
	jQuery.valHooks[ this ] = {
148
		set: function( elem, value ) {
149
			if ( jQuery.isArray( value ) ) {
150
				return ( elem.checked = jQuery.inArray( jQuery(elem).val(), value ) >= 0 );
151
			}
152
		}
153
	};
154
	if ( !support.checkOn ) {
155
		jQuery.valHooks[ this ].get = function( elem ) {
156
			return elem.getAttribute("value") === null ? "on" : elem.value;
157
		};
158
	}
159
});
160

    
161
});
(5-5/5)