url_tree.js
22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { PRIMARY_OUTLET } from './shared';
import { forEach, shallowEqual } from './utils/collection';
/**
* @return {?}
*/
export function createEmptyUrlTree() {
return new UrlTree(new UrlSegmentGroup([], {}), {}, null);
}
/**
* @param {?} container
* @param {?} containee
* @param {?} exact
* @return {?}
*/
export function containsTree(container, containee, exact) {
if (exact) {
return equalQueryParams(container.queryParams, containee.queryParams) &&
equalSegmentGroups(container.root, containee.root);
}
return containsQueryParams(container.queryParams, containee.queryParams) &&
containsSegmentGroup(container.root, containee.root);
}
/**
* @param {?} container
* @param {?} containee
* @return {?}
*/
function equalQueryParams(container, containee) {
return shallowEqual(container, containee);
}
/**
* @param {?} container
* @param {?} containee
* @return {?}
*/
function equalSegmentGroups(container, containee) {
if (!equalPath(container.segments, containee.segments))
return false;
if (container.numberOfChildren !== containee.numberOfChildren)
return false;
for (var c in containee.children) {
if (!container.children[c])
return false;
if (!equalSegmentGroups(container.children[c], containee.children[c]))
return false;
}
return true;
}
/**
* @param {?} container
* @param {?} containee
* @return {?}
*/
function containsQueryParams(container, containee) {
return Object.keys(containee).length <= Object.keys(container).length &&
Object.keys(containee).every(function (key) { return containee[key] === container[key]; });
}
/**
* @param {?} container
* @param {?} containee
* @return {?}
*/
function containsSegmentGroup(container, containee) {
return containsSegmentGroupHelper(container, containee, containee.segments);
}
/**
* @param {?} container
* @param {?} containee
* @param {?} containeePaths
* @return {?}
*/
function containsSegmentGroupHelper(container, containee, containeePaths) {
if (container.segments.length > containeePaths.length) {
var /** @type {?} */ current = container.segments.slice(0, containeePaths.length);
if (!equalPath(current, containeePaths))
return false;
if (containee.hasChildren())
return false;
return true;
}
else if (container.segments.length === containeePaths.length) {
if (!equalPath(container.segments, containeePaths))
return false;
for (var c in containee.children) {
if (!container.children[c])
return false;
if (!containsSegmentGroup(container.children[c], containee.children[c]))
return false;
}
return true;
}
else {
var /** @type {?} */ current = containeePaths.slice(0, container.segments.length);
var /** @type {?} */ next = containeePaths.slice(container.segments.length);
if (!equalPath(container.segments, current))
return false;
if (!container.children[PRIMARY_OUTLET])
return false;
return containsSegmentGroupHelper(container.children[PRIMARY_OUTLET], containee, next);
}
}
/**
* \@whatItDoes Represents the parsed URL.
*
* \@howToUse
*
* ```
* \@Component({templateUrl:'template.html'})
* class MyComponent {
* constructor(router: Router) {
* const tree: UrlTree =
* router.parseUrl('/team/33/(user/victor//support:help)?debug=true#fragment');
* const f = tree.fragment; // return 'fragment'
* const q = tree.queryParams; // returns {debug: 'true'}
* const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
* const s: UrlSegment[] = g.segments; // returns 2 segments 'team' and '33'
* g.children[PRIMARY_OUTLET].segments; // returns 2 segments 'user' and 'victor'
* g.children['support'].segments; // return 1 segment 'help'
* }
* }
* ```
*
* \@description
*
* Since a router state is a tree, and the URL is nothing but a serialized state, the URL is a
* serialized tree.
* UrlTree is a data structure that provides a lot of affordances in dealing with URLs
*
* \@stable
*/
export var UrlTree = (function () {
/**
* \@internal
* @param {?} root
* @param {?} queryParams
* @param {?} fragment
*/
function UrlTree(root, queryParams, fragment) {
this.root = root;
this.queryParams = queryParams;
this.fragment = fragment;
}
/**
* \@docsNotRequired
* @return {?}
*/
UrlTree.prototype.toString = function () { return new DefaultUrlSerializer().serialize(this); };
return UrlTree;
}());
function UrlTree_tsickle_Closure_declarations() {
/**
* The root segment group of the URL tree
* @type {?}
*/
UrlTree.prototype.root;
/**
* The query params of the URL
* @type {?}
*/
UrlTree.prototype.queryParams;
/**
* The fragment of the URL
* @type {?}
*/
UrlTree.prototype.fragment;
}
/**
* \@whatItDoes Represents the parsed URL segment group.
*
* See {\@link UrlTree} for more information.
*
* \@stable
*/
export var UrlSegmentGroup = (function () {
/**
* @param {?} segments
* @param {?} children
*/
function UrlSegmentGroup(segments, children) {
var _this = this;
this.segments = segments;
this.children = children;
/** The parent node in the url tree */
this.parent = null;
forEach(children, function (v, k) { return v.parent = _this; });
}
/**
* Wether the segment has child segments
* @return {?}
*/
UrlSegmentGroup.prototype.hasChildren = function () { return this.numberOfChildren > 0; };
Object.defineProperty(UrlSegmentGroup.prototype, "numberOfChildren", {
/**
* Number of child segments
* @return {?}
*/
get: function () { return Object.keys(this.children).length; },
enumerable: true,
configurable: true
});
/**
* \@docsNotRequired
* @return {?}
*/
UrlSegmentGroup.prototype.toString = function () { return serializePaths(this); };
return UrlSegmentGroup;
}());
function UrlSegmentGroup_tsickle_Closure_declarations() {
/**
* \@internal
* @type {?}
*/
UrlSegmentGroup.prototype._sourceSegment;
/**
* \@internal
* @type {?}
*/
UrlSegmentGroup.prototype._segmentIndexShift;
/**
* The parent node in the url tree
* @type {?}
*/
UrlSegmentGroup.prototype.parent;
/**
* The URL segments of this group. See {\@link UrlSegment} for more information
* @type {?}
*/
UrlSegmentGroup.prototype.segments;
/**
* The list of children of this group
* @type {?}
*/
UrlSegmentGroup.prototype.children;
}
/**
* \@whatItDoes Represents a single URL segment.
*
* \@howToUse
*
* ```
* \@Component({templateUrl:'template.html'})
* class MyComponent {
* constructor(router: Router) {
* const tree: UrlTree = router.parseUrl('/team;id=33');
* const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
* const s: UrlSegment[] = g.segments;
* s[0].path; // returns 'team'
* s[0].parameters; // returns {id: 33}
* }
* }
* ```
*
* \@description
*
* A UrlSegment is a part of a URL between the two slashes. It contains a path and the matrix
* parameters associated with the segment.
*
* \@stable
*/
export var UrlSegment = (function () {
/**
* @param {?} path
* @param {?} parameters
*/
function UrlSegment(path, parameters) {
this.path = path;
this.parameters = parameters;
}
/**
* \@docsNotRequired
* @return {?}
*/
UrlSegment.prototype.toString = function () { return serializePath(this); };
return UrlSegment;
}());
function UrlSegment_tsickle_Closure_declarations() {
/**
* The path part of a URL segment
* @type {?}
*/
UrlSegment.prototype.path;
/**
* The matrix parameters associated with a segment
* @type {?}
*/
UrlSegment.prototype.parameters;
}
/**
* @param {?} a
* @param {?} b
* @return {?}
*/
export function equalSegments(a, b) {
if (a.length !== b.length)
return false;
for (var /** @type {?} */ i = 0; i < a.length; ++i) {
if (a[i].path !== b[i].path)
return false;
if (!shallowEqual(a[i].parameters, b[i].parameters))
return false;
}
return true;
}
/**
* @param {?} a
* @param {?} b
* @return {?}
*/
export function equalPath(a, b) {
if (a.length !== b.length)
return false;
for (var /** @type {?} */ i = 0; i < a.length; ++i) {
if (a[i].path !== b[i].path)
return false;
}
return true;
}
/**
* @param {?} segment
* @param {?} fn
* @return {?}
*/
export function mapChildrenIntoArray(segment, fn) {
var /** @type {?} */ res = [];
forEach(segment.children, function (child, childOutlet) {
if (childOutlet === PRIMARY_OUTLET) {
res = res.concat(fn(child, childOutlet));
}
});
forEach(segment.children, function (child, childOutlet) {
if (childOutlet !== PRIMARY_OUTLET) {
res = res.concat(fn(child, childOutlet));
}
});
return res;
}
/**
* \@whatItDoes Serializes and deserializes a URL string into a URL tree.
*
* \@description The url serialization strategy is customizable. You can
* make all URLs case insensitive by providing a custom UrlSerializer.
*
* See {\@link DefaultUrlSerializer} for an example of a URL serializer.
*
* \@stable
* @abstract
*/
export var UrlSerializer = (function () {
function UrlSerializer() {
}
/**
* Parse a url into a {\@link UrlTree}
* @abstract
* @param {?} url
* @return {?}
*/
UrlSerializer.prototype.parse = function (url) { };
/**
* Converts a {\@link UrlTree} into a url
* @abstract
* @param {?} tree
* @return {?}
*/
UrlSerializer.prototype.serialize = function (tree) { };
return UrlSerializer;
}());
/**
* \@whatItDoes A default implementation of the {\@link UrlSerializer}.
*
* \@description
*
* Example URLs:
*
* ```
* /inbox/33(popup:compose)
* /inbox/33;open=true/messages/44
* ```
*
* DefaultUrlSerializer uses parentheses to serialize secondary segments (e.g., popup:compose), the
* colon syntax to specify the outlet, and the ';parameter=value' syntax (e.g., open=true) to
* specify route specific parameters.
*
* \@stable
*/
export var DefaultUrlSerializer = (function () {
function DefaultUrlSerializer() {
}
/**
* Parses a url into a {\@link UrlTree}
* @param {?} url
* @return {?}
*/
DefaultUrlSerializer.prototype.parse = function (url) {
var /** @type {?} */ p = new UrlParser(url);
return new UrlTree(p.parseRootSegment(), p.parseQueryParams(), p.parseFragment());
};
/**
* Converts a {\@link UrlTree} into a url
* @param {?} tree
* @return {?}
*/
DefaultUrlSerializer.prototype.serialize = function (tree) {
var /** @type {?} */ segment = "/" + serializeSegment(tree.root, true);
var /** @type {?} */ query = serializeQueryParams(tree.queryParams);
var /** @type {?} */ fragment = tree.fragment !== null && tree.fragment !== undefined ? "#" + encodeURI(tree.fragment) : '';
return "" + segment + query + fragment;
};
return DefaultUrlSerializer;
}());
/**
* @param {?} segment
* @return {?}
*/
export function serializePaths(segment) {
return segment.segments.map(function (p) { return serializePath(p); }).join('/');
}
/**
* @param {?} segment
* @param {?} root
* @return {?}
*/
function serializeSegment(segment, root) {
if (segment.hasChildren() && root) {
var /** @type {?} */ primary = segment.children[PRIMARY_OUTLET] ?
serializeSegment(segment.children[PRIMARY_OUTLET], false) :
'';
var /** @type {?} */ children_1 = [];
forEach(segment.children, function (v, k) {
if (k !== PRIMARY_OUTLET) {
children_1.push(k + ":" + serializeSegment(v, false));
}
});
if (children_1.length > 0) {
return primary + "(" + children_1.join('//') + ")";
}
else {
return "" + primary;
}
}
else if (segment.hasChildren() && !root) {
var /** @type {?} */ children = mapChildrenIntoArray(segment, function (v, k) {
if (k === PRIMARY_OUTLET) {
return [serializeSegment(segment.children[PRIMARY_OUTLET], false)];
}
else {
return [(k + ":" + serializeSegment(v, false))];
}
});
return serializePaths(segment) + "/(" + children.join('//') + ")";
}
else {
return serializePaths(segment);
}
}
/**
* @param {?} s
* @return {?}
*/
export function encode(s) {
return encodeURIComponent(s);
}
/**
* @param {?} s
* @return {?}
*/
export function decode(s) {
return decodeURIComponent(s);
}
/**
* @param {?} path
* @return {?}
*/
export function serializePath(path) {
return "" + encode(path.path) + serializeParams(path.parameters);
}
/**
* @param {?} params
* @return {?}
*/
function serializeParams(params) {
return pairs(params).map(function (p) { return (";" + encode(p.first) + "=" + encode(p.second)); }).join('');
}
/**
* @param {?} params
* @return {?}
*/
function serializeQueryParams(params) {
var /** @type {?} */ strParams = Object.keys(params).map(function (name) {
var /** @type {?} */ value = params[name];
return Array.isArray(value) ? value.map(function (v) { return (encode(name) + "=" + encode(v)); }).join('&') :
encode(name) + "=" + encode(value);
});
return strParams.length ? "?" + strParams.join("&") : '';
}
var Pair = (function () {
/**
* @param {?} first
* @param {?} second
*/
function Pair(first, second) {
this.first = first;
this.second = second;
}
return Pair;
}());
function Pair_tsickle_Closure_declarations() {
/** @type {?} */
Pair.prototype.first;
/** @type {?} */
Pair.prototype.second;
}
/**
* @param {?} obj
* @return {?}
*/
function pairs(obj) {
var /** @type {?} */ res = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
res.push(new Pair(prop, obj[prop]));
}
}
return res;
}
var /** @type {?} */ SEGMENT_RE = /^[^\/()?;=&#]+/;
/**
* @param {?} str
* @return {?}
*/
function matchSegments(str) {
SEGMENT_RE.lastIndex = 0;
var /** @type {?} */ match = str.match(SEGMENT_RE);
return match ? match[0] : '';
}
var /** @type {?} */ QUERY_PARAM_RE = /^[^=?&#]+/;
/**
* @param {?} str
* @return {?}
*/
function matchQueryParams(str) {
QUERY_PARAM_RE.lastIndex = 0;
var /** @type {?} */ match = str.match(SEGMENT_RE);
return match ? match[0] : '';
}
var /** @type {?} */ QUERY_PARAM_VALUE_RE = /^[^?&#]+/;
/**
* @param {?} str
* @return {?}
*/
function matchUrlQueryParamValue(str) {
QUERY_PARAM_VALUE_RE.lastIndex = 0;
var /** @type {?} */ match = str.match(QUERY_PARAM_VALUE_RE);
return match ? match[0] : '';
}
var UrlParser = (function () {
/**
* @param {?} url
*/
function UrlParser(url) {
this.url = url;
this.remaining = url;
}
/**
* @param {?} str
* @return {?}
*/
UrlParser.prototype.peekStartsWith = function (str) { return this.remaining.startsWith(str); };
/**
* @param {?} str
* @return {?}
*/
UrlParser.prototype.capture = function (str) {
if (!this.remaining.startsWith(str)) {
throw new Error("Expected \"" + str + "\".");
}
this.remaining = this.remaining.substring(str.length);
};
/**
* @return {?}
*/
UrlParser.prototype.parseRootSegment = function () {
if (this.remaining.startsWith('/')) {
this.capture('/');
}
if (this.remaining === '' || this.remaining.startsWith('?') || this.remaining.startsWith('#')) {
return new UrlSegmentGroup([], {});
}
return new UrlSegmentGroup([], this.parseChildren());
};
/**
* @return {?}
*/
UrlParser.prototype.parseChildren = function () {
if (this.remaining.length == 0) {
return {};
}
if (this.peekStartsWith('/')) {
this.capture('/');
}
var /** @type {?} */ paths = [];
if (!this.peekStartsWith('(')) {
paths.push(this.parseSegments());
}
while (this.peekStartsWith('/') && !this.peekStartsWith('//') && !this.peekStartsWith('/(')) {
this.capture('/');
paths.push(this.parseSegments());
}
var /** @type {?} */ children = {};
if (this.peekStartsWith('/(')) {
this.capture('/');
children = this.parseParens(true);
}
var /** @type {?} */ res = {};
if (this.peekStartsWith('(')) {
res = this.parseParens(false);
}
if (paths.length > 0 || Object.keys(children).length > 0) {
res[PRIMARY_OUTLET] = new UrlSegmentGroup(paths, children);
}
return res;
};
/**
* @return {?}
*/
UrlParser.prototype.parseSegments = function () {
var /** @type {?} */ path = matchSegments(this.remaining);
if (path === '' && this.peekStartsWith(';')) {
throw new Error("Empty path url segment cannot have parameters: '" + this.remaining + "'.");
}
this.capture(path);
var /** @type {?} */ matrixParams = {};
if (this.peekStartsWith(';')) {
matrixParams = this.parseMatrixParams();
}
return new UrlSegment(decode(path), matrixParams);
};
/**
* @return {?}
*/
UrlParser.prototype.parseQueryParams = function () {
var /** @type {?} */ params = {};
if (this.peekStartsWith('?')) {
this.capture('?');
this.parseQueryParam(params);
while (this.remaining.length > 0 && this.peekStartsWith('&')) {
this.capture('&');
this.parseQueryParam(params);
}
}
return params;
};
/**
* @return {?}
*/
UrlParser.prototype.parseFragment = function () {
if (this.peekStartsWith('#')) {
return decodeURI(this.remaining.substring(1));
}
return null;
};
/**
* @return {?}
*/
UrlParser.prototype.parseMatrixParams = function () {
var /** @type {?} */ params = {};
while (this.remaining.length > 0 && this.peekStartsWith(';')) {
this.capture(';');
this.parseParam(params);
}
return params;
};
/**
* @param {?} params
* @return {?}
*/
UrlParser.prototype.parseParam = function (params) {
var /** @type {?} */ key = matchSegments(this.remaining);
if (!key) {
return;
}
this.capture(key);
var /** @type {?} */ value = '';
if (this.peekStartsWith('=')) {
this.capture('=');
var /** @type {?} */ valueMatch = matchSegments(this.remaining);
if (valueMatch) {
value = valueMatch;
this.capture(value);
}
}
params[decode(key)] = decode(value);
};
/**
* @param {?} params
* @return {?}
*/
UrlParser.prototype.parseQueryParam = function (params) {
var /** @type {?} */ key = matchQueryParams(this.remaining);
if (!key) {
return;
}
this.capture(key);
var /** @type {?} */ value = '';
if (this.peekStartsWith('=')) {
this.capture('=');
var /** @type {?} */ valueMatch = matchUrlQueryParamValue(this.remaining);
if (valueMatch) {
value = valueMatch;
this.capture(value);
}
}
var /** @type {?} */ decodedKey = decode(key);
var /** @type {?} */ decodedVal = decode(value);
if (params.hasOwnProperty(decodedKey)) {
// Append to existing values
var /** @type {?} */ currentVal = params[decodedKey];
if (!Array.isArray(currentVal)) {
currentVal = [currentVal];
params[decodedKey] = currentVal;
}
currentVal.push(decodedVal);
}
else {
// Create a new value
params[decodedKey] = decodedVal;
}
};
/**
* @param {?} allowPrimary
* @return {?}
*/
UrlParser.prototype.parseParens = function (allowPrimary) {
var /** @type {?} */ segments = {};
this.capture('(');
while (!this.peekStartsWith(')') && this.remaining.length > 0) {
var /** @type {?} */ path = matchSegments(this.remaining);
var /** @type {?} */ next = this.remaining[path.length];
// if is is not one of these characters, then the segment was unescaped
// or the group was not closed
if (next !== '/' && next !== ')' && next !== ';') {
throw new Error("Cannot parse url '" + this.url + "'");
}
var /** @type {?} */ outletName = void 0;
if (path.indexOf(':') > -1) {
outletName = path.substr(0, path.indexOf(':'));
this.capture(outletName);
this.capture(':');
}
else if (allowPrimary) {
outletName = PRIMARY_OUTLET;
}
var /** @type {?} */ children = this.parseChildren();
segments[outletName] = Object.keys(children).length === 1 ? children[PRIMARY_OUTLET] :
new UrlSegmentGroup([], children);
if (this.peekStartsWith('//')) {
this.capture('//');
}
}
this.capture(')');
return segments;
};
return UrlParser;
}());
function UrlParser_tsickle_Closure_declarations() {
/** @type {?} */
UrlParser.prototype.remaining;
/** @type {?} */
UrlParser.prototype.url;
}
//# sourceMappingURL=url_tree.js.map