Skip to content

Commit ecd4f95

Browse files
committed
Tabs: Use .uniqueId() for panels and moved isLocal() into the widget prototype.
1 parent 604e094 commit ecd4f95

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

tests/unit/tabs/tabs_core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ test( "aria-controls", function() {
6767
tabs = element.find( ".ui-tabs-nav li" );
6868
equal( tabs.eq( 0 ).attr( "aria-controls" ), "colon:test" );
6969
equal( tabs.eq( 1 ).attr( "aria-controls" ), "inline-style" );
70-
ok( /^ui-tabs-\d+$/.test( tabs.eq( 2 ).attr( "aria-controls" ) ), "generated id" );
70+
ok( /^ui-id-\d+$/.test( tabs.eq( 2 ).attr( "aria-controls" ) ), "generated id" );
7171
equal( tabs.eq( 3 ).attr( "aria-controls" ), "custom-id" );
7272
});
7373

ui/jquery.ui.tabs.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@
1414
*/
1515
(function( $, undefined ) {
1616

17-
var tabId = 0,
18-
rhash = /#.*$/;
19-
20-
function getNextTabId() {
21-
return ++tabId;
22-
}
23-
24-
function isLocal( anchor ) {
25-
// support: IE7
26-
// IE7 doesn't normalize the href property when set via script (#9317)
27-
anchor = anchor.cloneNode( false );
28-
29-
return anchor.hash.length > 1 &&
30-
decodeURIComponent( anchor.href.replace( rhash, "" ) ) ===
31-
decodeURIComponent( location.href.replace( rhash, "" ) );
32-
}
33-
3417
$.widget( "ui.tabs", {
3518
version: "@VERSION",
3619
delay: 300,
@@ -49,6 +32,21 @@ $.widget( "ui.tabs", {
4932
load: null
5033
},
5134

35+
_isLocal: (function() {
36+
var rhash = /#.*$/;
37+
38+
return function( anchor ) {
39+
40+
// support: IE7
41+
// IE7 doesn't normalize the href property when set via script (#9317)
42+
anchor = anchor.cloneNode( false );
43+
44+
return anchor.hash.length > 1 &&
45+
decodeURIComponent( anchor.href.replace( rhash, "" ) ) ===
46+
decodeURIComponent( location.href.replace( rhash, "" ) );
47+
};
48+
})(),
49+
5250
_create: function() {
5351
var that = this,
5452
options = this.options;
@@ -296,10 +294,6 @@ $.widget( "ui.tabs", {
296294
}
297295
},
298296

299-
_tabId: function( tab ) {
300-
return tab.attr( "aria-controls" ) || "ui-tabs-" + getNextTabId();
301-
},
302-
303297
_sanitizeSelector: function( hash ) {
304298
return hash ? hash.replace( /[!"$%&'()*+,.\/:;<=>?@\[\]\^`{|}~]/g, "\\$&" ) : "";
305299
},
@@ -406,12 +400,15 @@ $.widget( "ui.tabs", {
406400
originalAriaControls = tab.attr( "aria-controls" );
407401

408402
// inline tab
409-
if ( isLocal( anchor ) ) {
403+
if ( that._isLocal( anchor ) ) {
410404
selector = anchor.hash;
405+
panelId = selector.substring( 1 );
411406
panel = that.element.find( that._sanitizeSelector( selector ) );
412407
// remote tab
413408
} else {
414-
panelId = that._tabId( tab );
409+
// If the tab doesn't already have aria-controls,
410+
// generate an id by using a throw-away element
411+
panelId = tab.attr( "aria-controls" ) || $( {} ).uniqueId()[ 0 ].id;
415412
selector = "#" + panelId;
416413
panel = that.element.find( selector );
417414
if ( !panel.length ) {
@@ -428,7 +425,7 @@ $.widget( "ui.tabs", {
428425
tab.data( "ui-tabs-aria-controls", originalAriaControls );
429426
}
430427
tab.attr({
431-
"aria-controls": selector.substring( 1 ),
428+
"aria-controls": panelId,
432429
"aria-labelledby": anchorId
433430
});
434431
panel.attr( "aria-labelledby", anchorId );
@@ -790,7 +787,7 @@ $.widget( "ui.tabs", {
790787
};
791788

792789
// not remote
793-
if ( isLocal( anchor[ 0 ] ) ) {
790+
if ( this._isLocal( anchor[ 0 ] ) ) {
794791
return;
795792
}
796793

0 commit comments

Comments
 (0)