From e93b3d236092018e3468bcfafdbb2516b67b9584 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 27 May 2016 11:29:23 -0700 Subject: [PATCH] fix(Location): make Location#platformStrategy:LocationStrategy property private BREAKING CHANGE: Location#platformStrategy property was previously accidentaly exported as public If any application requires access to the current location strategy, it should be accessed via DI instead by injecting the LocationStrategy token. The likelyhood of anyone actually depending on this property is very low. --- .../@angular/common/src/location/location.ts | 22 +++++++++++-------- .../@angular/common/testing/location_mock.ts | 5 +++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/modules/@angular/common/src/location/location.ts b/modules/@angular/common/src/location/location.ts index b778ce7d8a..05156be179 100644 --- a/modules/@angular/common/src/location/location.ts +++ b/modules/@angular/common/src/location/location.ts @@ -52,10 +52,14 @@ export class Location { /** @internal */ _baseHref: string; - constructor(public platformStrategy: LocationStrategy) { - var browserBaseHref = this.platformStrategy.getBaseHref(); + /** @internal */ + _platformStrategy: LocationStrategy; + + constructor(platformStrategy: LocationStrategy) { + this._platformStrategy = platformStrategy; + var browserBaseHref = this._platformStrategy.getBaseHref(); this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref)); - this.platformStrategy.onPopState((ev) => { + this._platformStrategy.onPopState((ev) => { ObservableWrapper.callEmit(this._subject, {'url': this.path(), 'pop': true, 'type': ev.type}); }); } @@ -63,7 +67,7 @@ export class Location { /** * Returns the normalized URL path. */ - path(): string { return this.normalize(this.platformStrategy.path()); } + path(): string { return this.normalize(this._platformStrategy.path()); } /** * Normalizes the given path and compares to the current normalized path. @@ -90,7 +94,7 @@ export class Location { if (url.length > 0 && !url.startsWith('/')) { url = '/' + url; } - return this.platformStrategy.prepareExternalUrl(url); + return this._platformStrategy.prepareExternalUrl(url); } // TODO: rename this method to pushState @@ -99,7 +103,7 @@ export class Location { * new item onto the platform's history. */ go(path: string, query: string = ''): void { - this.platformStrategy.pushState(null, '', path, query); + this._platformStrategy.pushState(null, '', path, query); } /** @@ -107,18 +111,18 @@ export class Location { * the top item on the platform's history stack. */ replaceState(path: string, query: string = ''): void { - this.platformStrategy.replaceState(null, '', path, query); + this._platformStrategy.replaceState(null, '', path, query); } /** * Navigates forward in the platform's history. */ - forward(): void { this.platformStrategy.forward(); } + forward(): void { this._platformStrategy.forward(); } /** * Navigates back in the platform's history. */ - back(): void { this.platformStrategy.back(); } + back(): void { this._platformStrategy.back(); } /** * Subscribe to the platform's `popState` events. diff --git a/modules/@angular/common/testing/location_mock.ts b/modules/@angular/common/testing/location_mock.ts index e017caa733..0d2089bd28 100644 --- a/modules/@angular/common/testing/location_mock.ts +++ b/modules/@angular/common/testing/location_mock.ts @@ -1,6 +1,7 @@ import {Injectable, EventEmitter} from '@angular/core'; import {ObservableWrapper} from '../src/facade/async'; import {Location} from '../index'; +import {LocationStrategy} from "../src/location/location_strategy"; /** * A spy for {@link Location} that allows tests to fire simulated location events. @@ -16,6 +17,8 @@ export class SpyLocation implements Location { _subject: EventEmitter = new EventEmitter(); /** @internal */ _baseHref: string = ''; + /** @internal */ + _platformStrategy: LocationStrategy = null; setInitialPath(url: string) { this._history[this._historyIndex].path = url; } @@ -101,8 +104,6 @@ export class SpyLocation implements Location { return ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn); } - // TODO: remove these once Location is an interface, and can be implemented cleanly - platformStrategy: any = null; normalize(url: string): string { return null; } }