#8150 - When removing the width and height attributes in IE6/7, setting to "" actually sets to 0 instead of auto

- Having fixed this automatically with the use of removeAttribute in browsers that support it, this will fix it for IE6/7 as well.

- This has no effect on width/height styles set elsewhere( test added to removeAttr )

- With this addition, I need to call attr in removeAttr for IE6/7, which means boolean calls like .attr("checked", "") will no longer remove the attribute, which I think is fine.  .attr("checked", false) will still remove.  If I had left it, it would have gone in an infinite loop since setting to empty string is the only way to remove it in these browsers.

- The hrefNormalized hooks were returning null if they weren't present.  Added the null check to the getter.

- Now that the style support fails in IE8 as well due to uppercasing everything, no need to have style included with the hrefNormalized hooks
This commit is contained in:
timmywil 2011-04-09 17:25:06 -04:00
parent 17afd80d48
commit 3a1b4661f5
2 changed files with 26 additions and 17 deletions

View file

@ -338,18 +338,13 @@ jQuery.extend({
name = jQuery.attrFix[ name ] || name; name = jQuery.attrFix[ name ] || name;
if ( jQuery.support.getSetAttribute ) { if ( jQuery.support.getSetAttribute ) {
// Use removeAttribute in browsers that support it
elem.removeAttribute( name ); elem.removeAttribute( name );
} else { } else {
// Only style is a special case. jQuery.attr( elem, name, "" );
if ( name === "style" ) {
elem.style.cssText = "";
} else {
elem.setAttribute( name, "" );
// Attempt to remove completely with DOM level 1
elem.removeAttributeNode( elem.getAttributeNode( name ) ); elem.removeAttributeNode( elem.getAttributeNode( name ) );
} }
} }
}
}, },
attrHooks: { attrHooks: {
@ -448,15 +443,28 @@ if ( !jQuery.support.getSetAttribute ) {
} }
} }
}; };
// Set width and height to auto instead of 0 on empty string( Bug #8150 )
// This is for removals
jQuery.each([ "width", "height" ], function( i, name ) {
jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], {
set: function( elem, value ) {
if ( value === "" ) {
elem.setAttribute( name, "auto" );
return value;
}
}
});
});
} }
// Remove certain attrs if set to false // Remove certain attrs if set to false
jQuery.each([ "selected", "checked", "readOnly", "disabled" ], function( i, name ) { jQuery.each([ "selected", "checked", "readOnly", "disabled" ], function( i, name ) {
jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], {
set: function( elem, value ) { set: function( elem, value ) {
if ( !value ) { if ( value === false ) {
jQuery.removeAttr( elem, name ); jQuery.removeAttr( elem, name );
return false; return value;
} }
} }
}); });
@ -464,10 +472,11 @@ jQuery.each([ "selected", "checked", "readOnly", "disabled" ], function( i, name
// Some attributes require a special call on IE // Some attributes require a special call on IE
if ( !jQuery.support.hrefNormalized ) { if ( !jQuery.support.hrefNormalized ) {
jQuery.each([ "href", "src", "style", "width", "height", "list" ], function( i, name ) { jQuery.each([ "href", "src", "width", "height", "list" ], function( i, name ) {
jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], {
get: function( elem ) { get: function( elem ) {
return elem.getAttribute( name, 2 ); var ret = elem.getAttribute( name, 2 );
return ret === null ? undefined : ret;
} }
}); });
}); });

View file

@ -76,7 +76,7 @@ test("prop(String, Object)", function() {
}); });
test("attr(String)", function() { test("attr(String)", function() {
expect(31); expect(32);
equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' ); equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' ); equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
@ -90,6 +90,7 @@ test("attr(String)", function() {
equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' ); equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' ); ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
equals( jQuery('#form').attr('blah', 'blah').attr('blah'), 'blah', 'Set non-existant attribute on a form' ); equals( jQuery('#form').attr('blah', 'blah').attr('blah'), 'blah', 'Set non-existant attribute on a form' );
equals( jQuery('#foo').attr('height'), undefined, 'Non existent height attribute should return undefined' );
// [7472] & [3113] (form contains an input with name="action" or name="id") // [7472] & [3113] (form contains an input with name="action" or name="id")
var extras = jQuery('<input name="id" name="name" /><input id="target" name="target" />').appendTo('#testForm'); var extras = jQuery('<input name="id" name="name" /><input id="target" name="target" />').appendTo('#testForm');
@ -173,7 +174,7 @@ test("attr(Hash)", function() {
}); });
test("attr(String, Object)", function() { test("attr(String, Object)", function() {
expect(30); expect(29);
var div = jQuery("div").attr("foo", "bar"), var div = jQuery("div").attr("foo", "bar"),
fail = false; fail = false;
@ -196,8 +197,6 @@ test("attr(String, Object)", function() {
equals( jQuery("#name").attr('name'), undefined, 'Remove name attribute' ); equals( jQuery("#name").attr('name'), undefined, 'Remove name attribute' );
jQuery("#check2").attr('checked', true); jQuery("#check2").attr('checked', true);
equals( document.getElementById('check2').checked, true, 'Set checked attribute' ); equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
jQuery("#check2").attr('checked', '');
equals( document.getElementById('check2').checked, false, 'Setting checked to empty string removes it' );
jQuery("#check2").attr('checked', false); jQuery("#check2").attr('checked', false);
equals( document.getElementById('check2').checked, false, 'Set checked attribute' ); equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
jQuery("#text1").attr('readonly', true); jQuery("#text1").attr('readonly', true);
@ -400,11 +399,12 @@ test("attr('tabindex', value)", function() {
}); });
test("removeAttr(String)", function() { test("removeAttr(String)", function() {
expect(4); expect(5);
equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" ); equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
equals( jQuery('#form').removeAttr('id').attr('id'), undefined, 'Remove id' ); equals( jQuery('#form').removeAttr('id').attr('id'), undefined, 'Remove id' );
equals( jQuery('#foo').attr('style', 'position:absolute;').removeAttr('style').attr('style'), undefined, 'Check removing style attribute' ); equals( jQuery('#foo').attr('style', 'position:absolute;').removeAttr('style').attr('style'), undefined, 'Check removing style attribute' );
equals( jQuery('#form').attr('style', 'position:absolute;').removeAttr('style').attr('style'), undefined, 'Check removing style attribute on a form' ); equals( jQuery('#form').attr('style', 'position:absolute;').removeAttr('style').attr('style'), undefined, 'Check removing style attribute on a form' );
equals( jQuery('#fx-test-group').attr('height', '3px').removeAttr('height').css('height'), "1px", 'Removing height attribute has no effect on height set with style attribute' );
}); });
test("removeProp(String)", function() { test("removeProp(String)", function() {