Skip to content
Permalink
Browse files

Merge branch 'modest_val_proposal' of https://github.com/timmywil/jquery

 into timmywil-modest_val_proposal

Conflicts:
	src/attributes.js
  • Loading branch information...
jeresig committed Apr 10, 2011
2 parents 14ecd9a + d47c0ae commit f89edbc6e4bd79aa1455e7b3a2e4767ce4371027
Showing with 102 additions and 74 deletions.
  1. +102 −74 src/attributes.js
@@ -6,7 +6,6 @@ var rclass = /[\n\t\r]/g,
rtype = /^(?:button|input)$/i,
rfocusable = /^(?:button|input|object|select|textarea)$/i,
rclickable = /^a(?:rea)?$/i,
rradiocheck = /^(?:radio|checkbox)$/i,
formHook;

jQuery.fn.extend({
@@ -166,117 +165,125 @@ jQuery.fn.extend({
},

val: function( value ) {
var hooks, ret,
elem = this[0];

if ( !arguments.length ) {
var elem = this[0];

if ( elem ) {
if ( jQuery.nodeName( elem, "option" ) ) {
// attributes.value is undefined in Blackberry 4.7 but
// uses .value. See #6932
var val = elem.attributes.value;
return !val || val.specified ? elem.value : elem.text;
}

// We need to handle select boxes special
if ( jQuery.nodeName( elem, "select" ) ) {
var index = elem.selectedIndex,
values = [],
options = elem.options,
one = elem.type === "select-one";

// Nothing was selected
if ( index < 0 ) {
return null;
}

// Loop through all the selected options
for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
var option = options[ i ];

// Don't return options that are disabled or in a disabled optgroup
if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
(!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {

// Get the specific value for the option
value = jQuery(option).val();
hooks = jQuery.valHooks[ elem.nodeName.toLowerCase() ] || jQuery.valHooks[ elem.type ];

// We don't need an array for one selects
if ( one ) {
return value;
}

// Multi-Selects return an array
values.push( value );
}
}

// Fixes Bug #2551 -- select.val() broken in IE after form.reset()
if ( one && !values.length && options.length ) {
return jQuery( options[ index ] ).val();
}

return values;
if ( hooks && "get" in hooks && (ret = hooks.get( elem )) !== undefined ) {
return ret;
}

// Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
return elem.getAttribute("value") === null ? "on" : elem.value;
}

// Everything else, we just grab the value
return (elem.value || "").replace(rreturn, "");

}

return undefined;
}

var isFunction = jQuery.isFunction(value);
var isFunction = jQuery.isFunction( value );

return this.each(function(i) {
var self = jQuery(this), val = value;
return this.each(function( i ) {
var self = jQuery(this), val;

if ( this.nodeType !== 1 ) {
return;
}

if ( isFunction ) {
val = value.call(this, i, self.val());
val = value.call( this, i, self.val() );
} else {
val = value;
}

// Treat null/undefined as ""; convert numbers to string
if ( val == null ) {
val = "";
} else if ( typeof val === "number" ) {
val += "";
} else if ( jQuery.isArray(val) ) {
val = jQuery.map(val, function (value) {
} else if ( jQuery.isArray( val ) ) {
val = jQuery.map(val, function ( value ) {
return value == null ? "" : value + "";
});
}

if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
this.checked = jQuery.inArray( self.val(), val ) >= 0;
hooks = jQuery.valHooks[ this.nodeName.toLowerCase() ] || jQuery.valHooks[ this.type ];

// If set returns undefined, fall back to normal setting
if ( !hooks || ("set" in hooks && hooks.set( this, val ) === undefined) ) {
this.value = val;
}
});
}
});

jQuery.extend({
valHooks: {
option: {
get: function( elem ) {
// attributes.value is undefined in Blackberry 4.7 but
// uses .value. See #6932
var val = elem.attributes.value;
return !val || val.specified ? elem.value : elem.text;
}
},
select: {
get: function( elem ) {
var index = elem.selectedIndex,
values = [],
options = elem.options,
one = elem.type === "select-one";

// Nothing was selected
if ( index < 0 ) {
return null;
}

// Loop through all the selected options
for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
var option = options[ i ];

// Don't return options that are disabled or in a disabled optgroup
if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
(!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {

} else if ( jQuery.nodeName( this, "select" ) ) {
var values = jQuery.makeArray(val);
// Get the specific value for the option
value = jQuery( option ).val();

// We don't need an array for one selects
if ( one ) {
return value;
}

jQuery( "option", this ).each(function() {
// Multi-Selects return an array
values.push( value );
}
}

// Fixes Bug #2551 -- select.val() broken in IE after form.reset()
if ( one && !values.length && options.length ) {
return jQuery( options[ index ] ).val();
}

return values;
},

set: function( elem, value ) {
var values = jQuery.makeArray( value );

jQuery(elem).find("option").each(function() {
this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
});

if ( !values.length ) {
this.selectedIndex = -1;
elem.selectedIndex = -1;
}

} else {
this.value = val;
return values;
}
});
}
});
}
},

jQuery.extend({
attrFn: {
val: true,
css: true,
@@ -528,4 +535,25 @@ if ( !jQuery.support.optSelected ) {
});
}

// Radios and checkboxes getter/setter
if ( !jQuery.support.checkOn ) {
jQuery.each([ "radio", "checkbox" ], function() {
jQuery.valHooks[ this ] = {
get: function( elem ) {
// Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
return elem.getAttribute("value") === null ? "on" : elem.value;
}
};
});
}
jQuery.each([ "radio", "checkbox" ], function() {
jQuery.valHooks[ this ] = jQuery.extend( jQuery.valHooks[ this ], {
set: function( elem, value ) {
if ( jQuery.isArray( value ) ) {
return (elem.checked = jQuery.inArray( jQuery(elem).val(), value ) >= 0);
}
}
});
});

})( jQuery );

0 comments on commit f89edbc

Please sign in to comment.
You can’t perform that action at this time.