Modularize special form code for IE6/7 and clean up attr again

This commit is contained in:
timmywil 2011-03-25 22:55:11 -04:00
parent e0900a686d
commit 479b28fb6a

View file

@ -6,7 +6,8 @@ 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; rradiocheck = /^(?:radio|checkbox)$/i,
formHook;
jQuery.fn.extend({ jQuery.fn.extend({
attr: function( name, value ) { attr: function( name, value ) {
@ -294,31 +295,25 @@ jQuery.extend({
var ret, hooks, var ret, hooks,
notxml = nType !== 1 || !jQuery.isXMLDoc( elem ), notxml = nType !== 1 || !jQuery.isXMLDoc( elem ),
isFormObjects = !jQuery.support.getSetAttribute && elem.nodeName === "FORM"; isFormObjects = !jQuery.support.getSetAttribute && ( name === "name" || elem.nodeName === "FORM" );
// Normalize the name if needed // Normalize the name if needed
name = notxml && jQuery.attrFix[ name ] || name; name = notxml && jQuery.attrFix[ name ] || name;
hooks = jQuery.attrHooks[ name ]; // Get the appropriate hook, or the formHook if getSetAttribute is not supported and we have form objects in IE6/7
hooks = isFormObjects && formHook ? formHook( name ) : jQuery.attrHooks[ name ];
if ( value !== undefined ) { if ( value !== undefined ) {
if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value )) !== undefined ) { if ( value === null ) {
return ret;
} else if ( value === null ) {
jQuery.removeAttr( elem, name ); jQuery.removeAttr( elem, name );
return undefined; return undefined;
} else { } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value )) !== undefined ) {
return ret;
// Check form objects in IE (multiple bugs related) } else {
// Only use nodeValue if the attribute node exists on the form elem.setAttribute( name, value );
if ( isFormObjects && (ret = elem.getAttributeNode( name )) ) {
ret.nodeValue = value;
} else {
elem.setAttribute( name, value );
}
return value; return value;
} }
@ -329,14 +324,7 @@ jQuery.extend({
} else { } else {
// Check form objects in IE (multiple bugs related) ret = elem.getAttribute( name );
if ( isFormObjects ) {
// Return undefined if not specified instead of empty string
ret = elem.getAttributeNode( name );
return ret && ret.specified ? ret.nodeValue : undefined;
} else {
ret = elem.getAttribute( name );
}
// Non-existent attributes return null, we normalize to undefined // Non-existent attributes return null, we normalize to undefined
// Instead of checking for null, we check for typeof object to catch inputs in IE6/7. Bug #7472 // Instead of checking for null, we check for typeof object to catch inputs in IE6/7. Bug #7472
@ -353,8 +341,9 @@ jQuery.extend({
if ( jQuery.support.getSetAttribute ) { if ( jQuery.support.getSetAttribute ) {
elem.removeAttribute( name ); elem.removeAttribute( name );
} else { } else {
// use DOM level 1 if getSetAttribute not supported (IE6-7) // Set to default empty string
elem.setAttribute( name, "" ); // Set to default empty string elem.setAttribute( name, "" );
// Attempt to remove completely with DOM level 1
elem.removeAttributeNode( elem.getAttributeNode( name ) ); elem.removeAttributeNode( elem.getAttributeNode( name ) );
} }
}, },
@ -436,12 +425,27 @@ if ( !jQuery.support.getSetAttribute ) {
frameborder: "frameBorder" frameborder: "frameBorder"
}); });
// Name attribute will not get removed in browsers that do not support getSetAttribute // Use this for any attribute on a form in IE6/7
// Return undefined on empty string or null // And the name attribute
jQuery.attrHooks.name = { formHook = function( name ) {
get: function( elem, value ) { return jQuery.attrHooks[ name ] || {
return elem.getAttributeNode("name").nodeValue || undefined; get: function( elem ) {
} var ret = elem.getAttributeNode( name );
// Return undefined if not specified instead of empty string
return ret && ret.specified ? ret.nodeValue : undefined;
},
set: function( elem, value ) {
// Check form objects in IE (multiple bugs related)
// Only use nodeValue if the attribute node exists on the form
var ret = elem.getAttributeNode( name );
if ( ret ) {
ret.nodeValue = value;
} else {
elem.setAttribute( name, value );
}
return value;
}
};
}; };
} }