From cd4c90b6e90f0f2e269600e319400660c92e93ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Wenzel?= Date: Thu, 13 Apr 2017 16:56:52 +0200 Subject: [PATCH] fix(router): browser history state not empty on new entries in IE/Edge The router doesn't handle the different behaviour in IE/Edge for history state on new entries. This fix consolidates the behaviour with Chrome etc. Depending on PR aurelia/history-browser/history-state-consolidation. Closes aurelia/router#489. --- src/router.js | 8 ++++---- src/util.js | 16 ++++++++-------- test/router.spec.js | 6 ++++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/router.js b/src/router.js index 03844d09..0fb8abd6 100644 --- a/src/router.js +++ b/src/router.js @@ -230,7 +230,7 @@ export class Router { } this.isExplicitNavigation = true; - return this.history.navigate(_resolveUrl(fragment, this.baseUrl, this.history._hasPushState), options); + return this.history.navigate(_resolveUrl(fragment, this.baseUrl, this.history._usePushState), options); } /** @@ -285,7 +285,7 @@ export class Router { } let path = this._recognizer.generate(name, params); - let rootedPath = _createRootedPath(path, this.baseUrl, this.history._hasPushState, options.absolute); + let rootedPath = _createRootedPath(path, this.baseUrl, this.history._usePushState, options.absolute); return options.absolute ? `${this.history.getAbsoluteRoot()}${rootedPath}` : rootedPath; } @@ -437,9 +437,9 @@ export class Router { for (let i = 0, length = nav.length; i < length; i++) { let current = nav[i]; if (!current.config.href) { - current.href = _createRootedPath(current.relativeHref, this.baseUrl, this.history._hasPushState); + current.href = _createRootedPath(current.relativeHref, this.baseUrl, this.history._usePushState); } else { - current.href = _normalizeAbsolutePath(current.config.href, this.history._hasPushState); + current.href = _normalizeAbsolutePath(current.config.href, this.history._usePushState); } } } diff --git a/src/util.js b/src/util.js index f89993c6..d53333d2 100644 --- a/src/util.js +++ b/src/util.js @@ -1,16 +1,16 @@ -export function _normalizeAbsolutePath(path, hasPushState, absolute = false) { - if (!hasPushState && path[0] !== '#') { +export function _normalizeAbsolutePath(path, usePushState, absolute = false) { + if (!usePushState && path[0] !== '#') { path = '#' + path; } - if (hasPushState && absolute) { + if (usePushState && absolute) { path = path.substring(1, path.length); } return path; } -export function _createRootedPath(fragment, baseUrl, hasPushState, absolute) { +export function _createRootedPath(fragment, baseUrl, usePushState, absolute) { if (isAbsoluteUrl.test(fragment)) { return fragment; } @@ -31,15 +31,15 @@ export function _createRootedPath(fragment, baseUrl, hasPushState, absolute) { path = path.substring(0, path.length - 1); } - return _normalizeAbsolutePath(path + fragment, hasPushState, absolute); + return _normalizeAbsolutePath(path + fragment, usePushState, absolute); } -export function _resolveUrl(fragment, baseUrl, hasPushState) { +export function _resolveUrl(fragment, baseUrl, usePushState) { if (isRootedPath.test(fragment)) { - return _normalizeAbsolutePath(fragment, hasPushState); + return _normalizeAbsolutePath(fragment, usePushState); } - return _createRootedPath(fragment, baseUrl, hasPushState); + return _createRootedPath(fragment, baseUrl, usePushState); } export function _ensureArrayWithSingleRoutePerConfig(config) { diff --git a/test/router.spec.js b/test/router.spec.js index 3112d2a9..ca5d8aa6 100644 --- a/test/router.spec.js +++ b/test/router.spec.js @@ -92,7 +92,7 @@ describe('the router', () => { expect(child.generate('parent')).toBe('#/parent'); expect(child.generate('child')).toBe('#/child-router/child'); - router.history._hasPushState = true; + router.history._usePushState = true; expect(router.generate('parent')).toBe('/parent'); expect(child.generate('parent')).toBe('/parent'); @@ -136,7 +136,9 @@ describe('the router', () => { .then(() => { expect(child.generate('test', { id: 1 }, options)).toBe(`${absoluteRoot}#/test/1`); expect(child.generate('parent', { id: 2 }, options)).toBe(`${absoluteRoot}#/parent/2`); - router.history._hasPushState = true; + + router.history._usePushState = true; + expect(child.generate('test', { id: 1 }, options)).toBe(`${absoluteRoot}test/1`); expect(child.generate('parent', { id: 2 }, options)).toBe(`${absoluteRoot}parent/2`); done();