UNPKG

@angular/common

Version:

Angular - commonly needed directives and services

1 lines 10.6 kB
{"version":3,"file":"_platform_location-chunk.mjs","sources":["../../../../../k8-fastbuild-ST-fdfa778d11ba/bin/packages/common/src/dom_adapter.ts","../../../../../k8-fastbuild-ST-fdfa778d11ba/bin/packages/common/src/location/platform_location.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nlet _DOM: DomAdapter = null!;\n\nexport function getDOM(): DomAdapter {\n return _DOM;\n}\n\nexport function setRootDomAdapter(adapter: DomAdapter) {\n _DOM ??= adapter;\n}\n\n/**\n * Provides DOM operations in an environment-agnostic way.\n *\n * @security Tread carefully! Interacting with the DOM directly is dangerous and\n * can introduce XSS risks.\n */\nexport abstract class DomAdapter {\n // Needs Domino-friendly test utility\n abstract dispatchEvent(el: any, evt: any): any;\n abstract readonly supportsDOMEvents: boolean;\n\n // Used by Meta\n abstract remove(el: any): void;\n abstract createElement(tagName: any, doc?: any): HTMLElement;\n abstract createHtmlDocument(): Document;\n abstract getDefaultDocument(): Document;\n\n // Used by By.css\n abstract isElementNode(node: any): boolean;\n\n // Used by Testability\n abstract isShadowRoot(node: any): boolean;\n\n // Used by KeyEventsPlugin\n abstract onAndCancel(el: any, evt: any, listener: any, options?: any): Function;\n\n // Used by PlatformLocation and ServerEventManagerPlugin\n abstract getGlobalEventTarget(doc: Document, target: string): any;\n\n // Used by PlatformLocation\n abstract getBaseHref(doc: Document): string | null;\n abstract resetBaseElement(): void;\n\n // TODO: remove dependency in DefaultValueAccessor\n abstract getUserAgent(): string;\n\n // Used in the legacy @angular/http package which has some usage in g3.\n abstract getCookie(name: string): string | null;\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {inject, Injectable, InjectionToken, DOCUMENT} from '@angular/core';\n\nimport {getDOM} from '../dom_adapter';\n\n/**\n * This class should not be used directly by an application developer. Instead, use\n * {@link Location}.\n *\n * `PlatformLocation` encapsulates all calls to DOM APIs, which allows the Router to be\n * platform-agnostic.\n * This means that we can have different implementation of `PlatformLocation` for the different\n * platforms that Angular supports. For example, `@angular/platform-browser` provides an\n * implementation specific to the browser environment, while `@angular/platform-server` provides\n * one suitable for use with server-side rendering.\n *\n * The `PlatformLocation` class is used directly by all implementations of {@link LocationStrategy}\n * when they need to interact with the DOM APIs like pushState, popState, etc.\n *\n * {@link LocationStrategy} in turn is used by the {@link Location} service which is used directly\n * by the {@link /api/router/Router Router} in order to navigate between routes. Since all interactions between\n * {@link /api/router/Router Router} /\n * {@link Location} / {@link LocationStrategy} and DOM APIs flow through the `PlatformLocation`\n * class, they are all platform-agnostic.\n *\n * @publicApi\n */\n@Injectable({providedIn: 'platform', useFactory: () => inject(BrowserPlatformLocation)})\nexport abstract class PlatformLocation {\n abstract getBaseHrefFromDOM(): string;\n abstract getState(): unknown;\n /**\n * Returns a function that, when executed, removes the `popstate` event handler.\n */\n abstract onPopState(fn: LocationChangeListener): VoidFunction;\n /**\n * Returns a function that, when executed, removes the `hashchange` event handler.\n */\n abstract onHashChange(fn: LocationChangeListener): VoidFunction;\n\n abstract get href(): string;\n abstract get protocol(): string;\n abstract get hostname(): string;\n abstract get port(): string;\n abstract get pathname(): string;\n abstract get search(): string;\n abstract get hash(): string;\n\n abstract replaceState(state: any, title: string, url: string): void;\n\n abstract pushState(state: any, title: string, url: string): void;\n\n abstract forward(): void;\n\n abstract back(): void;\n\n historyGo?(relativePosition: number): void {\n throw new Error(ngDevMode ? 'Not implemented' : '');\n }\n}\n\n/**\n * @description\n * Indicates when a location is initialized.\n *\n * @publicApi\n */\nexport const LOCATION_INITIALIZED = new InjectionToken<Promise<any>>(\n typeof ngDevMode !== 'undefined' && ngDevMode ? 'Location Initialized' : '',\n);\n\n/**\n * @description\n * A serializable version of the event from `onPopState` or `onHashChange`\n *\n * @publicApi\n */\nexport interface LocationChangeEvent {\n type: string;\n state: any;\n}\n\n/**\n * @publicApi\n */\nexport interface LocationChangeListener {\n (event: LocationChangeEvent): any;\n}\n\n/**\n * `PlatformLocation` encapsulates all of the direct calls to platform APIs.\n * This class should not be used directly by an application developer. Instead, use\n * {@link Location}.\n *\n * @publicApi\n */\n@Injectable({\n providedIn: 'platform',\n useFactory: () => new BrowserPlatformLocation(),\n})\nexport class BrowserPlatformLocation extends PlatformLocation {\n private _location: Location;\n private _history: History;\n private _doc = inject(DOCUMENT);\n\n constructor() {\n super();\n this._location = window.location;\n this._history = window.history;\n }\n\n override getBaseHrefFromDOM(): string {\n return getDOM().getBaseHref(this._doc)!;\n }\n\n override onPopState(fn: LocationChangeListener): VoidFunction {\n const window = getDOM().getGlobalEventTarget(this._doc, 'window');\n window.addEventListener('popstate', fn, false);\n return () => window.removeEventListener('popstate', fn);\n }\n\n override onHashChange(fn: LocationChangeListener): VoidFunction {\n const window = getDOM().getGlobalEventTarget(this._doc, 'window');\n window.addEventListener('hashchange', fn, false);\n return () => window.removeEventListener('hashchange', fn);\n }\n\n override get href(): string {\n return this._location.href;\n }\n override get protocol(): string {\n return this._location.protocol;\n }\n override get hostname(): string {\n return this._location.hostname;\n }\n override get port(): string {\n return this._location.port;\n }\n override get pathname(): string {\n return this._location.pathname;\n }\n override get search(): string {\n return this._location.search;\n }\n override get hash(): string {\n return this._location.hash;\n }\n override set pathname(newPath: string) {\n this._location.pathname = newPath;\n }\n\n override pushState(state: any, title: string, url: string): void {\n this._history.pushState(state, title, url);\n }\n\n override replaceState(state: any, title: string, url: string): void {\n this._history.replaceState(state, title, url);\n }\n\n override forward(): void {\n this._history.forward();\n }\n\n override back(): void {\n this._history.back();\n }\n\n override historyGo(relativePosition: number = 0): void {\n this._history.go(relativePosition);\n }\n\n override getState(): unknown {\n return this._history.state;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;AAQA,IAAI,IAAI,GAAe,IAAK;SAEZ,MAA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