From e4e617605053528329d6c3ee874e300cd5cb1e4c Mon Sep 17 00:00:00 2001 From: Richard Collins Date: Mon, 24 Feb 2014 11:44:52 +1300 Subject: [PATCH 1/3] Potential fix for #6162 --- src/ng/location.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/ng/location.js b/src/ng/location.js index a6c3952a9036..d56d5b65c0c5 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -268,6 +268,16 @@ function LocationHashbangInHtml5Url(appBase, hashPrefix) { return appBaseNoFile; } }; + + this.$$compose = function() { + var search = toKeyValue(this.$$search), + hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : ''; + + this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash; + // include hashPrefix in $$absUrl when $$url is empty so IE8 & 9 do not reload page because of removal of '#' + this.$$absUrl = appBase + hashPrefix + this.$$url; + }; + } @@ -642,6 +652,35 @@ function $LocationProvider(){ absHref = urlResolve(absHref.animVal).href; } + // Make relative links work in HTML5 mode for legacy browsers (or at least IE8 & 9) + // The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or somewhere#anchor or http://example.com/somewhere + if (LocationMode === LocationHashbangInHtml5Url) { + // get the actual href attribute - see http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx + // TODO check browser is in standards mode + var href = elm[0].getAttribute('href'); + + if (href.indexOf('://' == -1)) { // Ignore absolute URLs + if (href[0] == '/') { + // absolute path - replace old path + absHref = serverBase(absHref) + href; + } else { + // relative path - join with current path + var stack = $location.path().split("/"), + parts = href.split("/"); + stack.pop(); // remove top file + for (var i=0; i Date: Mon, 17 Mar 2014 12:03:44 +1300 Subject: [PATCH 2/3] Parse anchors in IE9 --- src/ng/location.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index d56d5b65c0c5..1ed352f1d62d 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -274,7 +274,7 @@ function LocationHashbangInHtml5Url(appBase, hashPrefix) { hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : ''; this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash; - // include hashPrefix in $$absUrl when $$url is empty so IE8 & 9 do not reload page because of removal of '#' + // include hashPrefix in $$absUrl when $$url is empty so IE8 & 9 do not reload page because of removal of '#' this.$$absUrl = appBase + hashPrefix + this.$$url; }; @@ -653,18 +653,21 @@ function $LocationProvider(){ } // Make relative links work in HTML5 mode for legacy browsers (or at least IE8 & 9) - // The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or somewhere#anchor or http://example.com/somewhere + // The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or somewhere#anchor or http://example.com/somewhere if (LocationMode === LocationHashbangInHtml5Url) { // get the actual href attribute - see http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx // TODO check browser is in standards mode var href = elm[0].getAttribute('href'); - - if (href.indexOf('://' == -1)) { // Ignore absolute URLs + + if (href.indexOf('://' == -1)) { // Ignore absolute URLs if (href[0] == '/') { // absolute path - replace old path absHref = serverBase(absHref) + href; + } else if (href[0] == '#') { + // local anchor + absHref = serverBase(absHref) + $location.path() + href; } else { - // relative path - join with current path + // relative path - join with current path var stack = $location.path().split("/"), parts = href.split("/"); stack.pop(); // remove top file @@ -678,7 +681,7 @@ function $LocationProvider(){ } absHref = serverBase(absHref) + stack.join("/"); } - } + } } var rewrittenUrl = $location.$$rewrite(absHref); From 32acecddc023173792e8f0fc30552e73d6bf6671 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Fri, 28 Mar 2014 14:13:25 -0400 Subject: [PATCH 3/3] feat($location): test $location enhancements from #6421 and fix them This CL fixes problems and adds test cases for changes from #6421. Changes include fixing the algorithm for preprocessing href attribute values, as well as supporting xlink:href attributes. --- src/ng/location.js | 21 +++++++------- test/ng/locationSpec.js | 62 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index 1ed352f1d62d..3caa14e8de61 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -653,33 +653,34 @@ function $LocationProvider(){ } // Make relative links work in HTML5 mode for legacy browsers (or at least IE8 & 9) - // The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or somewhere#anchor or http://example.com/somewhere + // The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or + // somewhere#anchor or http://example.com/somewhere if (LocationMode === LocationHashbangInHtml5Url) { - // get the actual href attribute - see http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx - // TODO check browser is in standards mode - var href = elm[0].getAttribute('href'); + // get the actual href attribute - see + // http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx + var href = elm.attr('href') || elm.attr('xlink:href'); - if (href.indexOf('://' == -1)) { // Ignore absolute URLs + if (href.indexOf('://') < 0) { // Ignore absolute URLs + var prefix = '#' + hashPrefix; if (href[0] == '/') { // absolute path - replace old path - absHref = serverBase(absHref) + href; + absHref = appBase + prefix + href; } else if (href[0] == '#') { // local anchor - absHref = serverBase(absHref) + $location.path() + href; + absHref = appBase + prefix + ($location.path() || '/') + href; } else { // relative path - join with current path var stack = $location.path().split("/"), parts = href.split("/"); - stack.pop(); // remove top file for (var i=0; i' + content + '')[0]; @@ -1067,6 +1074,51 @@ describe('$location', function() { }); + it('should rewrite relative links relative to current path when history disabled', function() { + configureService('link', true, false, true); + inject( + initBrowser(), + initLocation(), + function($browser, $location) { + $location.path('/some'); + browserTrigger(link, 'click'); + expectRewriteTo($browser, 'http://host.com/base/index.html#!/some/link'); + } + ); + }); + + + it('should replace current path when link begins with "/" and history disabled', function() { + configureService('/link', true, false, true); + inject( + initBrowser(), + initLocation(), + function($browser, $location) { + $location.path('/some'); + browserTrigger(link, 'click'); + expectRewriteTo($browser, 'http://host.com/base/index.html#!/link'); + } + ); + }); + + + it('should replace current hash fragment when link begins with "#" history disabled', function() { + configureService('#link', true, false, true); + inject( + initBrowser(), + initLocation(), + function($browser, $location) { + // Initialize browser URL + $location.path('/some'); + $location.hash('foo'); + browserTrigger(link, 'click'); + expect($location.hash()).toBe('link'); + expectRewriteTo($browser, 'http://host.com/base/index.html#!/some#link'); + } + ); + }); + + // don't run next tests on IE<9, as browserTrigger does not simulate pressed keys if (!(msie < 9)) {