From 2bdf7126878c87474bb7588ce093d0a3c57b0026 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Fri, 1 Dec 2017 08:28:55 +0000 Subject: [PATCH] fix($location): always decode special chars in `$location.url(value)` The original fix for #16312 included changing how `$location.url(value)` decoded the special characters passed to it as a setter. This broke a number of use cases (mostly involving the ui-router). Further analysis appears to show that we can solve #16312, to prevent urls being rewritten with decoded values, without modifying the behaviour of `$location.url`. This commit reverts changes to `$location.url(value)` so that encoded chars will once again be decoded and passed to `$location.path(value)`. In particular it will convert encoded forward slashes, which changes how the path is updated, since e.g. `a/b/%2Fc%2Fd` will become `a/b/c/d`. While this is arguably not "correct", it appears that there are too many use cases relying upon this behaviour. --- src/ng/location.js | 2 +- test/ng/locationSpec.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index 68c7c529fa81..69b333633f61 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -422,7 +422,7 @@ var locationPrototype = { } var match = PATH_MATCH.exec(url); - if (match[1] || url === '') this.path((this.$$html5 ? decodeURI : decodeURIComponent)(match[1])); + if (match[1] || url === '') this.path(decodeURIComponent(match[1])); if (match[2] || match[1] || url === '') this.search(match[3] || ''); this.hash(match[5] || ''); diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index af17a1e3db76..3dbfcd3af22c 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -681,11 +681,11 @@ describe('$location', function() { expect(locationUrl.path()).toEqual('/foo:bar'); }); - it('url() should not decode non-component special characters in html5 mode', function() { + it('url() should decode non-component special characters in html5 mode', function() { var locationUrl = new LocationHtml5Url('http://host.com', 'http://host.com'); locationUrl.$$parse('http://host.com'); locationUrl.url('/foo%3Abar'); - expect(locationUrl.path()).toEqual('/foo%3Abar'); + expect(locationUrl.path()).toEqual('/foo:bar'); }); }); });