Reduce the boolean list only to those that have corresponding IDLs that don't require being added to propFix; only set the IDL if it exists

- See http://jsfiddle.net/timmywil/u5NLn/ for how boolean attributes are handled in every browser.
This commit is contained in:
timmywil 2011-05-07 14:49:04 -04:00
parent 09c0cf995b
commit c085563270
2 changed files with 18 additions and 11 deletions

View file

@ -6,7 +6,7 @@ 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,
rboolean = /^(?:autofocus|autoplay|async|checked|controls|declare|defer|disabled|draggable|formnovalidate|hidden|ismap|loop|multiple|muted|noresize|noshade|nowrap|novalidate|open|pubdate|readonly|required|reversed|scoped|seamless|selected|truespeed)$/i, rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i,
rinvalidChar = /\:/, rinvalidChar = /\:/,
formHook, boolHook; formHook, boolHook;
@ -311,15 +311,15 @@ jQuery.extend({
hooks = jQuery.attrHooks[ name ]; hooks = jQuery.attrHooks[ name ];
if ( !hooks ) { if ( !hooks ) {
// Use formHook for forms and if the name contains certain characters
if ( formHook && (jQuery.nodeName( elem, "form" ) || rinvalidChar.test( name )) ) {
hooks = formHook;
// Use boolHook for boolean attributes // Use boolHook for boolean attributes
} else if ( rboolean.test( name ) && if ( rboolean.test( name ) &&
(typeof value === "boolean" || value === undefined) ) { (typeof value === "boolean" || value === undefined || value.toLowerCase() === name.toLowerCase()) ) {
hooks = boolHook; hooks = boolHook;
// Use formHook for forms and if the name contains certain characters
} else if ( formHook && (jQuery.nodeName( elem, "form" ) || rinvalidChar.test( name )) ) {
hooks = formHook;
} }
} }
@ -352,6 +352,7 @@ jQuery.extend({
}, },
removeAttr: function( elem, name ) { removeAttr: function( elem, name ) {
var propName;
if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
name = jQuery.attrFix[ name ] || name; name = jQuery.attrFix[ name ] || name;
@ -364,8 +365,8 @@ jQuery.extend({
} }
// Set corresponding property to false for boolean attributes // Set corresponding property to false for boolean attributes
if ( rboolean.test( name ) ) { if ( rboolean.test( name ) && (propName = jQuery.propFix[ name ] || name) in elem ) {
elem[ jQuery.propFix[ name ] || name ] = false; elem[ propName ] = false;
} }
} }
}, },
@ -465,13 +466,19 @@ boolHook = {
undefined; undefined;
}, },
set: function( elem, value, name ) { set: function( elem, value, name ) {
var propName;
if ( value === false ) { if ( value === false ) {
// Remove boolean attributes when set to false // Remove boolean attributes when set to false
jQuery.removeAttr( elem, name ); jQuery.removeAttr( elem, name );
} else { } else {
// value is true since we know at this point it's type boolean and not false // value is true since we know at this point it's type boolean and not false
// Set boolean attributes to the same name and set the DOM property // Set boolean attributes to the same name and set the DOM property
elem[ jQuery.propFix[ name ] || name ] = value; propName = jQuery.propFix[ name ] || name;
if ( propName in elem ) {
// Only set the IDL specifically if it already exists on the element
elem[ propName ] = value;
}
elem.setAttribute( name, name.toLowerCase() ); elem.setAttribute( name, name.toLowerCase() );
} }
return name; return name;

View file

@ -502,7 +502,7 @@ test("removeProp(String)", function() {
strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." ); strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
}); });
jQuery.each( [commentNode, textNode, attributeNode], function( i, ele ) { jQuery.each( [commentNode, textNode, attributeNode], function( i, ele ) {
$ele = jQuery( ele ); var $ele = jQuery( ele );
$ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" ); $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." ); strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
}); });