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

Commit

Permalink
fix($browser): normalize inputted URLs
Browse files Browse the repository at this point in the history
Fixes #16100
  • Loading branch information
jbedard committed Jun 24, 2018
1 parent 21263c9 commit d3a79fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/ng/browser.js
Expand Up @@ -131,6 +131,16 @@ function Browser(window, document, $log, $sniffer) {
// setter
if (url) {
var sameState = lastHistoryState === state;
var trailingSlashRe = /\/$/;
var hasTrailingSlash = trailingSlashRe.test(url);

// Normalize the inputted URL ...
url = urlResolve(url).href;

// ... but keep the (lack of) trailing '/' consistent
if (!hasTrailingSlash && trailingSlashRe.test(url)) {
url = url.replace(trailingSlashRe, "");
}

// Don't change anything if previous and current URLs and states match. This also prevents
// IE<10 from getting into redirect loop when in LocationHashbangInHtml5Url mode.
Expand Down
26 changes: 25 additions & 1 deletion test/ng/browserSpecs.js
Expand Up @@ -568,6 +568,30 @@ describe('browser', function() {
expect(replaceState).not.toHaveBeenCalled();
expect(locationReplace).not.toHaveBeenCalled();
});

it('should not do pushState with a URL only different in encoding', function() {
//A URL from something such as window.location.href
browser.url('http://server/abc?q=%27');

pushState.calls.reset();
replaceState.calls.reset();
locationReplace.calls.reset();

//A prettier URL from something such as $location
browser.url('http://server/abc?q=\'');
expect(pushState).not.toHaveBeenCalled();
expect(replaceState).not.toHaveBeenCalled();
expect(locationReplace).not.toHaveBeenCalled();
});

it('should not do pushState on $$checkUrlChange() with a URL only different in encoding', function() {
var callback = jasmine.createSpy('onUrlChange');
browser.onUrlChange(callback);
browser.url('http://server/abc?q=\'');

browser.$$checkUrlChange();
expect(callback).not.toHaveBeenCalled();
});
};
}
});
Expand Down Expand Up @@ -1014,7 +1038,7 @@ describe('browser', function() {
it('should not interfere with legacy browser url replace behavior', function() {
inject(function($rootScope) {
var current = fakeWindow.location.href;
var newUrl = 'notyet';
var newUrl = 'http://notyet';
sniffer.history = false;
expect(historyEntriesLength).toBe(1);
browser.url(newUrl, true);
Expand Down

0 comments on commit d3a79fb

Please sign in to comment.