Fix issue where non-existant attributes on forms in IE6/7 were throwing errors

This commit is contained in:
timmywil 2011-03-25 21:13:25 -04:00
parent 3892df207d
commit e0900a686d
2 changed files with 12 additions and 9 deletions

View File

@ -313,8 +313,9 @@ jQuery.extend({
} else { } else {
// Check form objects in IE (multiple bugs related) // Check form objects in IE (multiple bugs related)
if ( isFormObjects ) { // Only use nodeValue if the attribute node exists on the form
elem.getAttributeNode( name ).nodeValue = value; if ( isFormObjects && (ret = elem.getAttributeNode( name )) ) {
ret.nodeValue = value;
} else { } else {
elem.setAttribute( name, value ); elem.setAttribute( name, value );
} }
@ -330,8 +331,9 @@ jQuery.extend({
// Check form objects in IE (multiple bugs related) // Check form objects in IE (multiple bugs related)
if ( isFormObjects ) { if ( isFormObjects ) {
// Return undefined for empty string, which is the blank nodeValue in IE // Return undefined if not specified instead of empty string
ret = elem.getAttributeNode( name ).nodeValue || undefined; ret = elem.getAttributeNode( name );
return ret && ret.specified ? ret.nodeValue : undefined;
} else { } else {
ret = elem.getAttribute( name ); ret = elem.getAttribute( name );
} }

View File

@ -75,7 +75,7 @@ test("prop(String, Object)", function() {
}); });
test("attr(String)", function() { test("attr(String)", function() {
expect(25); expect(26);
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' );
@ -88,6 +88,7 @@ test("attr(String)", function() {
equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' ); equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
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' );
// [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');
@ -214,7 +215,7 @@ test("attr(String, Object)", function() {
td.attr("colspan", "2"); td.attr("colspan", "2");
equals( td[0].colSpan, 2, "Check colspan is correctly set" ); equals( td[0].colSpan, 2, "Check colspan is correctly set" );
table.attr("cellspacing", "2"); table.attr("cellspacing", "2");
equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" ); equals( table[0].cellSpacing, "2", "Check cellspacing is correctly set" );
// for #1070 // for #1070
jQuery("#name").attr('someAttr', '0'); jQuery("#name").attr('someAttr', '0');
@ -385,9 +386,9 @@ test("attr('tabindex', value)", function() {
}); });
test("removeAttr(String)", function() { test("removeAttr(String)", function() {
expect(1); expect(2);
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' );
}); });
test("removeProp(String)", function() { test("removeProp(String)", function() {