location.mjs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * @license Angular v20.1.0
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import * as i0 from '@angular/core';
  7. import { inject, Injectable, InjectionToken, DOCUMENT, Optional, Inject, ɵɵinject as __inject } from '@angular/core';
  8. import { Subject } from 'rxjs';
  9. let _DOM = null;
  10. function getDOM() {
  11. return _DOM;
  12. }
  13. function setRootDomAdapter(adapter) {
  14. _DOM ??= adapter;
  15. }
  16. /**
  17. * Provides DOM operations in an environment-agnostic way.
  18. *
  19. * @security Tread carefully! Interacting with the DOM directly is dangerous and
  20. * can introduce XSS risks.
  21. */
  22. class DomAdapter {
  23. }
  24. /**
  25. * This class should not be used directly by an application developer. Instead, use
  26. * {@link Location}.
  27. *
  28. * `PlatformLocation` encapsulates all calls to DOM APIs, which allows the Router to be
  29. * platform-agnostic.
  30. * This means that we can have different implementation of `PlatformLocation` for the different
  31. * platforms that Angular supports. For example, `@angular/platform-browser` provides an
  32. * implementation specific to the browser environment, while `@angular/platform-server` provides
  33. * one suitable for use with server-side rendering.
  34. *
  35. * The `PlatformLocation` class is used directly by all implementations of {@link LocationStrategy}
  36. * when they need to interact with the DOM APIs like pushState, popState, etc.
  37. *
  38. * {@link LocationStrategy} in turn is used by the {@link Location} service which is used directly
  39. * by the {@link /api/router/Router Router} in order to navigate between routes. Since all interactions between
  40. * {@link /api/router/Router Router} /
  41. * {@link Location} / {@link LocationStrategy} and DOM APIs flow through the `PlatformLocation`
  42. * class, they are all platform-agnostic.
  43. *
  44. * @publicApi
  45. */
  46. class PlatformLocation {
  47. historyGo(relativePosition) {
  48. throw new Error(ngDevMode ? 'Not implemented' : '');
  49. }
  50. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: PlatformLocation, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  51. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: PlatformLocation, providedIn: 'platform', useFactory: () => inject(BrowserPlatformLocation) });
  52. }
  53. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: PlatformLocation, decorators: [{
  54. type: Injectable,
  55. args: [{ providedIn: 'platform', useFactory: () => inject(BrowserPlatformLocation) }]
  56. }] });
  57. /**
  58. * @description
  59. * Indicates when a location is initialized.
  60. *
  61. * @publicApi
  62. */
  63. const LOCATION_INITIALIZED = new InjectionToken(ngDevMode ? 'Location Initialized' : '');
  64. /**
  65. * `PlatformLocation` encapsulates all of the direct calls to platform APIs.
  66. * This class should not be used directly by an application developer. Instead, use
  67. * {@link Location}.
  68. *
  69. * @publicApi
  70. */
  71. class BrowserPlatformLocation extends PlatformLocation {
  72. _location;
  73. _history;
  74. _doc = inject(DOCUMENT);
  75. constructor() {
  76. super();
  77. this._location = window.location;
  78. this._history = window.history;
  79. }
  80. getBaseHrefFromDOM() {
  81. return getDOM().getBaseHref(this._doc);
  82. }
  83. onPopState(fn) {
  84. const window = getDOM().getGlobalEventTarget(this._doc, 'window');
  85. window.addEventListener('popstate', fn, false);
  86. return () => window.removeEventListener('popstate', fn);
  87. }
  88. onHashChange(fn) {
  89. const window = getDOM().getGlobalEventTarget(this._doc, 'window');
  90. window.addEventListener('hashchange', fn, false);
  91. return () => window.removeEventListener('hashchange', fn);
  92. }
  93. get href() {
  94. return this._location.href;
  95. }
  96. get protocol() {
  97. return this._location.protocol;
  98. }
  99. get hostname() {
  100. return this._location.hostname;
  101. }
  102. get port() {
  103. return this._location.port;
  104. }
  105. get pathname() {
  106. return this._location.pathname;
  107. }
  108. get search() {
  109. return this._location.search;
  110. }
  111. get hash() {
  112. return this._location.hash;
  113. }
  114. set pathname(newPath) {
  115. this._location.pathname = newPath;
  116. }
  117. pushState(state, title, url) {
  118. this._history.pushState(state, title, url);
  119. }
  120. replaceState(state, title, url) {
  121. this._history.replaceState(state, title, url);
  122. }
  123. forward() {
  124. this._history.forward();
  125. }
  126. back() {
  127. this._history.back();
  128. }
  129. historyGo(relativePosition = 0) {
  130. this._history.go(relativePosition);
  131. }
  132. getState() {
  133. return this._history.state;
  134. }
  135. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: BrowserPlatformLocation, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  136. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: BrowserPlatformLocation, providedIn: 'platform', useFactory: () => new BrowserPlatformLocation() });
  137. }
  138. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: BrowserPlatformLocation, decorators: [{
  139. type: Injectable,
  140. args: [{
  141. providedIn: 'platform',
  142. useFactory: () => new BrowserPlatformLocation(),
  143. }]
  144. }], ctorParameters: () => [] });
  145. /**
  146. * Joins two parts of a URL with a slash if needed.
  147. *
  148. * @param start URL string
  149. * @param end URL string
  150. *
  151. *
  152. * @returns The joined URL string.
  153. */
  154. function joinWithSlash(start, end) {
  155. // If `start` is an empty string, return `end` as the result.
  156. if (!start)
  157. return end;
  158. // If `end` is an empty string, return `start` as the result.
  159. if (!end)
  160. return start;
  161. // If `start` ends with a slash, remove the leading slash from `end`.
  162. if (start.endsWith('/')) {
  163. return end.startsWith('/') ? start + end.slice(1) : start + end;
  164. }
  165. // If `start` doesn't end with a slash, add one if `end` doesn't start with a slash.
  166. return end.startsWith('/') ? start + end : `${start}/${end}`;
  167. }
  168. /**
  169. * Removes a trailing slash from a URL string if needed.
  170. * Looks for the first occurrence of either `#`, `?`, or the end of the
  171. * line as `/` characters and removes the trailing slash if one exists.
  172. *
  173. * @param url URL string.
  174. *
  175. * @returns The URL string, modified if needed.
  176. */
  177. function stripTrailingSlash(url) {
  178. // Find the index of the first occurrence of `#`, `?`, or the end of the string.
  179. // This marks the start of the query string, fragment, or the end of the URL path.
  180. const pathEndIdx = url.search(/#|\?|$/);
  181. // Check if the character before `pathEndIdx` is a trailing slash.
  182. // If it is, remove the trailing slash and return the modified URL.
  183. // Otherwise, return the URL as is.
  184. return url[pathEndIdx - 1] === '/' ? url.slice(0, pathEndIdx - 1) + url.slice(pathEndIdx) : url;
  185. }
  186. /**
  187. * Normalizes URL parameters by prepending with `?` if needed.
  188. *
  189. * @param params String of URL parameters.
  190. *
  191. * @returns The normalized URL parameters string.
  192. */
  193. function normalizeQueryParams(params) {
  194. return params && params[0] !== '?' ? `?${params}` : params;
  195. }
  196. /**
  197. * Enables the `Location` service to read route state from the browser's URL.
  198. * Angular provides two strategies:
  199. * `HashLocationStrategy` and `PathLocationStrategy`.
  200. *
  201. * Applications should use the `Router` or `Location` services to
  202. * interact with application route state.
  203. *
  204. * For instance, `HashLocationStrategy` produces URLs like
  205. * <code class="no-auto-link">http://example.com/#/foo</code>,
  206. * and `PathLocationStrategy` produces
  207. * <code class="no-auto-link">http://example.com/foo</code> as an equivalent URL.
  208. *
  209. * See these two classes for more.
  210. *
  211. * @publicApi
  212. */
  213. class LocationStrategy {
  214. historyGo(relativePosition) {
  215. throw new Error(ngDevMode ? 'Not implemented' : '');
  216. }
  217. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: LocationStrategy, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  218. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: LocationStrategy, providedIn: 'root', useFactory: () => inject(PathLocationStrategy) });
  219. }
  220. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: LocationStrategy, decorators: [{
  221. type: Injectable,
  222. args: [{ providedIn: 'root', useFactory: () => inject(PathLocationStrategy) }]
  223. }] });
  224. /**
  225. * A predefined DI token for the base href
  226. * to be used with the `PathLocationStrategy`.
  227. * The base href is the URL prefix that should be preserved when generating
  228. * and recognizing URLs.
  229. *
  230. * @usageNotes
  231. *
  232. * The following example shows how to use this token to configure the root app injector
  233. * with a base href value, so that the DI framework can supply the dependency anywhere in the app.
  234. *
  235. * ```ts
  236. * import {NgModule} from '@angular/core';
  237. * import {APP_BASE_HREF} from '@angular/common';
  238. *
  239. * @NgModule({
  240. * providers: [{provide: APP_BASE_HREF, useValue: '/my/app'}]
  241. * })
  242. * class AppModule {}
  243. * ```
  244. *
  245. * @publicApi
  246. */
  247. const APP_BASE_HREF = new InjectionToken(ngDevMode ? 'appBaseHref' : '');
  248. /**
  249. * @description
  250. * A {@link LocationStrategy} used to configure the {@link Location} service to
  251. * represent its state in the
  252. * [path](https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax) of the
  253. * browser's URL.
  254. *
  255. * If you're using `PathLocationStrategy`, you may provide a {@link APP_BASE_HREF}
  256. * or add a `<base href>` element to the document to override the default.
  257. *
  258. * For instance, if you provide an `APP_BASE_HREF` of `'/my/app/'` and call
  259. * `location.go('/foo')`, the browser's URL will become
  260. * `example.com/my/app/foo`. To ensure all relative URIs resolve correctly,
  261. * the `<base href>` and/or `APP_BASE_HREF` should end with a `/`.
  262. *
  263. * Similarly, if you add `<base href='/my/app/'/>` to the document and call
  264. * `location.go('/foo')`, the browser's URL will become
  265. * `example.com/my/app/foo`.
  266. *
  267. * Note that when using `PathLocationStrategy`, neither the query nor
  268. * the fragment in the `<base href>` will be preserved, as outlined
  269. * by the [RFC](https://tools.ietf.org/html/rfc3986#section-5.2.2).
  270. *
  271. * @usageNotes
  272. *
  273. * ### Example
  274. *
  275. * {@example common/location/ts/path_location_component.ts region='LocationComponent'}
  276. *
  277. * @publicApi
  278. */
  279. class PathLocationStrategy extends LocationStrategy {
  280. _platformLocation;
  281. _baseHref;
  282. _removeListenerFns = [];
  283. constructor(_platformLocation, href) {
  284. super();
  285. this._platformLocation = _platformLocation;
  286. this._baseHref =
  287. href ??
  288. this._platformLocation.getBaseHrefFromDOM() ??
  289. inject(DOCUMENT).location?.origin ??
  290. '';
  291. }
  292. /** @docs-private */
  293. ngOnDestroy() {
  294. while (this._removeListenerFns.length) {
  295. this._removeListenerFns.pop()();
  296. }
  297. }
  298. onPopState(fn) {
  299. this._removeListenerFns.push(this._platformLocation.onPopState(fn), this._platformLocation.onHashChange(fn));
  300. }
  301. getBaseHref() {
  302. return this._baseHref;
  303. }
  304. prepareExternalUrl(internal) {
  305. return joinWithSlash(this._baseHref, internal);
  306. }
  307. path(includeHash = false) {
  308. const pathname = this._platformLocation.pathname + normalizeQueryParams(this._platformLocation.search);
  309. const hash = this._platformLocation.hash;
  310. return hash && includeHash ? `${pathname}${hash}` : pathname;
  311. }
  312. pushState(state, title, url, queryParams) {
  313. const externalUrl = this.prepareExternalUrl(url + normalizeQueryParams(queryParams));
  314. this._platformLocation.pushState(state, title, externalUrl);
  315. }
  316. replaceState(state, title, url, queryParams) {
  317. const externalUrl = this.prepareExternalUrl(url + normalizeQueryParams(queryParams));
  318. this._platformLocation.replaceState(state, title, externalUrl);
  319. }
  320. forward() {
  321. this._platformLocation.forward();
  322. }
  323. back() {
  324. this._platformLocation.back();
  325. }
  326. getState() {
  327. return this._platformLocation.getState();
  328. }
  329. historyGo(relativePosition = 0) {
  330. this._platformLocation.historyGo?.(relativePosition);
  331. }
  332. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: PathLocationStrategy, deps: [{ token: PlatformLocation }, { token: APP_BASE_HREF, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
  333. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: PathLocationStrategy, providedIn: 'root' });
  334. }
  335. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: PathLocationStrategy, decorators: [{
  336. type: Injectable,
  337. args: [{ providedIn: 'root' }]
  338. }], ctorParameters: () => [{ type: PlatformLocation }, { type: undefined, decorators: [{
  339. type: Optional
  340. }, {
  341. type: Inject,
  342. args: [APP_BASE_HREF]
  343. }] }] });
  344. /**
  345. * @description
  346. *
  347. * A service that applications can use to interact with a browser's URL.
  348. *
  349. * Depending on the `LocationStrategy` used, `Location` persists
  350. * to the URL's path or the URL's hash segment.
  351. *
  352. * @usageNotes
  353. *
  354. * It's better to use the `Router.navigate()` service to trigger route changes. Use
  355. * `Location` only if you need to interact with or create normalized URLs outside of
  356. * routing.
  357. *
  358. * `Location` is responsible for normalizing the URL against the application's base href.
  359. * A normalized URL is absolute from the URL host, includes the application's base href, and has no
  360. * trailing slash:
  361. * - `/my/app/user/123` is normalized
  362. * - `my/app/user/123` **is not** normalized
  363. * - `/my/app/user/123/` **is not** normalized
  364. *
  365. * ### Example
  366. *
  367. * {@example common/location/ts/path_location_component.ts region='LocationComponent'}
  368. *
  369. * @publicApi
  370. */
  371. class Location {
  372. /** @internal */
  373. _subject = new Subject();
  374. /** @internal */
  375. _basePath;
  376. /** @internal */
  377. _locationStrategy;
  378. /** @internal */
  379. _urlChangeListeners = [];
  380. /** @internal */
  381. _urlChangeSubscription = null;
  382. constructor(locationStrategy) {
  383. this._locationStrategy = locationStrategy;
  384. const baseHref = this._locationStrategy.getBaseHref();
  385. // Note: This class's interaction with base HREF does not fully follow the rules
  386. // outlined in the spec https://www.freesoft.org/CIE/RFC/1808/18.htm.
  387. // Instead of trying to fix individual bugs with more and more code, we should
  388. // investigate using the URL constructor and providing the base as a second
  389. // argument.
  390. // https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#parameters
  391. this._basePath = _stripOrigin(stripTrailingSlash(_stripIndexHtml(baseHref)));
  392. this._locationStrategy.onPopState((ev) => {
  393. this._subject.next({
  394. 'url': this.path(true),
  395. 'pop': true,
  396. 'state': ev.state,
  397. 'type': ev.type,
  398. });
  399. });
  400. }
  401. /** @docs-private */
  402. ngOnDestroy() {
  403. this._urlChangeSubscription?.unsubscribe();
  404. this._urlChangeListeners = [];
  405. }
  406. /**
  407. * Normalizes the URL path for this location.
  408. *
  409. * @param includeHash True to include an anchor fragment in the path.
  410. *
  411. * @returns The normalized URL path.
  412. */
  413. // TODO: vsavkin. Remove the boolean flag and always include hash once the deprecated router is
  414. // removed.
  415. path(includeHash = false) {
  416. return this.normalize(this._locationStrategy.path(includeHash));
  417. }
  418. /**
  419. * Reports the current state of the location history.
  420. * @returns The current value of the `history.state` object.
  421. */
  422. getState() {
  423. return this._locationStrategy.getState();
  424. }
  425. /**
  426. * Normalizes the given path and compares to the current normalized path.
  427. *
  428. * @param path The given URL path.
  429. * @param query Query parameters.
  430. *
  431. * @returns True if the given URL path is equal to the current normalized path, false
  432. * otherwise.
  433. */
  434. isCurrentPathEqualTo(path, query = '') {
  435. return this.path() == this.normalize(path + normalizeQueryParams(query));
  436. }
  437. /**
  438. * Normalizes a URL path by stripping any trailing slashes.
  439. *
  440. * @param url String representing a URL.
  441. *
  442. * @returns The normalized URL string.
  443. */
  444. normalize(url) {
  445. return Location.stripTrailingSlash(_stripBasePath(this._basePath, _stripIndexHtml(url)));
  446. }
  447. /**
  448. * Normalizes an external URL path.
  449. * If the given URL doesn't begin with a leading slash (`'/'`), adds one
  450. * before normalizing. Adds a hash if `HashLocationStrategy` is
  451. * in use, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use.
  452. *
  453. * @param url String representing a URL.
  454. *
  455. * @returns A normalized platform-specific URL.
  456. */
  457. prepareExternalUrl(url) {
  458. if (url && url[0] !== '/') {
  459. url = '/' + url;
  460. }
  461. return this._locationStrategy.prepareExternalUrl(url);
  462. }
  463. // TODO: rename this method to pushState
  464. /**
  465. * Changes the browser's URL to a normalized version of a given URL, and pushes a
  466. * new item onto the platform's history.
  467. *
  468. * @param path URL path to normalize.
  469. * @param query Query parameters.
  470. * @param state Location history state.
  471. *
  472. */
  473. go(path, query = '', state = null) {
  474. this._locationStrategy.pushState(state, '', path, query);
  475. this._notifyUrlChangeListeners(this.prepareExternalUrl(path + normalizeQueryParams(query)), state);
  476. }
  477. /**
  478. * Changes the browser's URL to a normalized version of the given URL, and replaces
  479. * the top item on the platform's history stack.
  480. *
  481. * @param path URL path to normalize.
  482. * @param query Query parameters.
  483. * @param state Location history state.
  484. */
  485. replaceState(path, query = '', state = null) {
  486. this._locationStrategy.replaceState(state, '', path, query);
  487. this._notifyUrlChangeListeners(this.prepareExternalUrl(path + normalizeQueryParams(query)), state);
  488. }
  489. /**
  490. * Navigates forward in the platform's history.
  491. */
  492. forward() {
  493. this._locationStrategy.forward();
  494. }
  495. /**
  496. * Navigates back in the platform's history.
  497. */
  498. back() {
  499. this._locationStrategy.back();
  500. }
  501. /**
  502. * Navigate to a specific page from session history, identified by its relative position to the
  503. * current page.
  504. *
  505. * @param relativePosition Position of the target page in the history relative to the current
  506. * page.
  507. * A negative value moves backwards, a positive value moves forwards, e.g. `location.historyGo(2)`
  508. * moves forward two pages and `location.historyGo(-2)` moves back two pages. When we try to go
  509. * beyond what's stored in the history session, we stay in the current page. Same behaviour occurs
  510. * when `relativePosition` equals 0.
  511. * @see https://developer.mozilla.org/en-US/docs/Web/API/History_API#Moving_to_a_specific_point_in_history
  512. */
  513. historyGo(relativePosition = 0) {
  514. this._locationStrategy.historyGo?.(relativePosition);
  515. }
  516. /**
  517. * Registers a URL change listener. Use to catch updates performed by the Angular
  518. * framework that are not detectible through "popstate" or "hashchange" events.
  519. *
  520. * @param fn The change handler function, which take a URL and a location history state.
  521. * @returns A function that, when executed, unregisters a URL change listener.
  522. */
  523. onUrlChange(fn) {
  524. this._urlChangeListeners.push(fn);
  525. this._urlChangeSubscription ??= this.subscribe((v) => {
  526. this._notifyUrlChangeListeners(v.url, v.state);
  527. });
  528. return () => {
  529. const fnIndex = this._urlChangeListeners.indexOf(fn);
  530. this._urlChangeListeners.splice(fnIndex, 1);
  531. if (this._urlChangeListeners.length === 0) {
  532. this._urlChangeSubscription?.unsubscribe();
  533. this._urlChangeSubscription = null;
  534. }
  535. };
  536. }
  537. /** @internal */
  538. _notifyUrlChangeListeners(url = '', state) {
  539. this._urlChangeListeners.forEach((fn) => fn(url, state));
  540. }
  541. /**
  542. * Subscribes to the platform's `popState` events.
  543. *
  544. * Note: `Location.go()` does not trigger the `popState` event in the browser. Use
  545. * `Location.onUrlChange()` to subscribe to URL changes instead.
  546. *
  547. * @param value Event that is triggered when the state history changes.
  548. * @param exception The exception to throw.
  549. *
  550. * @see [onpopstate](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate)
  551. *
  552. * @returns Subscribed events.
  553. */
  554. subscribe(onNext, onThrow, onReturn) {
  555. return this._subject.subscribe({
  556. next: onNext,
  557. error: onThrow ?? undefined,
  558. complete: onReturn ?? undefined,
  559. });
  560. }
  561. /**
  562. * Normalizes URL parameters by prepending with `?` if needed.
  563. *
  564. * @param params String of URL parameters.
  565. *
  566. * @returns The normalized URL parameters string.
  567. */
  568. static normalizeQueryParams = normalizeQueryParams;
  569. /**
  570. * Joins two parts of a URL with a slash if needed.
  571. *
  572. * @param start URL string
  573. * @param end URL string
  574. *
  575. *
  576. * @returns The joined URL string.
  577. */
  578. static joinWithSlash = joinWithSlash;
  579. /**
  580. * Removes a trailing slash from a URL string if needed.
  581. * Looks for the first occurrence of either `#`, `?`, or the end of the
  582. * line as `/` characters and removes the trailing slash if one exists.
  583. *
  584. * @param url URL string.
  585. *
  586. * @returns The URL string, modified if needed.
  587. */
  588. static stripTrailingSlash = stripTrailingSlash;
  589. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: Location, deps: [{ token: LocationStrategy }], target: i0.ɵɵFactoryTarget.Injectable });
  590. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: Location, providedIn: 'root', useFactory: createLocation });
  591. }
  592. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: Location, decorators: [{
  593. type: Injectable,
  594. args: [{
  595. providedIn: 'root',
  596. // See #23917
  597. useFactory: createLocation,
  598. }]
  599. }], ctorParameters: () => [{ type: LocationStrategy }] });
  600. function createLocation() {
  601. return new Location(__inject(LocationStrategy));
  602. }
  603. function _stripBasePath(basePath, url) {
  604. if (!basePath || !url.startsWith(basePath)) {
  605. return url;
  606. }
  607. const strippedUrl = url.substring(basePath.length);
  608. if (strippedUrl === '' || ['/', ';', '?', '#'].includes(strippedUrl[0])) {
  609. return strippedUrl;
  610. }
  611. return url;
  612. }
  613. function _stripIndexHtml(url) {
  614. return url.replace(/\/index.html$/, '');
  615. }
  616. function _stripOrigin(baseHref) {
  617. // DO NOT REFACTOR! Previously, this check looked like this:
  618. // `/^(https?:)?\/\//.test(baseHref)`, but that resulted in
  619. // syntactically incorrect code after Closure Compiler minification.
  620. // This was likely caused by a bug in Closure Compiler, but
  621. // for now, the check is rewritten to use `new RegExp` instead.
  622. const isAbsoluteUrl = new RegExp('^(https?:)?//').test(baseHref);
  623. if (isAbsoluteUrl) {
  624. const [, pathname] = baseHref.split(/\/\/[^\/]+/);
  625. return pathname;
  626. }
  627. return baseHref;
  628. }
  629. export { APP_BASE_HREF, BrowserPlatformLocation, DomAdapter, LOCATION_INITIALIZED, Location, LocationStrategy, PathLocationStrategy, PlatformLocation, getDOM, joinWithSlash, normalizeQueryParams, setRootDomAdapter };
  630. //# sourceMappingURL=location.mjs.map