Skip to content

Commit a64dc04

Browse files
csnoverjeresig
authored andcommitted
Remove conditional that prevents attr from working on non-Element nodes. Fixes #7451.
1 parent d1a88b2 commit a64dc04

File tree

2 files changed

+66
-67
lines changed

2 files changed

+66
-67
lines changed

src/attributes.js

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -289,91 +289,88 @@ jQuery.extend({
289289
// Try to normalize/fix the name
290290
name = notxml && jQuery.props[ name ] || name;
291291

292-
// Only do all the following if this is a node (faster for style)
293-
if ( elem.nodeType === 1 ) {
294-
// These attributes require special treatment
295-
var special = rspecialurl.test( name );
296-
297-
// Safari mis-reports the default selected property of an option
298-
// Accessing the parent's selectedIndex property fixes it
299-
if ( name === "selected" && !jQuery.support.optSelected ) {
300-
var parent = elem.parentNode;
301-
if ( parent ) {
302-
parent.selectedIndex;
303-
304-
// Make sure that it also works with optgroups, see #5701
305-
if ( parent.parentNode ) {
306-
parent.parentNode.selectedIndex;
307-
}
292+
// These attributes require special treatment
293+
var special = rspecialurl.test( name );
294+
295+
// Safari mis-reports the default selected property of an option
296+
// Accessing the parent's selectedIndex property fixes it
297+
if ( name === "selected" && !jQuery.support.optSelected ) {
298+
var parent = elem.parentNode;
299+
if ( parent ) {
300+
parent.selectedIndex;
301+
302+
// Make sure that it also works with optgroups, see #5701
303+
if ( parent.parentNode ) {
304+
parent.parentNode.selectedIndex;
308305
}
309306
}
307+
}
310308

311-
// If applicable, access the attribute via the DOM 0 way
312-
// 'in' checks fail in Blackberry 4.7 #6931
313-
if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
314-
if ( set ) {
315-
// We can't allow the type property to be changed (since it causes problems in IE)
316-
if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
317-
jQuery.error( "type property can't be changed" );
318-
}
319-
320-
if ( value === null ) {
321-
if ( elem.nodeType === 1 ) {
322-
elem.removeAttribute( name );
323-
}
324-
325-
} else {
326-
elem[ name ] = value;
327-
}
328-
}
329-
330-
// browsers index elements by id/name on forms, give priority to attributes.
331-
if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
332-
return elem.getAttributeNode( name ).nodeValue;
309+
// If applicable, access the attribute via the DOM 0 way
310+
// 'in' checks fail in Blackberry 4.7 #6931
311+
if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
312+
if ( set ) {
313+
// We can't allow the type property to be changed (since it causes problems in IE)
314+
if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
315+
jQuery.error( "type property can't be changed" );
333316
}
334317

335-
// elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
336-
// http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
337-
if ( name === "tabIndex" ) {
338-
var attributeNode = elem.getAttributeNode( "tabIndex" );
318+
if ( value === null ) {
319+
if ( elem.nodeType === 1 ) {
320+
elem.removeAttribute( name );
321+
}
339322

340-
return attributeNode && attributeNode.specified ?
341-
attributeNode.value :
342-
rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
343-
0 :
344-
undefined;
323+
} else {
324+
elem[ name ] = value;
345325
}
326+
}
346327

347-
return elem[ name ];
328+
// browsers index elements by id/name on forms, give priority to attributes.
329+
if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
330+
return elem.getAttributeNode( name ).nodeValue;
348331
}
349332

350-
if ( !jQuery.support.style && notxml && name === "style" ) {
351-
if ( set ) {
352-
elem.style.cssText = "" + value;
353-
}
333+
// elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
334+
// http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
335+
if ( name === "tabIndex" ) {
336+
var attributeNode = elem.getAttributeNode( "tabIndex" );
354337

355-
return elem.style.cssText;
338+
return attributeNode && attributeNode.specified ?
339+
attributeNode.value :
340+
rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
341+
0 :
342+
undefined;
356343
}
357344

345+
return elem[ name ];
346+
}
347+
348+
if ( !jQuery.support.style && notxml && name === "style" ) {
358349
if ( set ) {
359-
// convert the value to a string (all browsers do this but IE) see #1070
360-
elem.setAttribute( name, "" + value );
350+
elem.style.cssText = "" + value;
361351
}
362352

363-
// Ensure that missing attributes return undefined
364-
// Blackberry 4.7 returns "" from getAttribute #6938
365-
if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
366-
return undefined;
367-
}
353+
return elem.style.cssText;
354+
}
368355

369-
var attr = !jQuery.support.hrefNormalized && notxml && special ?
370-
// Some attributes require a special call on IE
371-
elem.getAttribute( name, 2 ) :
372-
elem.getAttribute( name );
356+
if ( set ) {
357+
// convert the value to a string (all browsers do this but IE) see #1070
358+
elem.setAttribute( name, "" + value );
359+
}
373360

374-
// Non-existent attributes return null, we normalize to undefined
375-
return attr === null ? undefined : attr;
361+
// Ensure that missing attributes return undefined
362+
// Blackberry 4.7 returns "" from getAttribute #6938
363+
if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
364+
return undefined;
376365
}
366+
367+
var attr = !jQuery.support.hrefNormalized && notxml && special ?
368+
// Some attributes require a special call on IE
369+
elem.getAttribute( name, 2 ) :
370+
elem.getAttribute( name );
371+
372+
// Non-existent attributes return null, we normalize to undefined
373+
return attr === null ? undefined : attr;
377374
}
378375
});
379376

test/unit/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var bareObj = function(value) { return value; };
44
var functionReturningObj = function(value) { return (function() { return value; }); };
55

66
test("attr(String)", function() {
7-
expect(30);
7+
expect(31);
88

99
// This one sometimes fails randomly ?!
1010
equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
@@ -65,6 +65,8 @@ test("attr(String)", function() {
6565

6666
ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
6767
ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
68+
69+
equals( jQuery(document).attr("nodeName"), "#document", "attr works correctly on document nodes (bug #7451)." );
6870
});
6971

7072
if ( !isLocal ) {

0 commit comments

Comments
 (0)