feat(common): provide replacement for AngularJS $location service (#30055)
This commit provides a replacement for `$location`. The new service is written in Angular, and can be consumed into existing applications by using the downgraded version of the provider. Prior to this addition, applications upgrading from AngularJS to Angular could get into a situation where AngularJS wanted to control the URL, and would often parse or se rialize the URL in a different way than Angular. Additionally, AngularJS was alerted to URL changes only through the `$digest` cycle. This provided a buggy feedback loop from Angular to AngularJS. With this new `LocationUpgradeProvider`, the `$location` methods and events are provided in Angular, and use Angular APIs to make updates to the URL. Additionally, change s to the URL made by other parts of the Angular framework (such as the Router) will be listened for and will cause events to fire in AngularJS, but will no longer attempt to update the URL (since it was already updated by the Angular framework). This centralizes URL reads and writes to Angular and should help provide an easier path to upgrading AngularJS applications to Angular. PR Close #30055
This commit is contained in:
@ -46,6 +46,7 @@ export abstract class UrlCodec {
|
||||
* @publicApi
|
||||
*/
|
||||
export class AngularJSUrlCodec implements UrlCodec {
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L15
|
||||
encodePath(path: string): string {
|
||||
const segments = path.split('/');
|
||||
let i = segments.length;
|
||||
@ -59,6 +60,7 @@ export class AngularJSUrlCodec implements UrlCodec {
|
||||
return _stripIndexHtml((path && path[0] !== '/' && '/' || '') + path);
|
||||
}
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L42
|
||||
encodeSearch(search: string|{[k: string]: unknown}): string {
|
||||
if (typeof search === 'string') {
|
||||
search = parseKeyValue(search);
|
||||
@ -68,11 +70,13 @@ export class AngularJSUrlCodec implements UrlCodec {
|
||||
return search ? '?' + search : '';
|
||||
}
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L44
|
||||
encodeHash(hash: string) {
|
||||
hash = encodeUriSegment(hash);
|
||||
return hash ? '#' + hash : '';
|
||||
}
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L27
|
||||
decodePath(path: string, html5Mode = true): string {
|
||||
const segments = path.split('/');
|
||||
let i = segments.length;
|
||||
@ -88,13 +92,17 @@ export class AngularJSUrlCodec implements UrlCodec {
|
||||
return segments.join('/');
|
||||
}
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L72
|
||||
decodeSearch(search: string) { return parseKeyValue(search); }
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L73
|
||||
decodeHash(hash: string) {
|
||||
hash = decodeURIComponent(hash);
|
||||
return hash[0] === '#' ? hash.substring(1) : hash;
|
||||
}
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L149
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/location.js#L42
|
||||
normalize(href: string): string;
|
||||
normalize(path: string, search: {[k: string]: unknown}, hash: string, baseUrl?: string): string;
|
||||
normalize(pathOrHref: string, search?: {[k: string]: unknown}, hash?: string, baseUrl?: string):
|
||||
@ -128,6 +136,7 @@ export class AngularJSUrlCodec implements UrlCodec {
|
||||
|
||||
areEqual(a: string, b: string) { return this.normalize(a) === this.normalize(b); }
|
||||
|
||||
// https://github.com/angular/angular.js/blob/864c7f0/src/ng/urlUtils.js#L60
|
||||
parse(url: string, base?: string) {
|
||||
try {
|
||||
const parsed = new URL(url, base);
|
||||
@ -170,7 +179,8 @@ function tryDecodeURIComponent(value: string) {
|
||||
|
||||
|
||||
/**
|
||||
* Parses an escaped url query string into key-value pairs.
|
||||
* Parses an escaped url query string into key-value pairs. Logic taken from
|
||||
* https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1382
|
||||
* @returns {Object.<string,boolean|Array>}
|
||||
*/
|
||||
function parseKeyValue(keyValue: string): {[k: string]: unknown} {
|
||||
@ -200,6 +210,10 @@ function parseKeyValue(keyValue: string): {[k: string]: unknown} {
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes into key-value pairs. Logic taken from
|
||||
* https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1409
|
||||
*/
|
||||
function toKeyValue(obj: {[k: string]: unknown}) {
|
||||
const parts: unknown[] = [];
|
||||
for (const key in obj) {
|
||||
@ -230,6 +244,8 @@ function toKeyValue(obj: {[k: string]: unknown}) {
|
||||
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
||||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
||||
* / "*" / "+" / "," / ";" / "="
|
||||
*
|
||||
* Logic from https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1437
|
||||
*/
|
||||
function encodeUriSegment(val: string) {
|
||||
return encodeUriQuery(val, true)
|
||||
@ -249,6 +265,8 @@ function encodeUriSegment(val: string) {
|
||||
* pct-encoded = "%" HEXDIG HEXDIG
|
||||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
||||
* / "*" / "+" / "," / ";" / "="
|
||||
*
|
||||
* Logic from https://github.com/angular/angular.js/blob/864c7f0/src/Angular.js#L1456
|
||||
*/
|
||||
function encodeUriQuery(val: string, pctEncodeSpaces: boolean = false) {
|
||||
return encodeURIComponent(val)
|
||||
|
Reference in New Issue
Block a user