From 24f706e558a9ea72bc3687f6db66ca780a557407 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Tue, 5 Aug 2014 15:24:33 -0400 Subject: [PATCH 1/3] revert fix($location): fix and test html5Mode url-parsing algorithm for legacy browsers This reverts commit 49e7c32bb45ce3984df6768ba7b2f6a723a4ebe7. --- src/ng/location.js | 21 +++++++++---------- test/ng/locationSpec.js | 45 ----------------------------------------- 2 files changed, 10 insertions(+), 56 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index a73063bd5473..2ec139f8008f 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -663,34 +663,33 @@ function $LocationProvider(){ if (IGNORE_URI_REGEXP.test(absHref)) return; // 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 - var href = elm.attr('href') || elm.attr('xlink:href'); + // 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('://') < 0) { // Ignore absolute URLs - var prefix = '#' + hashPrefix; + if (href.indexOf('://' == -1)) { // Ignore absolute URLs if (href[0] == '/') { // absolute path - replace old path - absHref = appBase + prefix + href; + absHref = serverBase(absHref) + href; } else if (href[0] == '#') { // local anchor - absHref = appBase + prefix + ($location.path() || '/') + href; + absHref = serverBase(absHref) + $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= 9) { From d3221a8b1ff52826dae2d43b97f9c4f05b63748b Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Tue, 5 Aug 2014 15:26:16 -0400 Subject: [PATCH 2/3] revert fix($location): make legacy browsers behave like modern ones in html5Mode This reverts commit 3f047704c70a957596371fec554d3e1fb066a29d. Conflicts: src/ng/location.js --- src/ng/location.js | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index 2ec139f8008f..48849672e82f 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -265,16 +265,6 @@ 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; - }; - } @@ -662,38 +652,6 @@ function $LocationProvider(){ // Ignore when url is started with javascript: or mailto: if (IGNORE_URI_REGEXP.test(absHref)) return; - // 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 if (href[0] == '#') { - // local anchor - absHref = serverBase(absHref) + $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 Date: Wed, 6 Aug 2014 10:38:44 -0400 Subject: [PATCH 3/3] fix($location): don't rewrite hash-fragment links in same document $location will no longer rewrite hash fragment links in the same document (specified via href="#hash..."). Closes #8478 --- src/ng/location.js | 7 +++++++ test/ng/locationSpec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/ng/location.js b/src/ng/location.js index 48849672e82f..6ae4a495a324 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -641,6 +641,13 @@ function $LocationProvider(){ if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return; } + var href = elm.attr('href') || elm.attr('xlink:href'); + + if (href && href.indexOf('#' + hashPrefix + '/') !== 0 && href[0] === '#') { + // Don't rewrite hash-fragment links in the same document. + return; + } + var absHref = elm.prop('href'); if (isObject(absHref) && absHref.toString() === '[object SVGAnimatedString]') { diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 2a88f1a0c589..d52b364e7cef 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -1164,6 +1164,32 @@ describe('$location', function() { }); + it('should not rewrite when clicking on relative hash fragments', function() { + configureService('#foo', true, true, true); + inject( + initBrowser(), + initLocation(), + function($browser) { + browserTrigger(link, 'click'); + expectNoRewrite($browser); + } + ); + }); + + + it('should not rewrite when clicking on relative hash fragments in old browser', function() { + configureService('#foo', true, false, true); + inject( + initBrowser(), + initLocation(), + function($browser) { + browserTrigger(link, 'click'); + expectNoRewrite($browser); + } + ); + }); + + // don't run next tests on IE<9, as browserTrigger does not simulate pressed keys if (!msie || msie >= 9) {