Add fallback to prop for the window and document. Switch value to use the property instead of the attribute for back compat.

This commit is contained in:
timmywil 2011-05-10 00:27:52 -04:00
parent 8c13cfa805
commit 4526c8b0a0
2 changed files with 55 additions and 28 deletions

View file

@ -302,6 +302,11 @@ jQuery.extend({
return jQuery( elem )[ name ]( value );
}
// Fallback to prop when attributes are not supported
if ( !("getAttribute" in elem) ) {
return jQuery.prop( elem, name, value );
}
var ret, hooks,
notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
@ -381,7 +386,7 @@ jQuery.extend({
// Setting the type on a radio button after the value resets the value in IE6-9
// Reset value to it's default in case type is set after value
// This is for element creation
var val = elem.getAttribute("value");
var val = elem.value;
elem.setAttribute( "type", value );
if ( val ) {
elem.value = val;
@ -485,6 +490,24 @@ boolHook = {
}
};
// Use the value property for back compat
// Use the formHook for button elements in IE6/7 (#1954)
jQuery.attrHooks.value = {
get: function( elem, name ) {
if ( formHook && jQuery.nodeName( elem, "button" ) ) {
return formHook.get( elem, name );
}
return elem.value;
},
set: function( elem, value, name ) {
if ( formHook && jQuery.nodeName( elem, "button" ) ) {
return formHook.set( elem, value, name );
}
// Does not return so that setAttribute is also used
elem.value = value;
}
};
// IE6/7 do not support getting/setting some attributes with get/setAttribute
if ( !jQuery.support.getSetAttribute ) {
@ -492,7 +515,7 @@ if ( !jQuery.support.getSetAttribute ) {
jQuery.attrFix = jQuery.propFix;
// Use this for any attribute on a form in IE6/7
formHook = jQuery.attrHooks.name = jQuery.attrHooks.value = jQuery.valHooks.button = {
formHook = jQuery.attrHooks.name = jQuery.valHooks.button = {
get: function( elem, name ) {
var ret;
ret = elem.getAttributeNode( name );

View file

@ -144,7 +144,7 @@ test("attr(Hash)", function() {
});
test("attr(String, Object)", function() {
expect(59);
expect(66);
var div = jQuery("div").attr("foo", "bar"),
fail = false;
@ -206,9 +206,6 @@ test("attr(String, Object)", function() {
equals( document.getElementById("name").maxLength, 5, "Set maxlength attribute" );
jQuery("#name").attr("maxLength", "10");
equals( document.getElementById("name").maxLength, 10, "Set maxlength attribute" );
var $p = jQuery("#firstp").attr("nonexisting", "foo");
equals( $p.attr("nonexisting"), "foo", "attr(name, value) works correctly for non existing attributes (bug #7500).");
$p.removeAttr("nonexisting");
var $text = jQuery("#text1").attr("autofocus", true);
if ( "autofocus" in $text[0] ) {
@ -227,12 +224,19 @@ test("attr(String, Object)", function() {
var attributeNode = document.createAttribute("irrelevant"),
commentNode = document.createComment("some comment"),
textNode = document.createTextNode("some text");
textNode = document.createTextNode("some text"),
obj = {};
jQuery.each( [commentNode, textNode, attributeNode], function( i, ele ) {
var $ele = jQuery( ele );
$ele.attr( "nonexisting", "foo" );
strictEqual( $ele.attr("nonexisting"), undefined, "attr(name, value) works correctly on comment and text nodes (bug #7500)." );
jQuery.each( [commentNode, textNode, attributeNode], function( i, elem ) {
var $elem = jQuery( elem );
$elem.attr( "nonexisting", "foo" );
strictEqual( $elem.attr("nonexisting"), undefined, "attr(name, value) works correctly on comment and text nodes (bug #7500)." );
});
jQuery.each( [window, document, obj, "#firstp"], function( i, elem ) {
var $elem = jQuery( elem );
strictEqual( $elem.attr("nonexisting"), undefined, "attr works correctly for non existing attributes (bug #7500)." );
equal( $elem.attr("something", "foo" ).attr("something"), "foo", "attr falls back to prop on unsupported arguments" );
});
var table = jQuery("#table").append("<tr><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr>"),
@ -244,7 +248,7 @@ test("attr(String, Object)", function() {
table.attr("cellspacing", "2");
equals( table[0].cellSpacing, "2", "Check cellspacing is correctly set" );
equals( jQuery("#area1").attr("value"), undefined, "Value attribute retrieved correctly on textarea." );
equals( jQuery("#area1").attr("value"), "foobar", "Value attribute retrieves the property for backwards compatibility." );
// for #1070
jQuery("#name").attr("someAttr", "0");