]> source.dussan.org Git - jquery.git/commitdiff
Remove conditional that prevents attr from working on non-Element nodes. Fixes #7451. 75/head 79/head
authorColin Snover <github.com@zetafleet.com>
Tue, 9 Nov 2010 20:52:20 +0000 (15:52 -0500)
committerJohn Resig <jeresig@gmail.com>
Tue, 9 Nov 2010 20:52:20 +0000 (15:52 -0500)
src/attributes.js
test/unit/attributes.js

index be221afa3d687527df9698061db8e808b0fb4436..4824c29029fc5fb1f146a3f5f9f5b69c18617116 100644 (file)
@@ -289,91 +289,88 @@ jQuery.extend({
                // Try to normalize/fix the name
                name = notxml && jQuery.props[ name ] || name;
 
-               // Only do all the following if this is a node (faster for style)
-               if ( elem.nodeType === 1 ) {
-                       // These attributes require special treatment
-                       var special = rspecialurl.test( name );
-
-                       // Safari mis-reports the default selected property of an option
-                       // Accessing the parent's selectedIndex property fixes it
-                       if ( name === "selected" && !jQuery.support.optSelected ) {
-                               var parent = elem.parentNode;
-                               if ( parent ) {
-                                       parent.selectedIndex;
-       
-                                       // Make sure that it also works with optgroups, see #5701
-                                       if ( parent.parentNode ) {
-                                               parent.parentNode.selectedIndex;
-                                       }
+               // These attributes require special treatment
+               var special = rspecialurl.test( name );
+
+               // Safari mis-reports the default selected property of an option
+               // Accessing the parent's selectedIndex property fixes it
+               if ( name === "selected" && !jQuery.support.optSelected ) {
+                       var parent = elem.parentNode;
+                       if ( parent ) {
+                               parent.selectedIndex;
+
+                               // Make sure that it also works with optgroups, see #5701
+                               if ( parent.parentNode ) {
+                                       parent.parentNode.selectedIndex;
                                }
                        }
+               }
 
-                       // If applicable, access the attribute via the DOM 0 way
-                       // 'in' checks fail in Blackberry 4.7 #6931
-                       if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
-                               if ( set ) {
-                                       // We can't allow the type property to be changed (since it causes problems in IE)
-                                       if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
-                                               jQuery.error( "type property can't be changed" );
-                                       }
-
-                                       if ( value === null ) {
-                                               if ( elem.nodeType === 1 ) {
-                                                       elem.removeAttribute( name );
-                                               }
-
-                                       } else {
-                                               elem[ name ] = value;
-                                       }
-                               }
-
-                               // browsers index elements by id/name on forms, give priority to attributes.
-                               if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
-                                       return elem.getAttributeNode( name ).nodeValue;
+               // If applicable, access the attribute via the DOM 0 way
+               // 'in' checks fail in Blackberry 4.7 #6931
+               if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
+                       if ( set ) {
+                               // We can't allow the type property to be changed (since it causes problems in IE)
+                               if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
+                                       jQuery.error( "type property can't be changed" );
                                }
 
-                               // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
-                               // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
-                               if ( name === "tabIndex" ) {
-                                       var attributeNode = elem.getAttributeNode( "tabIndex" );
+                               if ( value === null ) {
+                                       if ( elem.nodeType === 1 ) {
+                                               elem.removeAttribute( name );
+                                       }
 
-                                       return attributeNode && attributeNode.specified ?
-                                               attributeNode.value :
-                                               rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
-                                                       0 :
-                                                       undefined;
+                               } else {
+                                       elem[ name ] = value;
                                }
+                       }
 
-                               return elem[ name ];
+                       // browsers index elements by id/name on forms, give priority to attributes.
+                       if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
+                               return elem.getAttributeNode( name ).nodeValue;
                        }
 
-                       if ( !jQuery.support.style && notxml && name === "style" ) {
-                               if ( set ) {
-                                       elem.style.cssText = "" + value;
-                               }
+                       // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
+                       // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
+                       if ( name === "tabIndex" ) {
+                               var attributeNode = elem.getAttributeNode( "tabIndex" );
 
-                               return elem.style.cssText;
+                               return attributeNode && attributeNode.specified ?
+                                       attributeNode.value :
+                                       rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
+                                               0 :
+                                               undefined;
                        }
 
+                       return elem[ name ];
+               }
+
+               if ( !jQuery.support.style && notxml && name === "style" ) {
                        if ( set ) {
-                               // convert the value to a string (all browsers do this but IE) see #1070
-                               elem.setAttribute( name, "" + value );
+                               elem.style.cssText = "" + value;
                        }
 
-                       // Ensure that missing attributes return undefined
-                       // Blackberry 4.7 returns "" from getAttribute #6938
-                       if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
-                               return undefined;
-                       }
+                       return elem.style.cssText;
+               }
 
-                       var attr = !jQuery.support.hrefNormalized && notxml && special ?
-                                       // Some attributes require a special call on IE
-                                       elem.getAttribute( name, 2 ) :
-                                       elem.getAttribute( name );
+               if ( set ) {
+                       // convert the value to a string (all browsers do this but IE) see #1070
+                       elem.setAttribute( name, "" + value );
+               }
 
-                       // Non-existent attributes return null, we normalize to undefined
-                       return attr === null ? undefined : attr;
+               // Ensure that missing attributes return undefined
+               // Blackberry 4.7 returns "" from getAttribute #6938
+               if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
+                       return undefined;
                }
+
+               var attr = !jQuery.support.hrefNormalized && notxml && special ?
+                               // Some attributes require a special call on IE
+                               elem.getAttribute( name, 2 ) :
+                               elem.getAttribute( name );
+
+               // Non-existent attributes return null, we normalize to undefined
+               return attr === null ? undefined : attr;
        }
 });
 
index 10f7b190f364d5072bab76a3914431141335683c..2d0a0d6cded14ecae26bd6af0623bd042a91ac08 100644 (file)
@@ -4,7 +4,7 @@ var bareObj = function(value) { return value; };
 var functionReturningObj = function(value) { return (function() { return value; }); };
 
 test("attr(String)", function() {
-       expect(30);
+       expect(31);
 
        // This one sometimes fails randomly ?!
        equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
@@ -65,6 +65,8 @@ test("attr(String)", function() {
 
        ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
        ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
+
+       equals( jQuery(document).attr("nodeName"), "#document", "attr works correctly on document nodes (bug #7451)." );
 });
 
 if ( !isLocal ) {