Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d3a79fb

Browse files
committed
fix($browser): normalize inputted URLs
Fixes #16100
1 parent 21263c9 commit d3a79fb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/ng/browser.js

+10
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ function Browser(window, document, $log, $sniffer) {
131131
// setter
132132
if (url) {
133133
var sameState = lastHistoryState === state;
134+
var trailingSlashRe = /\/$/;
135+
var hasTrailingSlash = trailingSlashRe.test(url);
136+
137+
// Normalize the inputted URL ...
138+
url = urlResolve(url).href;
139+
140+
// ... but keep the (lack of) trailing '/' consistent
141+
if (!hasTrailingSlash && trailingSlashRe.test(url)) {
142+
url = url.replace(trailingSlashRe, "");
143+
}
134144

135145
// Don't change anything if previous and current URLs and states match. This also prevents
136146
// IE<10 from getting into redirect loop when in LocationHashbangInHtml5Url mode.

test/ng/browserSpecs.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,30 @@ describe('browser', function() {
568568
expect(replaceState).not.toHaveBeenCalled();
569569
expect(locationReplace).not.toHaveBeenCalled();
570570
});
571+
572+
it('should not do pushState with a URL only different in encoding', function() {
573+
//A URL from something such as window.location.href
574+
browser.url('http://server/abc?q=%27');
575+
576+
pushState.calls.reset();
577+
replaceState.calls.reset();
578+
locationReplace.calls.reset();
579+
580+
//A prettier URL from something such as $location
581+
browser.url('http://server/abc?q=\'');
582+
expect(pushState).not.toHaveBeenCalled();
583+
expect(replaceState).not.toHaveBeenCalled();
584+
expect(locationReplace).not.toHaveBeenCalled();
585+
});
586+
587+
it('should not do pushState on $$checkUrlChange() with a URL only different in encoding', function() {
588+
var callback = jasmine.createSpy('onUrlChange');
589+
browser.onUrlChange(callback);
590+
browser.url('http://server/abc?q=\'');
591+
592+
browser.$$checkUrlChange();
593+
expect(callback).not.toHaveBeenCalled();
594+
});
571595
};
572596
}
573597
});
@@ -1014,7 +1038,7 @@ describe('browser', function() {
10141038
it('should not interfere with legacy browser url replace behavior', function() {
10151039
inject(function($rootScope) {
10161040
var current = fakeWindow.location.href;
1017-
var newUrl = 'notyet';
1041+
var newUrl = 'http://notyet';
10181042
sniffer.history = false;
10191043
expect(historyEntriesLength).toBe(1);
10201044
browser.url(newUrl, true);

0 commit comments

Comments
 (0)