fix(router): handle path:'' redirects and matches

This commit is contained in:
vsavkin
2016-06-24 11:17:17 -07:00
parent f463e09b9f
commit fbd2dd9ca2
7 changed files with 645 additions and 293 deletions

View File

@ -13,6 +13,7 @@ import {of } from 'rxjs/observable/of';
import {Route, RouterConfig} from './config';
import {PRIMARY_OUTLET} from './shared';
import {UrlPathWithParams, UrlSegment, UrlTree, mapChildren} from './url_tree';
import {merge} from './utils/collection';
class NoMatch {
constructor(public segment: UrlSegment = null) {}
@ -38,7 +39,10 @@ export function applyRedirects(urlTree: UrlTree, config: RouterConfig): Observab
}
}
function createUrlTree(urlTree: UrlTree, root: UrlSegment): Observable<UrlTree> {
function createUrlTree(urlTree: UrlTree, rootCandidate: UrlSegment): Observable<UrlTree> {
const root = rootCandidate.pathsWithParams.length > 0 ?
new UrlSegment([], {[PRIMARY_OUTLET]: rootCandidate}) :
rootCandidate;
return of (new UrlTree(root, urlTree.queryParams, urlTree.fragment));
}
@ -70,10 +74,10 @@ function expandPathsWithParams(
function expandPathsWithParamsAgainstRoute(
segment: UrlSegment, routes: Route[], route: Route, paths: UrlPathWithParams[], outlet: string,
allowRedirects: boolean): UrlSegment {
if ((route.outlet ? route.outlet : PRIMARY_OUTLET) !== outlet) throw new NoMatch();
if (route.redirectTo && !allowRedirects) throw new NoMatch();
if (getOutlet(route) !== outlet) throw new NoMatch();
if (route.redirectTo !== undefined && !allowRedirects) throw new NoMatch();
if (route.redirectTo) {
if (route.redirectTo !== undefined) {
return expandPathsWithParamsAgainstRouteUsingRedirect(segment, routes, route, paths, outlet);
} else {
return matchPathsWithParamsAgainstRoute(segment, route, paths);
@ -115,22 +119,23 @@ function expandRegularPathWithParamsAgainstRouteUsingRedirect(
}
function matchPathsWithParamsAgainstRoute(
segment: UrlSegment, route: Route, paths: UrlPathWithParams[]): UrlSegment {
rawSegment: UrlSegment, route: Route, paths: UrlPathWithParams[]): UrlSegment {
if (route.path === '**') {
return new UrlSegment(paths, {});
} else {
const {consumedPaths, lastChild} = match(segment, route, paths);
const {consumedPaths, lastChild} = match(rawSegment, route, paths);
const childConfig = route.children ? route.children : [];
const slicedPath = paths.slice(lastChild);
const rawSlicedPath = paths.slice(lastChild);
if (childConfig.length === 0 && slicedPath.length === 0) {
return new UrlSegment(consumedPaths, {});
const {segment, slicedPath} = split(rawSegment, consumedPaths, rawSlicedPath, childConfig);
// TODO: check that the right segment is present
} else if (slicedPath.length === 0 && segment.hasChildren()) {
if (slicedPath.length === 0 && segment.hasChildren()) {
const children = expandSegmentChildren(childConfig, segment);
return new UrlSegment(consumedPaths, children);
} else if (childConfig.length === 0 && slicedPath.length === 0) {
return new UrlSegment(consumedPaths, {});
} else {
const cs = expandPathsWithParams(segment, childConfig, slicedPath, PRIMARY_OUTLET, true);
return new UrlSegment(consumedPaths.concat(cs.pathsWithParams), cs.children);
@ -183,12 +188,11 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]): {
function applyRedirectCommands(
paths: UrlPathWithParams[], redirectTo: string,
posParams: {[k: string]: UrlPathWithParams}): UrlPathWithParams[] {
if (redirectTo.startsWith('/')) {
const parts = redirectTo.substring(1).split('/');
return createPaths(redirectTo, parts, paths, posParams);
const r = redirectTo.startsWith('/') ? redirectTo.substring(1) : redirectTo;
if (r === '') {
return [];
} else {
const parts = redirectTo.split('/');
return createPaths(redirectTo, parts, paths, posParams);
return createPaths(redirectTo, r.split('/'), paths, posParams);
}
}
@ -219,3 +223,72 @@ function findOrCreatePath(part: string, paths: UrlPathWithParams[]): UrlPathWith
return new UrlPathWithParams(part, {});
}
}
function split(
segment: UrlSegment, consumedPaths: UrlPathWithParams[], slicedPath: UrlPathWithParams[],
config: Route[]) {
if (slicedPath.length > 0 &&
containsEmptyPathRedirectsWithNamedOutlets(segment, slicedPath, config)) {
const s = new UrlSegment(
consumedPaths,
createChildrenForEmptyPaths(config, new UrlSegment(slicedPath, segment.children)));
return {segment: s, slicedPath: []};
} else if (slicedPath.length === 0 && containsEmptyPathRedirects(segment, slicedPath, config)) {
const s = new UrlSegment(
segment.pathsWithParams,
addEmptyPathsToChildrenIfNeeded(segment, slicedPath, config, segment.children));
return {segment: s, slicedPath};
} else {
return {segment, slicedPath};
}
}
function addEmptyPathsToChildrenIfNeeded(
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[],
children: {[name: string]: UrlSegment}): {[name: string]: UrlSegment} {
const res: {[name: string]: UrlSegment} = {};
for (let r of routes) {
if (emptyPathRedirect(segment, slicedPath, r) && !children[getOutlet(r)]) {
res[getOutlet(r)] = new UrlSegment([], {});
}
}
return merge(children, res);
}
function createChildrenForEmptyPaths(
routes: Route[], primarySegment: UrlSegment): {[name: string]: UrlSegment} {
const res: {[name: string]: UrlSegment} = {};
res[PRIMARY_OUTLET] = primarySegment;
for (let r of routes) {
if (r.path === '') {
res[getOutlet(r)] = new UrlSegment([], {});
}
}
return res;
}
function containsEmptyPathRedirectsWithNamedOutlets(
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[]): boolean {
return routes
.filter(
r => emptyPathRedirect(segment, slicedPath, r) && getOutlet(r) !== PRIMARY_OUTLET)
.length > 0;
}
function containsEmptyPathRedirects(
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[]): boolean {
return routes.filter(r => emptyPathRedirect(segment, slicedPath, r)).length > 0;
}
function emptyPathRedirect(
segment: UrlSegment, slicedPath: UrlPathWithParams[], r: Route): boolean {
if ((segment.hasChildren() || slicedPath.length > 0) && r.terminal) return false;
return r.path === '' && r.redirectTo !== undefined;
}
function getOutlet(route: Route): string {
return route.outlet ? route.outlet : PRIMARY_OUTLET;
}

View File

@ -48,8 +48,7 @@ function processSegment(config: Route[], segment: UrlSegment, extraParams: Param
if (segment.pathsWithParams.length === 0 && segment.hasChildren()) {
return processSegmentChildren(config, segment, extraParams);
} else {
return [processPathsWithParams(
config, segment, 0, segment.pathsWithParams, extraParams, outlet)];
return processPathsWithParams(config, segment, 0, segment.pathsWithParams, extraParams, outlet);
}
}
@ -72,7 +71,7 @@ function sortActivatedRouteSnapshots(nodes: TreeNode<ActivatedRouteSnapshot>[]):
function processPathsWithParams(
config: Route[], segment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
extraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot> {
extraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
for (let r of config) {
try {
return processPathsWithParamsAgainstRoute(r, segment, pathIndex, paths, extraParams, outlet);
@ -84,8 +83,8 @@ function processPathsWithParams(
}
function processPathsWithParamsAgainstRoute(
route: Route, segment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
parentExtraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot> {
route: Route, rawSegment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
parentExtraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
if (route.redirectTo) throw new NoMatch();
if ((route.outlet ? route.outlet : PRIMARY_OUTLET) !== outlet) throw new NoMatch();
@ -93,30 +92,33 @@ function processPathsWithParamsAgainstRoute(
if (route.path === '**') {
const params = paths.length > 0 ? last(paths).parameters : {};
const snapshot = new ActivatedRouteSnapshot(
paths, merge(parentExtraParams, params), outlet, route.component, route, segment, -1);
return new TreeNode<ActivatedRouteSnapshot>(snapshot, []);
paths, merge(parentExtraParams, params), outlet, route.component, route,
getSourceSegment(rawSegment), getPathIndexShift(rawSegment) - 1);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
}
const {consumedPaths, parameters, extraParams, lastChild} =
match(segment, route, paths, parentExtraParams);
const snapshot = new ActivatedRouteSnapshot(
consumedPaths, parameters, outlet, route.component, route, segment,
pathIndex + lastChild - 1);
const slicedPath = paths.slice(lastChild);
match(rawSegment, route, paths, parentExtraParams);
const rawSlicedPath = paths.slice(lastChild);
const childConfig = route.children ? route.children : [];
if (childConfig.length === 0 && slicedPath.length === 0) {
return new TreeNode<ActivatedRouteSnapshot>(snapshot, []);
const {segment, slicedPath} = split(rawSegment, consumedPaths, rawSlicedPath, childConfig);
// TODO: check that the right segment is present
} else if (slicedPath.length === 0 && segment.hasChildren()) {
const snapshot = new ActivatedRouteSnapshot(
consumedPaths, parameters, outlet, route.component, route, getSourceSegment(rawSegment),
getPathIndexShift(rawSegment) + pathIndex + lastChild - 1);
if (slicedPath.length === 0 && segment.hasChildren()) {
const children = processSegmentChildren(childConfig, segment, extraParams);
return new TreeNode<ActivatedRouteSnapshot>(snapshot, children);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
} else if (childConfig.length === 0 && slicedPath.length === 0) {
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
} else {
const child = processPathsWithParams(
const children = processPathsWithParams(
childConfig, segment, pathIndex + lastChild, slicedPath, extraParams, PRIMARY_OUTLET);
return new TreeNode<ActivatedRouteSnapshot>(snapshot, [child]);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
}
}
@ -173,4 +175,103 @@ function checkOutletNameUniqueness(nodes: TreeNode<ActivatedRouteSnapshot>[]): v
}
names[n.value.outlet] = n.value;
});
}
function getSourceSegment(segment: UrlSegment): UrlSegment {
let s = segment;
while (s._sourceSegment) {
s = s._sourceSegment;
}
return s;
}
function getPathIndexShift(segment: UrlSegment): number {
let s = segment;
let res = 0;
while (s._sourceSegment) {
s = s._sourceSegment;
res += segment._pathIndexShift;
}
return res;
}
function split(
segment: UrlSegment, consumedPaths: UrlPathWithParams[], slicedPath: UrlPathWithParams[],
config: Route[]) {
if (slicedPath.length > 0 &&
containsEmptyPathMatchesWithNamedOutlets(segment, slicedPath, config)) {
const s = new UrlSegment(
consumedPaths,
createChildrenForEmptyPaths(
segment, consumedPaths, config, new UrlSegment(slicedPath, segment.children)));
s._sourceSegment = segment;
s._pathIndexShift = 0;
return {segment: s, slicedPath: []};
} else if (slicedPath.length === 0 && containsEmptyPathMatches(segment, slicedPath, config)) {
const s = new UrlSegment(
segment.pathsWithParams,
addEmptyPathsToChildrenIfNeeded(segment, slicedPath, config, segment.children));
s._sourceSegment = segment;
s._pathIndexShift = 0;
return {segment: s, slicedPath};
} else {
return {segment, slicedPath};
}
}
function addEmptyPathsToChildrenIfNeeded(
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[],
children: {[name: string]: UrlSegment}): {[name: string]: UrlSegment} {
const res: {[name: string]: UrlSegment} = {};
for (let r of routes) {
if (emptyPathMatch(segment, slicedPath, r) && !children[getOutlet(r)]) {
const s = new UrlSegment([], {});
s._sourceSegment = segment;
s._pathIndexShift = segment.pathsWithParams.length;
res[getOutlet(r)] = s;
}
}
return merge(children, res);
}
function createChildrenForEmptyPaths(
segment: UrlSegment, consumedPaths: UrlPathWithParams[], routes: Route[],
primarySegment: UrlSegment): {[name: string]: UrlSegment} {
const res: {[name: string]: UrlSegment} = {};
res[PRIMARY_OUTLET] = primarySegment;
primarySegment._sourceSegment = segment;
primarySegment._pathIndexShift = consumedPaths.length;
for (let r of routes) {
if (r.path === '') {
const s = new UrlSegment([], {});
s._sourceSegment = segment;
s._pathIndexShift = consumedPaths.length;
res[getOutlet(r)] = s;
}
}
return res;
}
function containsEmptyPathMatchesWithNamedOutlets(
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[]): boolean {
return routes
.filter(r => emptyPathMatch(segment, slicedPath, r) && getOutlet(r) !== PRIMARY_OUTLET)
.length > 0;
}
function containsEmptyPathMatches(
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[]): boolean {
return routes.filter(r => emptyPathMatch(segment, slicedPath, r)).length > 0;
}
function emptyPathMatch(segment: UrlSegment, slicedPath: UrlPathWithParams[], r: Route): boolean {
if ((segment.hasChildren() || slicedPath.length > 0) && r.terminal) return false;
return r.path === '' && r.redirectTo === undefined;
}
function getOutlet(route: Route): string {
return route.outlet ? route.outlet : PRIMARY_OUTLET;
}

View File

@ -75,6 +75,16 @@ export class UrlTree {
}
export class UrlSegment {
/**
* @internal
*/
_sourceSegment: UrlSegment;
/**
* @internal
*/
_pathIndexShift: number;
public parent: UrlSegment = null;
constructor(
public pathsWithParams: UrlPathWithParams[], public children: {[key: string]: UrlSegment}) {
@ -306,7 +316,11 @@ class UrlParser {
}
parsePathWithParams(): UrlPathWithParams {
let path = matchPathWithParams(this.remaining);
const path = matchPathWithParams(this.remaining);
if (path === '' && this.peekStartsWith(';')) {
throw new Error(`Empty path url segment cannot have parameters: '${this.remaining}'.`);
}
this.capture(path);
let matrixParams: {[key: string]: any} = {};
if (this.peekStartsWith(';')) {