Permalink
Browse files

Navigation: Remove fallback for browsers lacking dynamic base support

Do not unconditionally inject base tag

Fixes gh-7331
Fixes gh-5757
Fixes gh-7489
Closes gh-7895
  • Loading branch information...
gabrielschulhof committed Jan 2, 2015
1 parent 866fa98 commit 780f0e45ccb8da6bae5fa4579568a6ba09bd5c36
View
@@ -84,10 +84,6 @@ define( [ "jquery", "./ns", "json!../package.json" ], function( jQuery, ns, pkg
hoverDelay: 200
},
// disable the alteration of the dynamic base tag or links in the case
// that a dynamic base tag isn't supported
dynamicBaseEnabled: true,
// default the property to remove dependency on assignment in init module
pageContainer: $(),
View
@@ -5,7 +5,11 @@
//>>css.structure: ../css/structure/jquery.mobile.core.css
//>>css.theme: ../css/themes/default/jquery.mobile.theme.css
define( [ "jquery", "./ns", "jquery-ui/jquery.ui.core" ], function( jQuery ) {
define( [
"jquery",
"./ns",
"./navigation/base",
"jquery-ui/jquery.ui.core" ], function( jQuery ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, window, undefined ) {
@@ -93,7 +97,7 @@ define( [ "jquery", "./ns", "jquery-ui/jquery.ui.core" ], function( jQuery ) {
var url = $( ele ).closest( ".ui-page" ).jqmData( "url" ),
base = $.mobile.path.documentBase.hrefNoHash;
if ( !$.mobile.dynamicBaseEnabled || !url || !$.mobile.path.isPath( url ) ) {
if ( !$.mobile.base.dynamicBaseEnabled || !url || !$.mobile.path.isPath( url ) ) {
url = base;
}
View
@@ -10,60 +10,69 @@ define([
(function( $, undefined ) {
// existing base tag?
var baseElement = $( "head" ).children( "base" ),
var base,
// Existing base tag?
baseElement = $( "head" ).children( "base" ),
// DEPRECATED as of 1.5.0 and will be removed in 1.6.0. As of 1.6.0 only
// base.dynamicBaseEnabled will be checked
getDynamicEnabled = function() {
// If a value has been set at the old, deprecated location, we return that value.
// Otherwise we return the value from the new location. We check explicitly for
// undefined because true and false are both valid values for dynamicBaseEnabled.
if ( $.mobile.dynamicBaseEnabled !== undefined ) {
return $.mobile.dynamicBaseEnabled;
}
return base.dynamicBaseEnabled;
};
// base element management, defined depending on dynamic base tag support
// TODO move to external widget
base = {
// define base element, for use in routing asset urls that are referenced
// Disable the alteration of the dynamic base tag or links
dynamicBaseEnabled: true,
// Make sure base element is defined, for use in routing asset urls that are referenced
// in Ajax-requested markup
element: ( baseElement.length ? baseElement :
$( "<base>", { href: $.mobile.path.documentBase.hrefNoHash } ).prependTo( $( "head" ) ) ),
element: function() {
if ( !( baseElement && baseElement.length ) ) {
baseElement = $( "<base>", { href: $.mobile.path.documentBase.hrefNoSearch } )
.prependTo( $( "head" ) );
}
linkSelector: "[src], link[href], a[rel='external'], :jqmData(ajax='false'), a[target]",
return baseElement;
},
// set the generated BASE element's href to a new page's base path
set: function( href ) {
// we should do nothing if the user wants to manage their url base
// manually
if ( !$.mobile.dynamicBaseEnabled ) {
// We should do nothing if the user wants to manage their url base manually.
// Note: Our method of ascertaining whether the user wants to manager their url base
// manually is DEPRECATED as of 1.5.0 and will be removed in 1.6.0. As of 1.6.0 the
// flag base.dynamicBaseEnabled will be checked, so the function getDynamicEnabled()
// will be removed.
if ( !getDynamicEnabled() ) {
return;
}
// we should use the base tag if we can manipulate it dynamically
if ( $.support.dynamicBaseTag ) {
base.element.attr( "href",
$.mobile.path.makeUrlAbsolute( href, $.mobile.path.documentBase ) );
}
},
rewrite: function( href, page ) {
var newPath = $.mobile.path.get( href );
page.find( base.linkSelector ).each(function( i, link ) {
var thisAttr = $( link ).is( "[href]" ) ? "href" :
$( link ).is( "[src]" ) ? "src" : "action",
theLocation = $.mobile.path.parseLocation(),
thisUrl = $( link ).attr( thisAttr );
// XXX_jblas: We need to fix this so that it removes the document
// base URL, and then prepends with the new page URL.
// if full path exists and is same, chop it - helps IE out
thisUrl = thisUrl.replace( theLocation.protocol + theLocation.doubleSlash +
theLocation.host + theLocation.pathname, "" );
if ( !/^(\w+:|#|\/)/.test( thisUrl ) ) {
$( link ).attr( thisAttr, newPath + thisUrl );
}
});
base.element().attr( "href",
$.mobile.path.makeUrlAbsolute( href, $.mobile.path.documentBase ) );
},
// set the generated BASE element's href to a new page's base path
reset: function(/* href */) {
base.element.attr( "href", $.mobile.path.documentBase.hrefNoSearch );
// DEPRECATED as of 1.5.0 and will be removed in 1.6.0. As of 1.6.0 only
// base.dynamicBaseEnabled will be checked
if ( !getDynamicEnabled() ) {
return;
}
base.element().attr( "href", $.mobile.path.documentBase.hrefNoSearch );
}
};
View
@@ -76,30 +76,6 @@ function transform3dTest() {
return ( !!ret && ret !== "none" );
}
// Test for dynamic-updating base tag support ( allows us to avoid href,src attr rewriting )
function baseTagTest() {
var fauxBase = location.protocol + "//" + location.host + location.pathname + "ui-dir/",
base = $( "head base" ),
fauxEle = null,
href = "",
link, rebase;
if ( !base.length ) {
base = fauxEle = $( "<base>", { "href": fauxBase }).appendTo( "head" );
} else {
href = base.attr( "href" );
}
link = $( "<a href='testurl' />" ).prependTo( fakeBody );
rebase = link[ 0 ].href;
base[ 0 ].href = href || location.pathname;
if ( fauxEle ) {
fauxEle.remove();
}
return rebase.indexOf( fauxBase ) === 0;
}
// Thanks Modernizr
function cssPointerEventsTest() {
var element = document.createElement( "x" ),
@@ -193,7 +169,6 @@ $.extend( $.support, {
"scrollTop" in document.documentElement ||
"scrollTop" in fakeBody[ 0 ]) && !webos && !operamini,
dynamicBaseTag: baseTagTest(),
cssPointerEvents: cssPointerEventsTest(),
boundingRect: boundingRect(),
inlineSVG: inlineSVG
@@ -464,10 +464,6 @@ define( [
}
},
_isRewritableBaseTag: function() {
return $.mobile.dynamicBaseEnabled && !$.support.dynamicBaseTag;
},
_createDataUrl: function( absoluteUrl ) {
return $.mobile.path.convertUrlToDataUrl( absoluteUrl );
},
@@ -552,11 +548,6 @@ define( [
return;
}
// rewrite src and href attrs to use a base url if the base tag won't work
if ( this._isRewritableBaseTag() && content ) {
this._getBase().rewrite( fileUrl, content );
}
this._include( content, settings );
// Remove loading message.
@@ -0,0 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile Navigation Test Suite</title>
<script src="../../../external/requirejs/require.js"></script>
<script src="../../../js/requirejs.config.js"></script>
<script src="../../../js/jquery.tag.inserter.js"></script>
<script src="../../tests/jquery.testHelper.js"></script>
<link rel="stylesheet" href="../../../css/themes/default/jquery.mobile.css"/>
<link rel="stylesheet" href="../../../external/qunit/qunit.css"/>
<link rel="stylesheet" href="../../jqm-tests.css"/>
<script src="../../../external/qunit/qunit.js"></script>
<script src="../../jquery.testHelper.js"></script>
<script>
$.testHelper.asyncLoad([
[
"navigation/base"
],
[
"dynamic_base_disabled_core.js"
]
]);
</script>
<script src="../swarminject.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
@@ -0,0 +1,31 @@
test( "Base tag inserted only if dynamic base tag support is enabled", function() {
var docBaseUrl = $.mobile.path.documentBase.hrefNoSearch;
deepEqual( $( "head" ).children( "base" ).length, 0, "Initially, no base tag is present" );
$.mobile.base.dynamicBaseEnabled = false;
$.mobile.base.set( "http://example.com/" );
deepEqual( $( "head" ).children( "base" ).length, 0,
"Calling set() while the flag is disabled causes no base tag to be inserted" );
$.mobile.base.dynamicBaseEnabled = true;
$.mobile.base.set( "http://example.com/" );
deepEqual( $( "head" ).children( "base" ).length, 1,
"Calling set() while the flag is enabled causes one base tag to be inserted" );
deepEqual( $( "head" ).children( "base" ).attr( "href" ), "http://example.com/",
"The base URL is correctly set after calling set() while the flag is enabled" );
$.mobile.base.dynamicBaseEnabled = false;
$.mobile.base.reset();
deepEqual( $( "head" ).children( "base" ).length, 1,
"Calling reset() while the flag is disabled causes no more base tags to be inserted" );
deepEqual( $( "head" ).children( "base" ).attr( "href" ), "http://example.com/",
"The base remains correct after calling reset() with the flag unset" );
$.mobile.base.dynamicBaseEnabled = true;
$.mobile.base.reset();
deepEqual( $( "head" ).children( "base" ).length, 1,
"Calling reset() while the flag is enabled causes no more base tags to be inserted" );
deepEqual( $( "head" ).children( "base" ).attr( "href" ), docBaseUrl,
"Calling reset() while the flag is enabled correctly resets the base URL" );
});
@@ -243,25 +243,4 @@
ok( baseHref !== $( "base" ).attr( "href" ), "the base href value should be changed" );
});
});
asyncTest( "enabling base tag changes when a dynamic base isn't supported should alter links", function() {
$.mobile.dynamicBaseEnabled = true;
$.support.dynamicBaseTag = false;
testBaseTagAlteration(function() {
var linkHref = $.mobile.activePage.find( "#base-change-link" ).attr( "href" );
if ( $.support.pushState ) {
equal( linkHref,
$.mobile.path.get( location.href ) + "foo",
"the link's href is changed" );
} else {
// compare the pathname of the links href with the directory of the current
// location + foo
equal( $.mobile.path.parseUrl( linkHref ).pathname,
$.mobile.path.parseUrl( location.href ).directory + "foo",
"the link's href is changed" );
}
});
});
})(jQuery);
@@ -77,14 +77,6 @@ $.testHelper.excludeFileProtocol(function(){
};
};
asyncTest( "detects dynamic base tag when new base element added and base href updates", function(){
mockBaseCheck(location.protocol + '//' + location.host + location.pathname + "ui-dir/");
$.testHelper.reloadModule( moduleName ).done( function() {
ok($.support.dynamicBaseTag);
start();
});
});
asyncTest( "detects no dynamic base tag when new base element added and base href unchanged", function(){
mockBaseCheck('testurl');
$.testHelper.reloadModule( moduleName ).done( function() {

0 comments on commit 780f0e4

Please sign in to comment.