2015-05-29 14:58:41 -07:00
|
|
|
import {
|
|
|
|
RegExp,
|
|
|
|
RegExpWrapper,
|
|
|
|
RegExpMatcherWrapper,
|
|
|
|
StringWrapper,
|
|
|
|
isPresent,
|
|
|
|
isBlank,
|
2015-07-07 13:05:05 -07:00
|
|
|
BaseException
|
2015-05-29 14:58:41 -07:00
|
|
|
} from 'angular2/src/facade/lang';
|
2015-06-30 13:18:51 -07:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
2015-05-29 14:58:41 -07:00
|
|
|
import {
|
|
|
|
Map,
|
|
|
|
MapWrapper,
|
|
|
|
StringMap,
|
|
|
|
StringMapWrapper,
|
|
|
|
List,
|
|
|
|
ListWrapper
|
|
|
|
} from 'angular2/src/facade/collection';
|
|
|
|
import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
2015-07-21 01:26:43 -07:00
|
|
|
import {parseAndAssignParamString} from 'angular2/src/router/helpers';
|
2015-04-17 09:59:56 -07:00
|
|
|
import {escapeRegex} from './url';
|
2015-06-30 13:18:51 -07:00
|
|
|
import {RouteHandler} from './route_handler';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
// TODO(jeffbcross): implement as interface when ts2dart adds support:
|
|
|
|
// https://github.com/angular/ts2dart/issues/173
|
|
|
|
export class Segment {
|
|
|
|
name: string;
|
|
|
|
regex: string;
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: TouchMap): string { return ''; }
|
|
|
|
}
|
|
|
|
|
|
|
|
class TouchMap {
|
|
|
|
map: StringMap<string, string> = StringMapWrapper.create();
|
|
|
|
keys: StringMap<string, boolean> = StringMapWrapper.create();
|
|
|
|
|
|
|
|
constructor(map: StringMap<string, any>) {
|
|
|
|
if (isPresent(map)) {
|
|
|
|
StringMapWrapper.forEach(map, (value, key) => {
|
|
|
|
this.map[key] = isPresent(value) ? value.toString() : null;
|
|
|
|
this.keys[key] = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(key: string): string {
|
|
|
|
StringMapWrapper.delete(this.keys, key);
|
|
|
|
return this.map[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
getUnused(): StringMap<string, any> {
|
|
|
|
var unused: StringMap<string, any> = StringMapWrapper.create();
|
|
|
|
var keys = StringMapWrapper.keys(this.keys);
|
|
|
|
ListWrapper.forEach(keys, (key) => { unused[key] = StringMapWrapper.get(this.map, key); });
|
|
|
|
return unused;
|
|
|
|
}
|
2015-05-29 14:58:41 -07:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:05:05 -07:00
|
|
|
function normalizeString(obj: any): string {
|
|
|
|
if (isBlank(obj)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return obj.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 10:57:38 -07:00
|
|
|
class ContinuationSegment extends Segment {}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
class StaticSegment extends Segment {
|
|
|
|
regex: string;
|
2015-06-29 10:37:55 +02:00
|
|
|
name: string = '';
|
2015-05-14 15:24:35 +02:00
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
constructor(public string: string) {
|
|
|
|
super();
|
2015-04-17 09:59:56 -07:00
|
|
|
this.regex = escapeRegex(string);
|
2015-07-08 10:57:38 -07:00
|
|
|
|
|
|
|
// we add this property so that the route matcher still sees
|
|
|
|
// this segment as a valid path even if do not use the matrix
|
|
|
|
// parameters
|
|
|
|
this.regex += '(;[^\/]+)?';
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: TouchMap): string { return this.string; }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
@IMPLEMENTS(Segment)
|
2015-04-17 09:59:56 -07:00
|
|
|
class DynamicSegment {
|
2015-06-29 10:37:55 +02:00
|
|
|
regex: string = "([^/]+)";
|
|
|
|
|
|
|
|
constructor(public name: string) {}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: TouchMap): string {
|
|
|
|
if (!StringMapWrapper.contains(params.map, this.name)) {
|
2015-05-29 14:58:41 -07:00
|
|
|
throw new BaseException(
|
2015-06-30 13:18:51 -07:00
|
|
|
`Route generator for '${this.name}' was not included in parameters passed.`);
|
2015-05-06 18:30:37 -07:00
|
|
|
}
|
2015-07-08 10:57:38 -07:00
|
|
|
return normalizeString(params.get(this.name));
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class StarSegment {
|
2015-06-29 10:37:55 +02:00
|
|
|
regex: string = "(.+)";
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-06-29 10:37:55 +02:00
|
|
|
constructor(public name: string) {}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: TouchMap): string { return normalizeString(params.get(this.name)); }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-23 12:46:38 +02:00
|
|
|
var paramMatcher = /^:([^\/]+)$/g;
|
|
|
|
var wildcardMatcher = /^\*([^\/]+)$/g;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-06-26 11:10:52 -07:00
|
|
|
function parsePathString(route: string): StringMap<string, any> {
|
2015-04-17 09:59:56 -07:00
|
|
|
// normalize route as not starting with a "/". Recognition will
|
|
|
|
// also normalize.
|
2015-06-17 11:57:38 -07:00
|
|
|
if (StringWrapper.startsWith(route, "/")) {
|
2015-04-17 09:59:56 -07:00
|
|
|
route = StringWrapper.substring(route, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var segments = splitBySlash(route);
|
2015-06-17 11:17:21 -07:00
|
|
|
var results = [];
|
2015-05-15 02:05:57 -07:00
|
|
|
var specificity = 0;
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
// The "specificity" of a path is used to determine which route is used when multiple routes match
|
|
|
|
// a URL.
|
|
|
|
// Static segments (like "/foo") are the most specific, followed by dynamic segments (like
|
|
|
|
// "/:id"). Star segments
|
2015-05-15 02:05:57 -07:00
|
|
|
// add no specificity. Segments at the start of the path are more specific than proceeding ones.
|
2015-05-29 14:58:41 -07:00
|
|
|
// The code below uses place values to combine the different types of segments into a single
|
|
|
|
// integer that we can
|
|
|
|
// sort later. Each static segment is worth hundreds of points of specificity (10000, 9900, ...,
|
|
|
|
// 200), and each
|
2015-05-15 02:05:57 -07:00
|
|
|
// dynamic segment is worth single points of specificity (100, 99, ... 2).
|
|
|
|
if (segments.length > 98) {
|
|
|
|
throw new BaseException(`'${route}' has more than the maximum supported number of segments.`);
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-06-17 11:57:38 -07:00
|
|
|
var limit = segments.length - 1;
|
|
|
|
for (var i = 0; i <= limit; i++) {
|
2015-05-29 14:58:41 -07:00
|
|
|
var segment = segments[i], match;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
if (isPresent(match = RegExpWrapper.firstMatch(paramMatcher, segment))) {
|
2015-06-17 11:17:21 -07:00
|
|
|
results.push(new DynamicSegment(match[1]));
|
2015-05-15 02:05:57 -07:00
|
|
|
specificity += (100 - i);
|
2015-04-17 09:59:56 -07:00
|
|
|
} else if (isPresent(match = RegExpWrapper.firstMatch(wildcardMatcher, segment))) {
|
2015-06-17 11:17:21 -07:00
|
|
|
results.push(new StarSegment(match[1]));
|
2015-06-17 11:57:38 -07:00
|
|
|
} else if (segment == '...') {
|
|
|
|
if (i < limit) {
|
|
|
|
// TODO (matsko): setup a proper error here `
|
|
|
|
throw new BaseException(`Unexpected "..." before the end of the path for "${route}".`);
|
|
|
|
}
|
|
|
|
results.push(new ContinuationSegment());
|
2015-04-17 09:59:56 -07:00
|
|
|
} else if (segment.length > 0) {
|
2015-06-17 11:17:21 -07:00
|
|
|
results.push(new StaticSegment(segment));
|
2015-05-15 02:05:57 -07:00
|
|
|
specificity += 100 * (100 - i);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 11:10:52 -07:00
|
|
|
var result = StringMapWrapper.create();
|
|
|
|
StringMapWrapper.set(result, 'segments', results);
|
|
|
|
StringMapWrapper.set(result, 'specificity', specificity);
|
|
|
|
return result;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
function splitBySlash(url: string): List<string> {
|
2015-05-14 15:24:35 +02:00
|
|
|
return url.split('/');
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 02:01:34 -07:00
|
|
|
var RESERVED_CHARS = RegExpWrapper.create('//|\\(|\\)|;|\\?|=');
|
|
|
|
function assertPath(path: string) {
|
|
|
|
if (StringWrapper.contains(path, '#')) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Path "${path}" should not include "#". Use "HashLocationStrategy" instead.`);
|
|
|
|
}
|
|
|
|
var illegalCharacter = RegExpWrapper.firstMatch(RESERVED_CHARS, path);
|
|
|
|
if (isPresent(illegalCharacter)) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Path "${path}" contains "${illegalCharacter[0]}" which is not allowed in a route config.`);
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
// represents something like '/foo/:bar'
|
|
|
|
export class PathRecognizer {
|
2015-05-29 14:58:41 -07:00
|
|
|
segments: List<Segment>;
|
|
|
|
regex: RegExp;
|
|
|
|
specificity: number;
|
2015-06-17 11:57:38 -07:00
|
|
|
terminal: boolean = true;
|
2015-05-29 14:58:41 -07:00
|
|
|
|
2015-07-21 01:26:43 -07:00
|
|
|
static matrixRegex: RegExp = RegExpWrapper.create('^(.*\/[^\/]+?)(;[^\/]+)?\/?$');
|
|
|
|
static queryRegex: RegExp = RegExpWrapper.create('^(.*\/[^\/]+?)(\\?[^\/]+)?$');
|
|
|
|
|
|
|
|
constructor(public path: string, public handler: RouteHandler, public isRoot: boolean = false) {
|
2015-07-17 02:01:34 -07:00
|
|
|
assertPath(path);
|
2015-05-12 14:53:13 -07:00
|
|
|
var parsed = parsePathString(path);
|
2015-05-15 02:05:57 -07:00
|
|
|
var specificity = parsed['specificity'];
|
2015-05-12 14:53:13 -07:00
|
|
|
var segments = parsed['segments'];
|
2015-04-17 09:59:56 -07:00
|
|
|
var regexString = '^';
|
|
|
|
|
2015-06-17 11:57:38 -07:00
|
|
|
ListWrapper.forEach(segments, (segment) => {
|
|
|
|
if (segment instanceof ContinuationSegment) {
|
|
|
|
this.terminal = false;
|
|
|
|
} else {
|
|
|
|
regexString += '/' + segment.regex;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.terminal) {
|
|
|
|
regexString += '$';
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
this.regex = RegExpWrapper.create(regexString);
|
|
|
|
this.segments = segments;
|
2015-05-15 02:05:57 -07:00
|
|
|
this.specificity = specificity;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
parseParams(url: string): StringMap<string, string> {
|
2015-07-08 10:57:38 -07:00
|
|
|
// the last segment is always the star one since it's terminal
|
|
|
|
var segmentsLimit = this.segments.length - 1;
|
|
|
|
var containsStarSegment =
|
|
|
|
segmentsLimit >= 0 && this.segments[segmentsLimit] instanceof StarSegment;
|
|
|
|
|
2015-07-21 01:26:43 -07:00
|
|
|
var paramsString, useQueryString = this.isRoot && this.terminal;
|
2015-07-08 10:57:38 -07:00
|
|
|
if (!containsStarSegment) {
|
2015-07-21 01:26:43 -07:00
|
|
|
var matches = RegExpWrapper.firstMatch(
|
|
|
|
useQueryString ? PathRecognizer.queryRegex : PathRecognizer.matrixRegex, url);
|
2015-07-08 10:57:38 -07:00
|
|
|
if (isPresent(matches)) {
|
|
|
|
url = matches[1];
|
2015-07-21 01:26:43 -07:00
|
|
|
paramsString = matches[2];
|
2015-07-08 10:57:38 -07:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:26:43 -07:00
|
|
|
url = StringWrapper.replaceAll(url, /(;[^\/]+)(?=(\/|$))/g, '');
|
2015-07-08 10:57:38 -07:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
var params = StringMapWrapper.create();
|
|
|
|
var urlPart = url;
|
2015-07-08 10:57:38 -07:00
|
|
|
|
|
|
|
for (var i = 0; i <= segmentsLimit; i++) {
|
2015-04-17 09:59:56 -07:00
|
|
|
var segment = this.segments[i];
|
2015-06-17 11:57:38 -07:00
|
|
|
if (segment instanceof ContinuationSegment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
var match = RegExpWrapper.firstMatch(RegExpWrapper.create('/' + segment.regex), urlPart);
|
|
|
|
urlPart = StringWrapper.substring(urlPart, match[0].length);
|
|
|
|
if (segment.name.length > 0) {
|
2015-07-08 10:57:38 -07:00
|
|
|
params[segment.name] = match[1];
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:26:43 -07:00
|
|
|
if (isPresent(paramsString) && paramsString.length > 0) {
|
|
|
|
var expectedStartingValue = useQueryString ? '?' : ';';
|
|
|
|
if (paramsString[0] == expectedStartingValue) {
|
|
|
|
parseAndAssignParamString(expectedStartingValue, paramsString, params);
|
|
|
|
}
|
2015-07-08 10:57:38 -07:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: StringMap<string, any>): string {
|
|
|
|
var paramTokens = new TouchMap(params);
|
|
|
|
var applyLeadingSlash = false;
|
2015-07-21 01:26:43 -07:00
|
|
|
var useQueryString = this.isRoot && this.terminal;
|
2015-07-08 10:57:38 -07:00
|
|
|
|
|
|
|
var url = '';
|
|
|
|
for (var i = 0; i < this.segments.length; i++) {
|
|
|
|
let segment = this.segments[i];
|
|
|
|
let s = segment.generate(paramTokens);
|
|
|
|
applyLeadingSlash = applyLeadingSlash || (segment instanceof ContinuationSegment);
|
|
|
|
|
|
|
|
if (s.length > 0) {
|
|
|
|
url += (i > 0 ? '/' : '') + s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var unusedParams = paramTokens.getUnused();
|
2015-07-21 01:26:43 -07:00
|
|
|
if (!StringMapWrapper.isEmpty(unusedParams)) {
|
|
|
|
url += useQueryString ? '?' : ';';
|
|
|
|
var paramToken = useQueryString ? '&' : ';';
|
|
|
|
var i = 0;
|
|
|
|
StringMapWrapper.forEach(unusedParams, (value, key) => {
|
|
|
|
if (i++ > 0) {
|
|
|
|
url += paramToken;
|
|
|
|
}
|
|
|
|
url += key;
|
|
|
|
if (!isPresent(value) && useQueryString) {
|
|
|
|
value = 'true';
|
|
|
|
}
|
|
|
|
if (isPresent(value)) {
|
|
|
|
url += '=' + value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-08 10:57:38 -07:00
|
|
|
|
|
|
|
if (applyLeadingSlash) {
|
|
|
|
url += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
2015-06-30 13:18:51 -07:00
|
|
|
|
|
|
|
resolveComponentType(): Promise<any> { return this.handler.resolveComponentType(); }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|