Commit a64dc040 authored by Colin Snover's avatar Colin Snover Committed by John Resig

Remove conditional that prevents attr from working on non-Element nodes. Fixes #7451.

parent d1a88b2e
...@@ -289,91 +289,88 @@ jQuery.extend({ ...@@ -289,91 +289,88 @@ jQuery.extend({
// Try to normalize/fix the name // Try to normalize/fix the name
name = notxml && jQuery.props[ name ] || name; name = notxml && jQuery.props[ name ] || name;
// Only do all the following if this is a node (faster for style) // These attributes require special treatment
if ( elem.nodeType === 1 ) { var special = rspecialurl.test( name );
// 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
// Safari mis-reports the default selected property of an option if ( name === "selected" && !jQuery.support.optSelected ) {
// Accessing the parent's selectedIndex property fixes it var parent = elem.parentNode;
if ( name === "selected" && !jQuery.support.optSelected ) { if ( parent ) {
var parent = elem.parentNode; parent.selectedIndex;
if ( parent ) {
parent.selectedIndex; // Make sure that it also works with optgroups, see #5701
if ( parent.parentNode ) {
// Make sure that it also works with optgroups, see #5701 parent.parentNode.selectedIndex;
if ( parent.parentNode ) {
parent.parentNode.selectedIndex;
}
} }
} }
}
// If applicable, access the attribute via the DOM 0 way // If applicable, access the attribute via the DOM 0 way
// 'in' checks fail in Blackberry 4.7 #6931 // 'in' checks fail in Blackberry 4.7 #6931
if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) { if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
if ( set ) { if ( set ) {
// We can't allow the type property to be changed (since it causes problems in IE) // 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 ) { if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
jQuery.error( "type property can't be changed" ); 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;
} }
// elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set if ( value === null ) {
// http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/ if ( elem.nodeType === 1 ) {
if ( name === "tabIndex" ) { elem.removeAttribute( name );
var attributeNode = elem.getAttributeNode( "tabIndex" ); }
return attributeNode && attributeNode.specified ? } else {
attributeNode.value : elem[ name ] = value;
rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
0 :
undefined;
} }
}
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" ) { // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
if ( set ) { // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
elem.style.cssText = "" + value; 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 ) { if ( set ) {
// convert the value to a string (all browsers do this but IE) see #1070 elem.style.cssText = "" + value;
elem.setAttribute( name, "" + value );
} }
// Ensure that missing attributes return undefined return elem.style.cssText;
// 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 ? if ( set ) {
// Some attributes require a special call on IE // convert the value to a string (all browsers do this but IE) see #1070
elem.getAttribute( name, 2 ) : elem.setAttribute( name, "" + value );
elem.getAttribute( name ); }
// Non-existent attributes return null, we normalize to undefined // Ensure that missing attributes return undefined
return attr === null ? undefined : attr; // 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;
} }
}); });
......
...@@ -4,7 +4,7 @@ var bareObj = function(value) { return value; }; ...@@ -4,7 +4,7 @@ var bareObj = function(value) { return value; };
var functionReturningObj = function(value) { return (function() { return value; }); }; var functionReturningObj = function(value) { return (function() { return value; }); };
test("attr(String)", function() { test("attr(String)", function() {
expect(30); expect(31);
// This one sometimes fails randomly ?! // This one sometimes fails randomly ?!
equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' ); equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
...@@ -65,6 +65,8 @@ test("attr(String)", function() { ...@@ -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("<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." ); 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 ) { if ( !isLocal ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment