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

Conflicts:
	src/attributes.js
This commit is contained in:
jeresig 2011-04-10 16:56:56 -04:00
commit f89edbc6e4

View file

@ -6,7 +6,6 @@ var rclass = /[\n\t\r]/g,
rtype = /^(?:button|input)$/i, rtype = /^(?:button|input)$/i,
rfocusable = /^(?:button|input|object|select|textarea)$/i, rfocusable = /^(?:button|input|object|select|textarea)$/i,
rclickable = /^a(?:rea)?$/i, rclickable = /^a(?:rea)?$/i,
rradiocheck = /^(?:radio|checkbox)$/i,
formHook; formHook;
jQuery.fn.extend({ jQuery.fn.extend({
@ -166,19 +165,71 @@ jQuery.fn.extend({
}, },
val: function( value ) { val: function( value ) {
if ( !arguments.length ) { var hooks, ret,
var elem = this[0]; elem = this[0];
if ( !arguments.length ) {
if ( elem ) { if ( elem ) {
if ( jQuery.nodeName( elem, "option" ) ) { hooks = jQuery.valHooks[ elem.nodeName.toLowerCase() ] || jQuery.valHooks[ elem.type ];
if ( hooks && "get" in hooks && (ret = hooks.get( elem )) !== undefined ) {
return ret;
}
return (elem.value || "").replace(rreturn, "");
}
return undefined;
}
var isFunction = jQuery.isFunction( 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() );
} 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 ) {
return value == null ? "" : value + "";
});
}
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 // attributes.value is undefined in Blackberry 4.7 but
// uses .value. See #6932 // uses .value. See #6932
var val = elem.attributes.value; var val = elem.attributes.value;
return !val || val.specified ? elem.value : elem.text; return !val || val.specified ? elem.value : elem.text;
} }
},
// We need to handle select boxes special select: {
if ( jQuery.nodeName( elem, "select" ) ) { get: function( elem ) {
var index = elem.selectedIndex, var index = elem.selectedIndex,
values = [], values = [],
options = elem.options, options = elem.options,
@ -216,67 +267,23 @@ jQuery.fn.extend({
} }
return values; return values;
} },
// Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified set: function( elem, value ) {
if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) { var values = jQuery.makeArray( value );
return elem.getAttribute("value") === null ? "on" : elem.value;
}
// Everything else, we just grab the value jQuery(elem).find("option").each(function() {
return (elem.value || "").replace(rreturn, "");
}
return undefined;
}
var isFunction = jQuery.isFunction(value);
return this.each(function(i) {
var self = jQuery(this), val = value;
if ( this.nodeType !== 1 ) {
return;
}
if ( isFunction ) {
val = value.call(this, i, self.val());
}
// 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) {
return value == null ? "" : value + "";
});
}
if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
this.checked = jQuery.inArray( self.val(), val ) >= 0;
} else if ( jQuery.nodeName( this, "select" ) ) {
var values = jQuery.makeArray(val);
jQuery( "option", this ).each(function() {
this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0; this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
}); });
if ( !values.length ) { if ( !values.length ) {
this.selectedIndex = -1; elem.selectedIndex = -1;
} }
return values;
}
}
},
} else {
this.value = val;
}
});
}
});
jQuery.extend({
attrFn: { attrFn: {
val: true, val: true,
css: 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 ); })( jQuery );