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
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions tests/unit/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ test( "outerHeight(true) - setter", function() {
test( "uniqueId / removeUniqueId", function() {
expect( 3 );
var el = $( "img" ).eq( 0 );
strictEqual( el.attr( "id" ), undefined, "element has no initial id" );
equal( el.attr( "id" ), null, "element has no initial id" );
el.uniqueId();
ok( /ui-id-\d+$/.test( el.attr( "id" ) ), "element has generated id" );
el.removeUniqueId();
strictEqual( el.attr( "id" ), undefined, "unique id has been removed from element" );
equal( el.attr( "id" ), null, "unique id has been removed from element" );
});

})( jQuery );
2 changes: 1 addition & 1 deletion tests/unit/dialog/dialog_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test( "ARIA", function() {
element.remove();

element = $("<div><div aria-describedby='section2'><p id='section2'>descriotion</p></div></div>").dialog();
strictEqual( element.dialog( "widget" ).attr( "aria-describedby" ), undefined, "no aria-describedby added, as already present in markup" );
equal( element.dialog( "widget" ).attr( "aria-describedby" ), null, "no aria-describedby added, as already present in markup" );
element.remove();
});

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/menu/menu_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ test( "{ role: null }", function( assert ) {
}),
items = element.find( "li" );
expect( 2 + 3 * items.length );
strictEqual( element.attr( "role" ), undefined );
equal( element.attr( "role" ), null );
ok( items.length > 0, "number of menu items" );
items.each(function( item ) {
assert.hasClasses( $( this ), "ui-menu-item" );
equal( $( this ).find( ".ui-menu-item-wrapper" ).attr( "role" ), undefined,
equal( $( this ).find( ".ui-menu-item-wrapper" ).attr( "role" ), null,
"menu item ("+ item + ") role" );
equal( $( this ).find( ".ui-menu-item-wrapper" ).attr( "tabindex" ), "-1",
"tabindex for menu item ("+ item + ")" );
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/progressbar/progressbar_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test( "accessibility", function() {
element.progressbar( "option", "value", false );
equal( element.attr( "aria-valuemin" ), 0, "aria-valuemin" );
equal( element.attr( "aria-valuemax" ), 150, "aria-valuemax" );
strictEqual( element.attr( "aria-valuenow" ), undefined, "aria-valuenow" );
equal( element.attr( "aria-valuenow" ), null, "aria-valuenow" );
});

}( jQuery ) );
4 changes: 2 additions & 2 deletions tests/unit/testsuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ window.domEqual = function( selector, modifier, message ) {
result = {};
$.each( properties, function( index, attr ) {
var value = elem.prop( attr );
result[ attr ] = value !== undefined ? value : "";
result[ attr ] = value != null ? value : "";
Copy link
Member

Choose a reason for hiding this comment

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

This strict comparison was fine as-is; .prop lacks the DOM semantics that urged us to try changing .attr.

Copy link
Member

Choose a reason for hiding this comment

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

And looking at the broader context, domEqual might still work if you stopped doing this empty-string normalization altogether and just assigned results from .prop and .attr directly.

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 do the normalization because of space delimited attributes, the most common one being class. The attribute will start out undefined, but after elem.addClass( "foo" ).removeClass( "foo" ), the attribute will be an empty string. We consider this equivalent.

For the property check, the aim was just consistency in the logic. I don't think we've ever consider null and undefined to be non-equivalent in our check.

});
$.each( attributes, function( index, attr ) {
var value = elem.attr( attr );
result[ attr ] = value !== undefined ? value : "";
result[ attr ] = value != null ? value : "";
});
result.style = getElementStyles( elem[ 0 ] );
result.events = $._data( elem[ 0 ], "events" );
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/tooltip/tooltip_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ test( "accessibility", function() {
equal( element.attr( "aria-describedby" ), "fixture-span " + tooltipId,
"multiple describedby when open" );

// strictEqual to distinguish between .removeAttr( "title" ) and .attr( "title", "" )
strictEqual( element.attr( "title" ), undefined, "no title when open" );
equal( element.attr( "title" ), null, "no title when open" );
equal( liveRegion.children().length, 1 );
equal( liveRegion.children().last().html(), "..." );
element.tooltip( "close" );
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/tooltip/tooltip_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test( "enable/disable", function( assert ) {
assert.lacksClasses( element.tooltip( "widget" ), "ui-state-disabled" );
ok( !element.tooltip( "widget" ).attr( "aria-disabled" ), "element doesn't get aria-disabled" );
assert.lacksClasses( element.tooltip( "widget" ), "ui-tooltip-disabled" );
strictEqual( tooltip.attr( "title" ), undefined, "title removed on disable" );
equal( tooltip.attr( "title" ), null, "title removed on disable" );

element.tooltip( "open" );
equal( $( ".ui-tooltip" ).length, 0, "open does nothing when disabled" );
Expand Down
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
9 changes: 6 additions & 3 deletions ui/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ $.widget( "ui.dialog", {
index: this.element.parent().children().index( this.element )
};
this.originalTitle = this.element.attr( "title" );
this.options.title = this.options.title || this.originalTitle;
if ( this.options.title == null && this.originalTitle != null ) {
Copy link
Member

Choose a reason for hiding this comment

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

That looks like a ride-along bug fix for empty-string title. 😎

this.options.title = this.originalTitle;
}

this._createWrapper();

Expand Down Expand Up @@ -433,10 +435,11 @@ $.widget( "ui.dialog", {
},

_title: function( title ) {
if ( !this.options.title ) {
if ( this.options.title ) {
title.text( this.options.title );
Copy link
Member Author

Choose a reason for hiding this comment

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

Even if core back out the change, this change should stay.

Copy link
Member

Choose a reason for hiding this comment

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

I would say the same of every change here. If nothing else, specifically counting on undefined seems risky.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's legitimate. I'll go ahead and land this.

} else {
title.html( "&#160;" );
}
title.text( this.options.title );
},

_createButtonPane: function() {
Expand Down
2 changes: 1 addition & 1 deletion ui/spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ return $.widget( "ui.spinner", {

$.each( [ "min", "max", "step" ], function( i, option ) {
var value = element.attr( option );
if ( value !== undefined && value.length ) {
if ( value != null && value.length ) {
options[ option ] = value;
}
} );
Expand Down