feat(router): enforce usage of ... syntax for parent to child component routes

This commit is contained in:
Matias Niemelä
2015-06-17 11:57:38 -07:00
parent fa7a3e3449
commit 2d2ae9b8d8
8 changed files with 103 additions and 27 deletions

View File

@ -27,6 +27,10 @@ export class Segment {
regex: string;
}
export class ContinuationSegment extends Segment {
generate(params): string { return ''; }
}
class StaticSegment extends Segment {
regex: string;
name: string;
@ -71,7 +75,7 @@ var wildcardMatcher = RegExpWrapper.create("^\\*([^\/]+)$");
function parsePathString(route: string) {
// normalize route as not starting with a "/". Recognition will
// also normalize.
if (route[0] === "/") {
if (StringWrapper.startsWith(route, "/")) {
route = StringWrapper.substring(route, 1);
}
@ -93,7 +97,8 @@ function parsePathString(route: string) {
throw new BaseException(`'${route}' has more than the maximum supported number of segments.`);
}
for (var i = 0; i < segments.length; i++) {
var limit = segments.length - 1;
for (var i = 0; i <= limit; i++) {
var segment = segments[i], match;
if (isPresent(match = RegExpWrapper.firstMatch(paramMatcher, segment))) {
@ -101,6 +106,12 @@ function parsePathString(route: string) {
specificity += (100 - i);
} else if (isPresent(match = RegExpWrapper.firstMatch(wildcardMatcher, segment))) {
results.push(new StarSegment(match[1]));
} 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());
} else if (segment.length > 0) {
results.push(new StaticSegment(segment));
specificity += 100 * (100 - i);
@ -120,6 +131,7 @@ export class PathRecognizer {
segments: List<Segment>;
regex: RegExp;
specificity: number;
terminal: boolean = true;
constructor(public path: string, public handler: any) {
this.segments = [];
@ -131,7 +143,17 @@ export class PathRecognizer {
var segments = parsed['segments'];
var regexString = '^';
ListWrapper.forEach(segments, (segment) => { regexString += '/' + segment.regex; });
ListWrapper.forEach(segments, (segment) => {
if (segment instanceof ContinuationSegment) {
this.terminal = false;
} else {
regexString += '/' + segment.regex;
}
});
if (this.terminal) {
regexString += '$';
}
this.regex = RegExpWrapper.create(regexString);
this.segments = segments;
@ -143,6 +165,10 @@ export class PathRecognizer {
var urlPart = url;
for (var i = 0; i < this.segments.length; i++) {
var segment = this.segments[i];
if (segment instanceof ContinuationSegment) {
continue;
}
var match = RegExpWrapper.firstMatch(RegExpWrapper.create('/' + segment.regex), urlPart);
urlPart = StringWrapper.substring(urlPart, match[0].length);
if (segment.name.length > 0) {

View File

@ -14,7 +14,7 @@ import {
StringMapWrapper
} from 'angular2/src/facade/collection';
import {PathRecognizer} from './path_recognizer';
import {PathRecognizer, ContinuationSegment} from './path_recognizer';
/**
* `RouteRecognizer` is responsible for recognizing routes for a single component.
@ -32,9 +32,14 @@ export class RouteRecognizer {
this.redirects = new Map();
}
addRedirect(path: string, target: string): void { this.redirects.set(path, target); }
addRedirect(path: string, target: string): void {
if (path == '/') {
path = '';
}
this.redirects.set(path, target);
}
addConfig(path: string, handler: any, alias: string = null): void {
addConfig(path: string, handler: any, alias: string = null): boolean {
var recognizer = new PathRecognizer(path, handler);
MapWrapper.forEach(this.matchers, (matcher, _) => {
if (recognizer.regex.toString() == matcher.regex.toString()) {
@ -46,6 +51,7 @@ export class RouteRecognizer {
if (isPresent(alias)) {
this.names.set(alias, recognizer);
}
return recognizer.terminal;
}
@ -55,6 +61,9 @@ export class RouteRecognizer {
*/
recognize(url: string): List<RouteMatch> {
var solutions = [];
if (url.length > 0 && url[url.length - 1] == '/') {
url = url.substring(0, url.length - 1);
}
MapWrapper.forEach(this.redirects, (target, path) => {
// "/" redirect case

View File

@ -53,12 +53,17 @@ export class RouteRegistry {
config, {'component': normalizeComponentDeclaration(config['component'])});
var component = config['component'];
this.configFromComponent(component);
var terminal = recognizer.addConfig(config['path'], config, config['as']);
recognizer.addConfig(config['path'], config, config['as']);
if (component['type'] == 'constructor') {
if (terminal) {
assertTerminalComponent(component['constructor'], config['path']);
} else {
this.configFromComponent(component['constructor']);
}
}
}
/**
* Reads the annotations of a component and configures the registry based on them
*/
@ -221,3 +226,21 @@ function mostSpecific(instructions: List<Instruction>): Instruction {
}
return mostSpecificSolution;
}
function assertTerminalComponent(component, path) {
if (!isType(component)) {
return;
}
var annotations = reflector.annotations(component);
if (isPresent(annotations)) {
for (var i = 0; i < annotations.length; i++) {
var annotation = annotations[i];
if (annotation instanceof RouteConfig) {
throw new BaseException(
`Child routes are not allowed for "${path}". Use "..." on the parent's route path.`);
}
}
}
}