Skip to content
Permalink
Browse files
Make sure that missing attributes return undefined in Blackberry 4.7.…
… Fixes #6938.
  • Loading branch information
jeresig committed Aug 24, 2010
1 parent 1bdcffb commit cb40495
Showing 1 changed file with 6 additions and 0 deletions.
@@ -325,6 +325,12 @@ jQuery.extend({
elem.setAttribute( name, "" + value );
}

// Ensure that missing attributes return undefined
// Blackberry 4.7 returns "" from getAttribute #6938
if ( !elem.attributes[ name ] && !elem.hasAttribute( name ) ) {

This comment has been minimized.

Copy link
@jitter

jitter Sep 27, 2010

Contributor

This change breaks some attr() calls in IE6/IE7 as they don't seem to support the hasAttribute call.

Example this test from test/unit/attributes.js:
equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );

For textarea the casing of maxLength in IE6/IE7 is all lowercase. As the casing of name is different from the internal casing of IE6/7 for the attribute we get a Object doesn't support this property or method exception when the !elem.hasAttribute( name ) part is evaluated.

This comment has been minimized.

Copy link
@jeresig

jeresig Sep 27, 2010

Author Member

I've landed a fix for this here: a384d84 Thankfully we already had a fallback in place which allowed it to continue working. There are some bugs with .val() in IE now that I'm checking in to - not sure if it's related to this, or not. Thanks again!

return undefined;
}

var attr = !jQuery.support.hrefNormalized && notxml && special ?
// Some attributes require a special call on IE
elem.getAttribute( name, 2 ) :

7 comments on commit cb40495

@jdalton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means it follows spec
getAttribute shouldn't be used as a hasAttribute alternative and just over-complicates things when attempted.

@jzaefferer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we can be happy that other browsers don't follow the spec, and hope for a fix to the spec instead.

@jdalton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or yall could return an empty string "" if it gets a null value :D

@jzaefferer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then add hasAttr to jQuery?

@jdalton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Why not? Lets go for the gold and get a decent attribute API for jQuery.

@jzaefferer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is adding an additional method gold, while returning undefined for undefined attributes already works?

Would you prefer to do hasProperty(myObject.someProperty) && myObject.someProperty... instead of just myObject.someProperty && myObject.someProperty...?

@jdalton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow you. I guess you aren't a fan.
As long as node.hasAttribute(), and attrNode.specified as a fallback, are used, even internally, it will help reduce the likelihood of other errors.

Please sign in to comment.