common-upgrade.umd.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /**
  2. * @license Angular v8.1.0
  3. * (c) 2010-2019 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common'), require('@angular/core'), require('@angular/upgrade/static')) :
  8. typeof define === 'function' && define.amd ? define('@angular/common/upgrade', ['exports', '@angular/common', '@angular/core', '@angular/upgrade/static'], factory) :
  9. (global = global || self, factory((global.ng = global.ng || {}, global.ng.common = global.ng.common || {}, global.ng.common.upgrade = {}), global.ng.common, global.ng.core, global.ng.upgrade.static));
  10. }(this, function (exports, common, core, _static) { 'use strict';
  11. /*! *****************************************************************************
  12. Copyright (c) Microsoft Corporation. All rights reserved.
  13. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  14. this file except in compliance with the License. You may obtain a copy of the
  15. License at http://www.apache.org/licenses/LICENSE-2.0
  16. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  18. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  19. MERCHANTABLITY OR NON-INFRINGEMENT.
  20. See the Apache Version 2.0 License for specific language governing permissions
  21. and limitations under the License.
  22. ***************************************************************************** */
  23. var __assign = function() {
  24. __assign = Object.assign || function __assign(t) {
  25. for (var s, i = 1, n = arguments.length; i < n; i++) {
  26. s = arguments[i];
  27. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  28. }
  29. return t;
  30. };
  31. return __assign.apply(this, arguments);
  32. };
  33. function __decorate(decorators, target, key, desc) {
  34. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  35. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  36. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  37. return c > 3 && r && Object.defineProperty(target, key, r), r;
  38. }
  39. function __read(o, n) {
  40. var m = typeof Symbol === "function" && o[Symbol.iterator];
  41. if (!m) return o;
  42. var i = m.call(o), r, ar = [], e;
  43. try {
  44. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  45. }
  46. catch (error) { e = { error: error }; }
  47. finally {
  48. try {
  49. if (r && !r.done && (m = i["return"])) m.call(i);
  50. }
  51. finally { if (e) throw e.error; }
  52. }
  53. return ar;
  54. }
  55. /**
  56. * @license
  57. * Copyright Google Inc. All Rights Reserved.
  58. *
  59. * Use of this source code is governed by an MIT-style license that can be
  60. * found in the LICENSE file at https://angular.io/license
  61. */
  62. function deepEqual(a, b) {
  63. if (a === b) {
  64. return true;
  65. }
  66. else if (!a || !b) {
  67. return false;
  68. }
  69. else {
  70. try {
  71. if ((a.prototype !== b.prototype) || (Array.isArray(a) && Array.isArray(b))) {
  72. return false;
  73. }
  74. return JSON.stringify(a) === JSON.stringify(b);
  75. }
  76. catch (e) {
  77. return false;
  78. }
  79. }
  80. }
  81. function isAnchor(el) {
  82. return el.href !== undefined;
  83. }
  84. function isPromise(obj) {
  85. // allow any Promise/A+ compliant thenable.
  86. // It's up to the caller to ensure that obj.then conforms to the spec
  87. return !!obj && typeof obj.then === 'function';
  88. }
  89. /**
  90. * @license
  91. * Copyright Google Inc. All Rights Reserved.
  92. *
  93. * Use of this source code is governed by an MIT-style license that can be
  94. * found in the LICENSE file at https://angular.io/license
  95. */
  96. var PATH_MATCH = /^([^?#]*)(\?([^#]*))?(#(.*))?$/;
  97. var DOUBLE_SLASH_REGEX = /^\s*[\\/]{2,}/;
  98. var IGNORE_URI_REGEXP = /^\s*(javascript|mailto):/i;
  99. var DEFAULT_PORTS = {
  100. 'http:': 80,
  101. 'https:': 443,
  102. 'ftp:': 21
  103. };
  104. /**
  105. * Location service that provides a drop-in replacement for the $location service
  106. * provided in AngularJS.
  107. *
  108. * @see [Using the Angular Unified Location Service](guide/upgrade#using-the-unified-angular-location-service)
  109. *
  110. * @publicApi
  111. */
  112. var $locationShim = /** @class */ (function () {
  113. function $locationShim($injector, location, platformLocation, urlCodec, locationStrategy) {
  114. var _this = this;
  115. this.location = location;
  116. this.platformLocation = platformLocation;
  117. this.urlCodec = urlCodec;
  118. this.locationStrategy = locationStrategy;
  119. this.initalizing = true;
  120. this.updateBrowser = false;
  121. this.$$absUrl = '';
  122. this.$$url = '';
  123. this.$$host = '';
  124. this.$$replace = false;
  125. this.$$path = '';
  126. this.$$search = '';
  127. this.$$hash = '';
  128. this.$$changeListeners = [];
  129. this.cachedState = null;
  130. this.lastBrowserUrl = '';
  131. // This variable should be used *only* inside the cacheState function.
  132. this.lastCachedState = null;
  133. var initialUrl = this.browserUrl();
  134. var parsedUrl = this.urlCodec.parse(initialUrl);
  135. if (typeof parsedUrl === 'string') {
  136. throw 'Invalid URL';
  137. }
  138. this.$$protocol = parsedUrl.protocol;
  139. this.$$host = parsedUrl.hostname;
  140. this.$$port = parseInt(parsedUrl.port) || DEFAULT_PORTS[parsedUrl.protocol] || null;
  141. this.$$parseLinkUrl(initialUrl, initialUrl);
  142. this.cacheState();
  143. this.$$state = this.browserState();
  144. if (isPromise($injector)) {
  145. $injector.then(function ($i) { return _this.initialize($i); });
  146. }
  147. else {
  148. this.initialize($injector);
  149. }
  150. }
  151. $locationShim.prototype.initialize = function ($injector) {
  152. var _this = this;
  153. var $rootScope = $injector.get('$rootScope');
  154. var $rootElement = $injector.get('$rootElement');
  155. $rootElement.on('click', function (event) {
  156. if (event.ctrlKey || event.metaKey || event.shiftKey || event.which === 2 ||
  157. event.button === 2) {
  158. return;
  159. }
  160. var elm = event.target;
  161. // traverse the DOM up to find first A tag
  162. while (elm && elm.nodeName.toLowerCase() !== 'a') {
  163. // ignore rewriting if no A tag (reached root element, or no parent - removed from document)
  164. if (elm === $rootElement[0] || !(elm = elm.parentNode)) {
  165. return;
  166. }
  167. }
  168. if (!isAnchor(elm)) {
  169. return;
  170. }
  171. var absHref = elm.href;
  172. var relHref = elm.getAttribute('href');
  173. // Ignore when url is started with javascript: or mailto:
  174. if (IGNORE_URI_REGEXP.test(absHref)) {
  175. return;
  176. }
  177. if (absHref && !elm.getAttribute('target') && !event.isDefaultPrevented()) {
  178. if (_this.$$parseLinkUrl(absHref, relHref)) {
  179. // We do a preventDefault for all urls that are part of the AngularJS application,
  180. // in html5mode and also without, so that we are able to abort navigation without
  181. // getting double entries in the location history.
  182. event.preventDefault();
  183. // update location manually
  184. if (_this.absUrl() !== _this.browserUrl()) {
  185. $rootScope.$apply();
  186. }
  187. }
  188. }
  189. });
  190. this.location.onUrlChange(function (newUrl, newState) {
  191. var oldUrl = _this.absUrl();
  192. var oldState = _this.$$state;
  193. _this.$$parse(newUrl);
  194. newUrl = _this.absUrl();
  195. _this.$$state = newState;
  196. var defaultPrevented = $rootScope.$broadcast('$locationChangeStart', newUrl, oldUrl, newState, oldState)
  197. .defaultPrevented;
  198. // if the location was changed by a `$locationChangeStart` handler then stop
  199. // processing this location change
  200. if (_this.absUrl() !== newUrl)
  201. return;
  202. // If default was prevented, set back to old state. This is the state that was locally
  203. // cached in the $location service.
  204. if (defaultPrevented) {
  205. _this.$$parse(oldUrl);
  206. _this.state(oldState);
  207. _this.setBrowserUrlWithFallback(oldUrl, false, oldState);
  208. }
  209. else {
  210. _this.initalizing = false;
  211. $rootScope.$broadcast('$locationChangeSuccess', newUrl, oldUrl, newState, oldState);
  212. _this.resetBrowserUpdate();
  213. }
  214. if (!$rootScope.$$phase) {
  215. $rootScope.$digest();
  216. }
  217. });
  218. // update browser
  219. $rootScope.$watch(function () {
  220. if (_this.initalizing || _this.updateBrowser) {
  221. _this.updateBrowser = false;
  222. var oldUrl_1 = _this.browserUrl();
  223. var newUrl = _this.absUrl();
  224. var oldState_1 = _this.browserState();
  225. var currentReplace_1 = _this.$$replace;
  226. var urlOrStateChanged_1 = !_this.urlCodec.areEqual(oldUrl_1, newUrl) || oldState_1 !== _this.$$state;
  227. // Fire location changes one time to on initialization. This must be done on the
  228. // next tick (thus inside $evalAsync()) in order for listeners to be registered
  229. // before the event fires. Mimicing behavior from $locationWatch:
  230. // https://github.com/angular/angular.js/blob/master/src/ng/location.js#L983
  231. if (_this.initalizing || urlOrStateChanged_1) {
  232. _this.initalizing = false;
  233. $rootScope.$evalAsync(function () {
  234. // Get the new URL again since it could have changed due to async update
  235. var newUrl = _this.absUrl();
  236. var defaultPrevented = $rootScope
  237. .$broadcast('$locationChangeStart', newUrl, oldUrl_1, _this.$$state, oldState_1)
  238. .defaultPrevented;
  239. // if the location was changed by a `$locationChangeStart` handler then stop
  240. // processing this location change
  241. if (_this.absUrl() !== newUrl)
  242. return;
  243. if (defaultPrevented) {
  244. _this.$$parse(oldUrl_1);
  245. _this.$$state = oldState_1;
  246. }
  247. else {
  248. // This block doesn't run when initalizing because it's going to perform the update to
  249. // the URL which shouldn't be needed when initalizing.
  250. if (urlOrStateChanged_1) {
  251. _this.setBrowserUrlWithFallback(newUrl, currentReplace_1, oldState_1 === _this.$$state ? null : _this.$$state);
  252. _this.$$replace = false;
  253. }
  254. $rootScope.$broadcast('$locationChangeSuccess', newUrl, oldUrl_1, _this.$$state, oldState_1);
  255. }
  256. });
  257. }
  258. }
  259. _this.$$replace = false;
  260. });
  261. };
  262. $locationShim.prototype.resetBrowserUpdate = function () {
  263. this.$$replace = false;
  264. this.$$state = this.browserState();
  265. this.updateBrowser = false;
  266. this.lastBrowserUrl = this.browserUrl();
  267. };
  268. $locationShim.prototype.browserUrl = function (url, replace, state) {
  269. // In modern browsers `history.state` is `null` by default; treating it separately
  270. // from `undefined` would cause `$browser.url('/foo')` to change `history.state`
  271. // to undefined via `pushState`. Instead, let's change `undefined` to `null` here.
  272. if (typeof state === 'undefined') {
  273. state = null;
  274. }
  275. // setter
  276. if (url) {
  277. var sameState = this.lastHistoryState === state;
  278. // Normalize the inputted URL
  279. url = this.urlCodec.parse(url).href;
  280. // Don't change anything if previous and current URLs and states match.
  281. if (this.lastBrowserUrl === url && sameState) {
  282. return this;
  283. }
  284. this.lastBrowserUrl = url;
  285. this.lastHistoryState = state;
  286. // Remove server base from URL as the Angular APIs for updating URL require
  287. // it to be the path+.
  288. url = this.stripBaseUrl(this.getServerBase(), url) || url;
  289. // Set the URL
  290. if (replace) {
  291. this.locationStrategy.replaceState(state, '', url, '');
  292. }
  293. else {
  294. this.locationStrategy.pushState(state, '', url, '');
  295. }
  296. this.cacheState();
  297. return this;
  298. // getter
  299. }
  300. else {
  301. return this.platformLocation.href;
  302. }
  303. };
  304. $locationShim.prototype.cacheState = function () {
  305. // This should be the only place in $browser where `history.state` is read.
  306. this.cachedState = this.platformLocation.getState();
  307. if (typeof this.cachedState === 'undefined') {
  308. this.cachedState = null;
  309. }
  310. // Prevent callbacks fo fire twice if both hashchange & popstate were fired.
  311. if (deepEqual(this.cachedState, this.lastCachedState)) {
  312. this.cachedState = this.lastCachedState;
  313. }
  314. this.lastCachedState = this.cachedState;
  315. this.lastHistoryState = this.cachedState;
  316. };
  317. /**
  318. * This function emulates the $browser.state() function from AngularJS. It will cause
  319. * history.state to be cached unless changed with deep equality check.
  320. */
  321. $locationShim.prototype.browserState = function () { return this.cachedState; };
  322. $locationShim.prototype.stripBaseUrl = function (base, url) {
  323. if (url.startsWith(base)) {
  324. return url.substr(base.length);
  325. }
  326. return undefined;
  327. };
  328. $locationShim.prototype.getServerBase = function () {
  329. var _a = this.platformLocation, protocol = _a.protocol, hostname = _a.hostname, port = _a.port;
  330. var baseHref = this.locationStrategy.getBaseHref();
  331. var url = protocol + "//" + hostname + (port ? ':' + port : '') + (baseHref || '/');
  332. return url.endsWith('/') ? url : url + '/';
  333. };
  334. $locationShim.prototype.parseAppUrl = function (url) {
  335. if (DOUBLE_SLASH_REGEX.test(url)) {
  336. throw new Error("Bad Path - URL cannot start with double slashes: " + url);
  337. }
  338. var prefixed = (url.charAt(0) !== '/');
  339. if (prefixed) {
  340. url = '/' + url;
  341. }
  342. var match = this.urlCodec.parse(url, this.getServerBase());
  343. if (typeof match === 'string') {
  344. throw new Error("Bad URL - Cannot parse URL: " + url);
  345. }
  346. var path = prefixed && match.pathname.charAt(0) === '/' ? match.pathname.substring(1) : match.pathname;
  347. this.$$path = this.urlCodec.decodePath(path);
  348. this.$$search = this.urlCodec.decodeSearch(match.search);
  349. this.$$hash = this.urlCodec.decodeHash(match.hash);
  350. // make sure path starts with '/';
  351. if (this.$$path && this.$$path.charAt(0) !== '/') {
  352. this.$$path = '/' + this.$$path;
  353. }
  354. };
  355. /**
  356. * Registers listeners for URL changes. This API is used to catch updates performed by the
  357. * AngularJS framework. These changes are a subset of the `$locationChangeStart` and
  358. * `$locationChangeSuccess` events which fire when AngularJS updates its internally-referenced
  359. * version of the browser URL.
  360. *
  361. * It's possible for `$locationChange` events to happen, but for the browser URL
  362. * (window.location) to remain unchanged. This `onChange` callback will fire only when AngularJS
  363. * actually updates the browser URL (window.location).
  364. *
  365. * @param fn The callback function that is triggered for the listener when the URL changes.
  366. * @param err The callback function that is triggered when an error occurs.
  367. */
  368. $locationShim.prototype.onChange = function (fn, err) {
  369. if (err === void 0) { err = function (e) { }; }
  370. this.$$changeListeners.push([fn, err]);
  371. };
  372. /** @internal */
  373. $locationShim.prototype.$$notifyChangeListeners = function (url, state, oldUrl, oldState) {
  374. if (url === void 0) { url = ''; }
  375. if (oldUrl === void 0) { oldUrl = ''; }
  376. this.$$changeListeners.forEach(function (_a) {
  377. var _b = __read(_a, 2), fn = _b[0], err = _b[1];
  378. try {
  379. fn(url, state, oldUrl, oldState);
  380. }
  381. catch (e) {
  382. err(e);
  383. }
  384. });
  385. };
  386. /**
  387. * Parses the provided URL, and sets the current URL to the parsed result.
  388. *
  389. * @param url The URL string.
  390. */
  391. $locationShim.prototype.$$parse = function (url) {
  392. var pathUrl;
  393. if (url.startsWith('/')) {
  394. pathUrl = url;
  395. }
  396. else {
  397. // Remove protocol & hostname if URL starts with it
  398. pathUrl = this.stripBaseUrl(this.getServerBase(), url);
  399. }
  400. if (typeof pathUrl === 'undefined') {
  401. throw new Error("Invalid url \"" + url + "\", missing path prefix \"" + this.getServerBase() + "\".");
  402. }
  403. this.parseAppUrl(pathUrl);
  404. if (!this.$$path) {
  405. this.$$path = '/';
  406. }
  407. this.composeUrls();
  408. };
  409. /**
  410. * Parses the provided URL and its relative URL.
  411. *
  412. * @param url The full URL string.
  413. * @param relHref A URL string relative to the full URL string.
  414. */
  415. $locationShim.prototype.$$parseLinkUrl = function (url, relHref) {
  416. // When relHref is passed, it should be a hash and is handled separately
  417. if (relHref && relHref[0] === '#') {
  418. this.hash(relHref.slice(1));
  419. return true;
  420. }
  421. var rewrittenUrl;
  422. var appUrl = this.stripBaseUrl(this.getServerBase(), url);
  423. if (typeof appUrl !== 'undefined') {
  424. rewrittenUrl = this.getServerBase() + appUrl;
  425. }
  426. else if (this.getServerBase() === url + '/') {
  427. rewrittenUrl = this.getServerBase();
  428. }
  429. // Set the URL
  430. if (rewrittenUrl) {
  431. this.$$parse(rewrittenUrl);
  432. }
  433. return !!rewrittenUrl;
  434. };
  435. $locationShim.prototype.setBrowserUrlWithFallback = function (url, replace, state) {
  436. var oldUrl = this.url();
  437. var oldState = this.$$state;
  438. try {
  439. this.browserUrl(url, replace, state);
  440. // Make sure $location.state() returns referentially identical (not just deeply equal)
  441. // state object; this makes possible quick checking if the state changed in the digest
  442. // loop. Checking deep equality would be too expensive.
  443. this.$$state = this.browserState();
  444. this.$$notifyChangeListeners(url, state, oldUrl, oldState);
  445. }
  446. catch (e) {
  447. // Restore old values if pushState fails
  448. this.url(oldUrl);
  449. this.$$state = oldState;
  450. throw e;
  451. }
  452. };
  453. $locationShim.prototype.composeUrls = function () {
  454. this.$$url = this.urlCodec.normalize(this.$$path, this.$$search, this.$$hash);
  455. this.$$absUrl = this.getServerBase() + this.$$url.substr(1); // remove '/' from front of URL
  456. this.updateBrowser = true;
  457. };
  458. /**
  459. * Retrieves the full URL representation with all segments encoded according to
  460. * rules specified in
  461. * [RFC 3986](http://www.ietf.org/rfc/rfc3986.txt).
  462. *
  463. *
  464. * ```js
  465. * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
  466. * let absUrl = $location.absUrl();
  467. * // => "http://example.com/#/some/path?foo=bar&baz=xoxo"
  468. * ```
  469. */
  470. $locationShim.prototype.absUrl = function () { return this.$$absUrl; };
  471. $locationShim.prototype.url = function (url) {
  472. if (typeof url === 'string') {
  473. if (!url.length) {
  474. url = '/';
  475. }
  476. var match = PATH_MATCH.exec(url);
  477. if (!match)
  478. return this;
  479. if (match[1] || url === '')
  480. this.path(this.urlCodec.decodePath(match[1]));
  481. if (match[2] || match[1] || url === '')
  482. this.search(match[3] || '');
  483. this.hash(match[5] || '');
  484. // Chainable method
  485. return this;
  486. }
  487. return this.$$url;
  488. };
  489. /**
  490. * Retrieves the protocol of the current URL.
  491. *
  492. * ```js
  493. * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
  494. * let protocol = $location.protocol();
  495. * // => "http"
  496. * ```
  497. */
  498. $locationShim.prototype.protocol = function () { return this.$$protocol; };
  499. /**
  500. * Retrieves the protocol of the current URL.
  501. *
  502. * In contrast to the non-AngularJS version `location.host` which returns `hostname:port`, this
  503. * returns the `hostname` portion only.
  504. *
  505. *
  506. * ```js
  507. * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
  508. * let host = $location.host();
  509. * // => "example.com"
  510. *
  511. * // given URL http://user:password@example.com:8080/#/some/path?foo=bar&baz=xoxo
  512. * host = $location.host();
  513. * // => "example.com"
  514. * host = location.host;
  515. * // => "example.com:8080"
  516. * ```
  517. */
  518. $locationShim.prototype.host = function () { return this.$$host; };
  519. /**
  520. * Retrieves the port of the current URL.
  521. *
  522. * ```js
  523. * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
  524. * let port = $location.port();
  525. * // => 80
  526. * ```
  527. */
  528. $locationShim.prototype.port = function () { return this.$$port; };
  529. $locationShim.prototype.path = function (path) {
  530. if (typeof path === 'undefined') {
  531. return this.$$path;
  532. }
  533. // null path converts to empty string. Prepend with "/" if needed.
  534. path = path !== null ? path.toString() : '';
  535. path = path.charAt(0) === '/' ? path : '/' + path;
  536. this.$$path = path;
  537. this.composeUrls();
  538. return this;
  539. };
  540. $locationShim.prototype.search = function (search, paramValue) {
  541. switch (arguments.length) {
  542. case 0:
  543. return this.$$search;
  544. case 1:
  545. if (typeof search === 'string' || typeof search === 'number') {
  546. this.$$search = this.urlCodec.decodeSearch(search.toString());
  547. }
  548. else if (typeof search === 'object' && search !== null) {
  549. // Copy the object so it's never mutated
  550. search = __assign({}, search);
  551. // remove object undefined or null properties
  552. for (var key in search) {
  553. if (search[key] == null)
  554. delete search[key];
  555. }
  556. this.$$search = search;
  557. }
  558. else {
  559. throw new Error('LocationProvider.search(): First argument must be a string or an object.');
  560. }
  561. break;
  562. default:
  563. if (typeof search === 'string') {
  564. var currentSearch = this.search();
  565. if (typeof paramValue === 'undefined' || paramValue === null) {
  566. delete currentSearch[search];
  567. return this.search(currentSearch);
  568. }
  569. else {
  570. currentSearch[search] = paramValue;
  571. return this.search(currentSearch);
  572. }
  573. }
  574. }
  575. this.composeUrls();
  576. return this;
  577. };
  578. $locationShim.prototype.hash = function (hash) {
  579. if (typeof hash === 'undefined') {
  580. return this.$$hash;
  581. }
  582. this.$$hash = hash !== null ? hash.toString() : '';
  583. this.composeUrls();
  584. return this;
  585. };
  586. /**
  587. * Changes to `$location` during the current `$digest` will replace the current
  588. * history record, instead of adding a new one.
  589. */
  590. $locationShim.prototype.replace = function () {
  591. this.$$replace = true;
  592. return this;
  593. };
  594. $locationShim.prototype.state = function (state) {
  595. if (typeof state === 'undefined') {
  596. return this.$$state;
  597. }
  598. this.$$state = state;
  599. return this;
  600. };
  601. return $locationShim;
  602. }());
  603. /**
  604. * The factory function used to create an instance of the `$locationShim` in Angular,
  605. * and provides an API-compatiable `$locationProvider` for AngularJS.
  606. *
  607. * @publicApi
  608. */
  609. var $locationShimProvider = /** @class */ (function () {
  610. function $locationShimProvider(ngUpgrade, location, platformLocation, urlCodec, locationStrategy) {
  611. this.ngUpgrade = ngUpgrade;
  612. this.location = location;
  613. this.platformLocation = platformLocation;
  614. this.urlCodec = urlCodec;
  615. this.locationStrategy = locationStrategy;
  616. }
  617. /**
  618. * Factory method that returns an instance of the $locationShim
  619. */
  620. $locationShimProvider.prototype.$get = function () {
  621. return new $locationShim(this.ngUpgrade.$injector, this.location, this.platformLocation, this.urlCodec, this.locationStrategy);
  622. };
  623. /**
  624. * Stub method used to keep API compatible with AngularJS. This setting is configured through
  625. * the LocationUpgradeModule's `config` method in your Angular app.
  626. */
  627. $locationShimProvider.prototype.hashPrefix = function (prefix) {
  628. throw new Error('Configure LocationUpgrade through LocationUpgradeModule.config method.');
  629. };
  630. /**
  631. * Stub method used to keep API compatible with AngularJS. This setting is configured through
  632. * the LocationUpgradeModule's `config` method in your Angular app.
  633. */
  634. $locationShimProvider.prototype.html5Mode = function (mode) {
  635. throw new Error('Configure LocationUpgrade through LocationUpgradeModule.config method.');
  636. };
  637. return $locationShimProvider;
  638. }());
  639. /**
  640. * @license
  641. * Copyright Google Inc. All Rights Reserved.
  642. *
  643. * Use of this source code is governed by an MIT-style license that can be
  644. * found in the LICENSE file at https://angular.io/license
  645. */
  646. /**
  647. * A codec for encoding and decoding URL parts.
  648. *
  649. * @publicApi
  650. **/
  651. var UrlCodec = /** @class */ (function () {
  652. function UrlCodec() {
  653. }
  654. return UrlCodec;
  655. }());
  656. /**
  657. * A `UrlCodec` that uses logic from AngularJS to serialize and parse URLs
  658. * and URL parameters.
  659. *
  660. * @publicApi
  661. */
  662. var AngularJSUrlCodec = /** @class */ (function () {
  663. function AngularJSUrlCodec() {
  664. }
  665. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L15
  666. AngularJSUrlCodec.prototype.encodePath = function (path) {
  667. var segments = path.split('/');
  668. var i = segments.length;
  669. while (i--) {
  670. // decode forward slashes to prevent them from being double encoded
  671. segments[i] = encodeUriSegment(segments[i].replace(/%2F/g, '/'));
  672. }
  673. path = segments.join('/');
  674. return _stripIndexHtml((path && path[0] !== '/' && '/' || '') + path);
  675. };
  676. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L42
  677. AngularJSUrlCodec.prototype.encodeSearch = function (search) {
  678. if (typeof search === 'string') {
  679. search = parseKeyValue(search);
  680. }
  681. search = toKeyValue(search);
  682. return search ? '?' + search : '';
  683. };
  684. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L44
  685. AngularJSUrlCodec.prototype.encodeHash = function (hash) {
  686. hash = encodeUriSegment(hash);
  687. return hash ? '#' + hash : '';
  688. };
  689. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L27
  690. AngularJSUrlCodec.prototype.decodePath = function (path, html5Mode) {
  691. if (html5Mode === void 0) { html5Mode = true; }
  692. var segments = path.split('/');
  693. var i = segments.length;
  694. while (i--) {
  695. segments[i] = decodeURIComponent(segments[i]);
  696. if (html5Mode) {
  697. // encode forward slashes to prevent them from being mistaken for path separators
  698. segments[i] = segments[i].replace(/\//g, '%2F');
  699. }
  700. }
  701. return segments.join('/');
  702. };
  703. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L72
  704. AngularJSUrlCodec.prototype.decodeSearch = function (search) { return parseKeyValue(search); };
  705. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L73
  706. AngularJSUrlCodec.prototype.decodeHash = function (hash) {
  707. hash = decodeURIComponent(hash);
  708. return hash[0] === '#' ? hash.substring(1) : hash;
  709. };
  710. AngularJSUrlCodec.prototype.normalize = function (pathOrHref, search, hash, baseUrl) {
  711. if (arguments.length === 1) {
  712. var parsed = this.parse(pathOrHref, baseUrl);
  713. if (typeof parsed === 'string') {
  714. return parsed;
  715. }
  716. var serverUrl = parsed.protocol + "://" + parsed.hostname + (parsed.port ? ':' + parsed.port : '');
  717. return this.normalize(this.decodePath(parsed.pathname), this.decodeSearch(parsed.search), this.decodeHash(parsed.hash), serverUrl);
  718. }
  719. else {
  720. var encPath = this.encodePath(pathOrHref);
  721. var encSearch = search && this.encodeSearch(search) || '';
  722. var encHash = hash && this.encodeHash(hash) || '';
  723. var joinedPath = (baseUrl || '') + encPath;
  724. if (!joinedPath.length || joinedPath[0] !== '/') {
  725. joinedPath = '/' + joinedPath;
  726. }
  727. return joinedPath + encSearch + encHash;
  728. }
  729. };
  730. AngularJSUrlCodec.prototype.areEqual = function (valA, valB) { return this.normalize(valA) === this.normalize(valB); };
  731. // https://github.com/angular/angular.js/blob/864c7f0/src/ng/urlUtils.js#L60
  732. AngularJSUrlCodec.prototype.parse = function (url, base) {
  733. try {
  734. var parsed = new URL(url, base);
  735. return {
  736. href: parsed.href,
  737. protocol: parsed.protocol ? parsed.protocol.replace(/:$/, '') : '',
  738. host: parsed.host,
  739. search: parsed.search ? parsed.search.replace(/^\?/, '') : '',
  740. hash: parsed.hash ? parsed.hash.replace(/^#/, '') : '',
  741. hostname: parsed.hostname,
  742. port: parsed.port,
  743. pathname: (parsed.pathname.charAt(0) === '/') ? parsed.pathname : '/' + parsed.pathname
  744. };
  745. }
  746. catch (e) {
  747. throw new Error("Invalid URL (" + url + ") with base (" + base + ")");
  748. }
  749. };
  750. return AngularJSUrlCodec;
  751. }());
  752. function _stripIndexHtml(url) {
  753. return url.replace(/\/index.html$/, '');
  754. }
  755. /**
  756. * Tries to decode the URI component without throwing an exception.
  757. *
  758. * @private
  759. * @param str value potential URI component to check.
  760. * @returns {boolean} True if `value` can be decoded
  761. * with the decodeURIComponent function.
  762. */
  763. function tryDecodeURIComponent(value) {
  764. try {
  765. return decodeURIComponent(value);
  766. }
  767. catch (e) {
  768. // Ignore any invalid uri component.
  769. return undefined;
  770. }
  771. }
  772. /**
  773. * Parses an escaped url query string into key-value pairs. Logic taken from
  774. * https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1382
  775. * @returns {Object.<string,boolean|Array>}
  776. */
  777. function parseKeyValue(keyValue) {
  778. var obj = {};
  779. (keyValue || '').split('&').forEach(function (keyValue) {
  780. var splitPoint, key, val;
  781. if (keyValue) {
  782. key = keyValue = keyValue.replace(/\+/g, '%20');
  783. splitPoint = keyValue.indexOf('=');
  784. if (splitPoint !== -1) {
  785. key = keyValue.substring(0, splitPoint);
  786. val = keyValue.substring(splitPoint + 1);
  787. }
  788. key = tryDecodeURIComponent(key);
  789. if (typeof key !== 'undefined') {
  790. val = typeof val !== 'undefined' ? tryDecodeURIComponent(val) : true;
  791. if (!obj.hasOwnProperty(key)) {
  792. obj[key] = val;
  793. }
  794. else if (Array.isArray(obj[key])) {
  795. obj[key].push(val);
  796. }
  797. else {
  798. obj[key] = [obj[key], val];
  799. }
  800. }
  801. }
  802. });
  803. return obj;
  804. }
  805. /**
  806. * Serializes into key-value pairs. Logic taken from
  807. * https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1409
  808. */
  809. function toKeyValue(obj) {
  810. var parts = [];
  811. var _loop_1 = function (key) {
  812. var value = obj[key];
  813. if (Array.isArray(value)) {
  814. value.forEach(function (arrayValue) {
  815. parts.push(encodeUriQuery(key, true) +
  816. (arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
  817. });
  818. }
  819. else {
  820. parts.push(encodeUriQuery(key, true) +
  821. (value === true ? '' : '=' + encodeUriQuery(value, true)));
  822. }
  823. };
  824. for (var key in obj) {
  825. _loop_1(key);
  826. }
  827. return parts.length ? parts.join('&') : '';
  828. }
  829. /**
  830. * We need our custom method because encodeURIComponent is too aggressive and doesn't follow
  831. * http://www.ietf.org/rfc/rfc3986.txt with regards to the character set (pchar) allowed in path
  832. * segments:
  833. * segment = *pchar
  834. * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  835. * pct-encoded = "%" HEXDIG HEXDIG
  836. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  837. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  838. * / "*" / "+" / "," / ";" / "="
  839. *
  840. * Logic from https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1437
  841. */
  842. function encodeUriSegment(val) {
  843. return encodeUriQuery(val, true)
  844. .replace(/%26/gi, '&')
  845. .replace(/%3D/gi, '=')
  846. .replace(/%2B/gi, '+');
  847. }
  848. /**
  849. * This method is intended for encoding *key* or *value* parts of query component. We need a custom
  850. * method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
  851. * encoded per http://tools.ietf.org/html/rfc3986:
  852. * query = *( pchar / "/" / "?" )
  853. * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  854. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  855. * pct-encoded = "%" HEXDIG HEXDIG
  856. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  857. * / "*" / "+" / "," / ";" / "="
  858. *
  859. * Logic from https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1456
  860. */
  861. function encodeUriQuery(val, pctEncodeSpaces) {
  862. if (pctEncodeSpaces === void 0) { pctEncodeSpaces = false; }
  863. return encodeURIComponent(val)
  864. .replace(/%40/gi, '@')
  865. .replace(/%3A/gi, ':')
  866. .replace(/%24/g, '$')
  867. .replace(/%2C/gi, ',')
  868. .replace(/%3B/gi, ';')
  869. .replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
  870. }
  871. /**
  872. * @license
  873. * Copyright Google Inc. All Rights Reserved.
  874. *
  875. * Use of this source code is governed by an MIT-style license that can be
  876. * found in the LICENSE file at https://angular.io/license
  877. */
  878. /**
  879. * A provider token used to configure the location upgrade module.
  880. *
  881. * @publicApi
  882. */
  883. var LOCATION_UPGRADE_CONFIGURATION = new core.InjectionToken('LOCATION_UPGRADE_CONFIGURATION');
  884. var APP_BASE_HREF_RESOLVED = new core.InjectionToken('APP_BASE_HREF_RESOLVED');
  885. /**
  886. * `NgModule` used for providing and configuring Angular's Unified Location Service for upgrading.
  887. *
  888. * @see [Using the Unified Angular Location Service](guide/upgrade#using-the-unified-angular-location-service)
  889. *
  890. * @publicApi
  891. */
  892. var LocationUpgradeModule = /** @class */ (function () {
  893. function LocationUpgradeModule() {
  894. }
  895. LocationUpgradeModule_1 = LocationUpgradeModule;
  896. LocationUpgradeModule.config = function (config) {
  897. return {
  898. ngModule: LocationUpgradeModule_1,
  899. providers: [
  900. common.Location,
  901. {
  902. provide: $locationShim,
  903. useFactory: provide$location,
  904. deps: [_static.UpgradeModule, common.Location, common.PlatformLocation, UrlCodec, common.LocationStrategy]
  905. },
  906. { provide: LOCATION_UPGRADE_CONFIGURATION, useValue: config ? config : {} },
  907. { provide: UrlCodec, useFactory: provideUrlCodec, deps: [LOCATION_UPGRADE_CONFIGURATION] },
  908. {
  909. provide: APP_BASE_HREF_RESOLVED,
  910. useFactory: provideAppBaseHref,
  911. deps: [LOCATION_UPGRADE_CONFIGURATION, [new core.Inject(common.APP_BASE_HREF), new core.Optional()]]
  912. },
  913. {
  914. provide: common.LocationStrategy,
  915. useFactory: provideLocationStrategy,
  916. deps: [
  917. common.PlatformLocation,
  918. APP_BASE_HREF_RESOLVED,
  919. LOCATION_UPGRADE_CONFIGURATION,
  920. ]
  921. },
  922. ],
  923. };
  924. };
  925. var LocationUpgradeModule_1;
  926. LocationUpgradeModule = LocationUpgradeModule_1 = __decorate([
  927. core.NgModule({ imports: [common.CommonModule] })
  928. ], LocationUpgradeModule);
  929. return LocationUpgradeModule;
  930. }());
  931. function provideAppBaseHref(config, appBaseHref) {
  932. if (config && config.appBaseHref != null) {
  933. return config.appBaseHref;
  934. }
  935. else if (appBaseHref != null) {
  936. return appBaseHref;
  937. }
  938. return '';
  939. }
  940. function provideUrlCodec(config) {
  941. var codec = config && config.urlCodec || AngularJSUrlCodec;
  942. return new codec();
  943. }
  944. function provideLocationStrategy(platformLocation, baseHref, options) {
  945. if (options === void 0) { options = {}; }
  946. return options.useHash ? new common.HashLocationStrategy(platformLocation, baseHref) :
  947. new common.PathLocationStrategy(platformLocation, baseHref);
  948. }
  949. function provide$location(ngUpgrade, location, platformLocation, urlCodec, locationStrategy) {
  950. var $locationProvider = new $locationShimProvider(ngUpgrade, location, platformLocation, urlCodec, locationStrategy);
  951. return $locationProvider.$get();
  952. }
  953. /**
  954. * @license
  955. * Copyright Google Inc. All Rights Reserved.
  956. *
  957. * Use of this source code is governed by an MIT-style license that can be
  958. * found in the LICENSE file at https://angular.io/license
  959. */
  960. /**
  961. * @license
  962. * Copyright Google Inc. All Rights Reserved.
  963. *
  964. * Use of this source code is governed by an MIT-style license that can be
  965. * found in the LICENSE file at https://angular.io/license
  966. */
  967. // This file only reexports content of the `src` folder. Keep it that way.
  968. /**
  969. * @license
  970. * Copyright Google Inc. All Rights Reserved.
  971. *
  972. * Use of this source code is governed by an MIT-style license that can be
  973. * found in the LICENSE file at https://angular.io/license
  974. */
  975. /**
  976. * Generated bundle index. Do not edit.
  977. */
  978. exports.ɵangular_packages_common_upgrade_upgrade_d = provide$location;
  979. exports.ɵangular_packages_common_upgrade_upgrade_a = provideAppBaseHref;
  980. exports.ɵangular_packages_common_upgrade_upgrade_c = provideLocationStrategy;
  981. exports.ɵangular_packages_common_upgrade_upgrade_b = provideUrlCodec;
  982. exports.$locationShim = $locationShim;
  983. exports.$locationShimProvider = $locationShimProvider;
  984. exports.LOCATION_UPGRADE_CONFIGURATION = LOCATION_UPGRADE_CONFIGURATION;
  985. exports.LocationUpgradeModule = LocationUpgradeModule;
  986. exports.AngularJSUrlCodec = AngularJSUrlCodec;
  987. exports.UrlCodec = UrlCodec;
  988. Object.defineProperty(exports, '__esModule', { value: true });
  989. }));
  990. //# sourceMappingURL=common-upgrade.umd.js.map