Tabs: Fixed detection of local vs. remote tabs. Fixes #4941 - Mishandling of base tag. Fixes #4836 - Self refering href only partially detected.

This commit is contained in:
Scott González 2011-08-11 16:48:58 -04:00
parent ac04462d22
commit 18a3b53988
2 changed files with 21 additions and 31 deletions

View File

@ -48,7 +48,7 @@ test( "aria-controls", function() {
tabs = element.find( ".ui-tabs-nav a" );
tabs.each(function() {
var tab = $( this );
equal( tab.attr( "href" ).substring( 1 ), tab.attr( "aria-controls" ) );
equal( tab.prop( "hash" ).substring( 1 ), tab.attr( "aria-controls" ) );
});
element = $( "#tabs2" ).tabs();

50
ui/jquery.ui.tabs.js vendored
View File

@ -18,6 +18,19 @@ function getNextTabId() {
return ++tabId;
}
var isLocal = (function() {
var rhash = /#.*$/,
currentPage = location.href.replace( rhash, "" );
return function( anchor ) {
// clone the node to work around IE 6 not normalizing the href property
// if it's manually set, i.e., a.href = "#foo" kills the normalization
anchor = anchor.cloneNode();
return anchor.hash.length > 1 &&
anchor.href.replace( rhash, "" ) === currentPage;
};
})();
$.widget( "ui.tabs", {
version: "@VERSION",
options: {
@ -197,8 +210,7 @@ $.widget( "ui.tabs", {
},
_processTabs: function() {
var self = this,
fragmentId = /^#.+/; // Safari 2 reports '#' for an empty hash
var self = this;
this.list = this.element.find( "ol,ul" ).eq( 0 );
this.lis = $( " > li:has(a[href])", this.list );
@ -208,30 +220,14 @@ $.widget( "ui.tabs", {
this.panels = $( [] );
this.anchors.each(function( i, a ) {
var href = $( a ).attr( "href" ),
hrefBase = href.split( "#" )[ 0 ],
selector,
panel,
baseEl;
// For dynamically created HTML that contains a hash as href IE < 8 expands
// such href to the full page url with hash and then misinterprets tab as ajax.
// Same consideration applies for an added tab with a fragment identifier
// since a[href=#fragment-identifier] does unexpectedly not match.
// Thus normalize href attribute...
if ( hrefBase && ( hrefBase === location.toString().split( "#" )[ 0 ] ||
( baseEl = $( "base" )[ 0 ]) && hrefBase === baseEl.href ) ) {
href = a.hash;
a.href = href;
}
var selector, panel;
// inline tab
if ( fragmentId.test( href ) ) {
selector = href;
if ( isLocal( a ) ) {
selector = a.hash;
panel = self.element.find( self._sanitizeSelector( selector ) );
// remote tab
// prevent loading the page itself if href is just "#"
} else if ( href && href !== "#" ) {
} else {
var id = self._tabId( a );
selector = "#" + id;
panel = self.element.find( selector );
@ -239,9 +235,6 @@ $.widget( "ui.tabs", {
panel = self._createPanel( id );
panel.insertAfter( self.panels[ i - 1 ] || self.list );
}
// invalid tab href
} else {
self.options.disabled.push( i );
}
if ( panel.length) {
@ -525,21 +518,18 @@ $.widget( "ui.tabs", {
options = this.options,
anchor = this.anchors.eq( index ),
panel = self._getPanelForTab( anchor ),
// TODO until #3808 is fixed strip fragment identifier from url
// (IE fails to load from such url)
url = anchor.attr( "href" ).replace( /#.*$/, "" ),
eventData = {
tab: anchor,
panel: panel
};
// not remote
if ( !url ) {
if ( isLocal( anchor[ 0 ] ) ) {
return;
}
this.xhr = $.ajax({
url: url,
url: anchor.attr( "href" ),
beforeSend: function( jqXHR, settings ) {
return self._trigger( "beforeLoad", event,
$.extend( { jqXHR : jqXHR, ajaxSettings: settings }, eventData ) );