Skip to content

Null attributes #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Core: Fix :focusable and :tabbable with jQuery git
jQuery now returns `null` for empty attributes instead of `undefined`
  • Loading branch information
scottgonzalez committed Mar 23, 2015
commit cd8698c9c2307144adc1e1b2e280681dc7fc95e4
12 changes: 6 additions & 6 deletions ui/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ $.fn.extend( {
} );

// selectors
function focusable( element, isTabIndexNotNaN ) {
function focusable( element, hasTabindex ) {
var map, mapName, img,
nodeName = element.nodeName.toLowerCase();
if ( "area" === nodeName ) {
Expand All @@ -131,8 +131,8 @@ function focusable( element, isTabIndexNotNaN ) {
return ( /^(input|select|textarea|button|object)$/.test( nodeName ) ?
!element.disabled :
"a" === nodeName ?
element.href || isTabIndexNotNaN :
isTabIndexNotNaN ) &&
element.href || hasTabindex :
hasTabindex ) &&
// the element and all of its ancestors must be visible
visible( element );
}
Expand All @@ -157,13 +157,13 @@ $.extend( $.expr[ ":" ], {
},

focusable: function( element ) {
return focusable( element, !isNaN( $.attr( element, "tabindex" ) ) );
return focusable( element, $.attr( element, "tabindex" ) != null );
Copy link
Member

Choose a reason for hiding this comment

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

This code isn't equivalent, and it looks like even the old version has bugs for some unusual tabindex values (e.g., "0xFF", "Infinity"). Reviewing https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute and reviewing browser behavior, I think you want !isNaN( parseInt( $.attr( element, "tabindex" ), 10 ) ). See also: https://github.com/jquery/jquery/blob/7b111310977a30d02a8260a7ed6c9c437bb1b0a5/src/attributes/prop.js#L64-L79

Copy link
Member Author

Choose a reason for hiding this comment

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

"0xFF" and "Infinity" aren't valid values. We've decided not to account for such markup.

Copy link
Member

Choose a reason for hiding this comment

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

So the presence of a "tabindex" attribute is all that matters, independent of its value? In that case, 👍.

Copy link
Member Author

Choose a reason for hiding this comment

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

For our intents, yes. We assume that if the attribute exists, it contains a valid value. If someone were to file a bug saying that they did something like tabindex="bananas" and it resulted in our selectors finding incorrect elements, we'd close it the same as if they filed a bug about our widgets not working properly when they used the same id on multiple elements :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

We used to actually test for invalid values: f80d9eb#diff-47738b779c41035dcc321cc78634c6a5R87, but we dropped those tests four years ago.

},

tabbable: function( element ) {
var tabIndex = $.attr( element, "tabindex" ),
isTabIndexNaN = isNaN( tabIndex );
return ( isTabIndexNaN || tabIndex >= 0 ) && focusable( element, !isTabIndexNaN );
hasTabindex = tabIndex != null;
return ( !hasTabindex || tabIndex >= 0 ) && focusable( element, hasTabindex );
Copy link
Member

Choose a reason for hiding this comment

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

Same concerns here.

}
} );

Expand Down