feat(regex_url_paths): add regex_group_names
to handle consistency with serializers
By adding `regex_named_groups` to regex route configurations we can consistently map regex matching groups to component parameters. This should fix #7554. Closes #7694
This commit is contained in:

committed by
Misko Hevery

parent
1f7449ccf4
commit
ce013a3dd9
@ -22,14 +22,17 @@ export abstract class AbstractRoute implements RouteDefinition {
|
||||
useAsDefault: boolean;
|
||||
path: string;
|
||||
regex: string;
|
||||
regex_group_names: string[];
|
||||
serializer: RegexSerializer;
|
||||
data: {[key: string]: any};
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data}: RouteDefinition) {
|
||||
constructor({name, useAsDefault, path, regex, regex_group_names, serializer,
|
||||
data}: RouteDefinition) {
|
||||
this.name = name;
|
||||
this.useAsDefault = useAsDefault;
|
||||
this.path = path;
|
||||
this.regex = regex;
|
||||
this.regex_group_names = regex_group_names;
|
||||
this.serializer = serializer;
|
||||
this.data = data;
|
||||
}
|
||||
@ -62,12 +65,14 @@ export class Route extends AbstractRoute {
|
||||
component: any;
|
||||
aux: string = null;
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, component}: RouteDefinition) {
|
||||
constructor({name, useAsDefault, path, regex, regex_group_names, serializer, data,
|
||||
component}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
regex_group_names: regex_group_names,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
@ -99,12 +104,14 @@ export class Route extends AbstractRoute {
|
||||
export class AuxRoute extends AbstractRoute {
|
||||
component: any;
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, component}: RouteDefinition) {
|
||||
constructor({name, useAsDefault, path, regex, regex_group_names, serializer, data,
|
||||
component}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
regex_group_names: regex_group_names,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
@ -141,12 +148,14 @@ export class AsyncRoute extends AbstractRoute {
|
||||
loader: () => Promise<Type>;
|
||||
aux: string = null;
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, loader}: RouteDefinition) {
|
||||
constructor({name, useAsDefault, path, regex, regex_group_names, serializer, data,
|
||||
loader}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
regex_group_names: regex_group_names,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
@ -179,12 +188,14 @@ export class AsyncRoute extends AbstractRoute {
|
||||
export class Redirect extends AbstractRoute {
|
||||
redirectTo: any[];
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, redirectTo}: RouteDefinition) {
|
||||
constructor({name, useAsDefault, path, regex, regex_group_names, serializer, data,
|
||||
redirectTo}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
regex_group_names: regex_group_names,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
|
@ -5,6 +5,7 @@ abstract class RouteDefinition {
|
||||
final String name;
|
||||
final bool useAsDefault;
|
||||
final String regex;
|
||||
final List<String> regex_group_names;
|
||||
final Function serializer;
|
||||
const RouteDefinition({this.path, this.name, this.useAsDefault : false, this.regex, this.serializer});
|
||||
const RouteDefinition({this.path, this.name, this.useAsDefault : false, this.regex, this.regex_group_names, this.serializer});
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ export interface RouteDefinition {
|
||||
path?: string;
|
||||
aux?: string;
|
||||
regex?: string;
|
||||
regex_group_names?: string[];
|
||||
serializer?: RegexSerializer;
|
||||
component?: Type | ComponentDefinition;
|
||||
loader?: () => Promise<Type>;
|
||||
|
@ -1,10 +1,21 @@
|
||||
import {RegExpWrapper, RegExpMatcherWrapper, isBlank} from '../../../src/facade/lang';
|
||||
import {BaseException} from '@angular/core';
|
||||
import {Url} from '../../url_parser';
|
||||
import {RoutePath, GeneratedUrl, MatchedUrl} from './route_path';
|
||||
|
||||
|
||||
export interface RegexSerializer { (params: {[key: string]: any}): GeneratedUrl; }
|
||||
|
||||
function computeNumberOfRegexGroups(regex: string): number {
|
||||
// cleverly compute regex groups by appending an alternative empty matching
|
||||
// pattern and match against an empty string, the resulting match still
|
||||
// receives all the other groups
|
||||
var test_regex = RegExpWrapper.create(regex + "|");
|
||||
var matcher = RegExpWrapper.matcher(test_regex, '');
|
||||
var match = RegExpMatcherWrapper.next(matcher);
|
||||
return match.length;
|
||||
}
|
||||
|
||||
export class RegexRoutePath implements RoutePath {
|
||||
public hash: string;
|
||||
public terminal: boolean = true;
|
||||
@ -12,9 +23,19 @@ export class RegexRoutePath implements RoutePath {
|
||||
|
||||
private _regex: RegExp;
|
||||
|
||||
constructor(private _reString: string, private _serializer: RegexSerializer) {
|
||||
constructor(private _reString: string, private _serializer: RegexSerializer,
|
||||
private _groupNames?: Array<string>) {
|
||||
this.hash = this._reString;
|
||||
this._regex = RegExpWrapper.create(this._reString);
|
||||
if (this._groupNames != null) {
|
||||
var groups = computeNumberOfRegexGroups(this._reString);
|
||||
if (groups != _groupNames.length) {
|
||||
throw new BaseException(
|
||||
`Regex group names [${this._groupNames.join(',')}] must contain names for \
|
||||
each matching group and a name for the complete match as its first element of regex \
|
||||
'${this._reString}'. ${groups} group names are expected.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
matchUrl(url: Url): MatchedUrl {
|
||||
@ -28,7 +49,7 @@ export class RegexRoutePath implements RoutePath {
|
||||
}
|
||||
|
||||
for (let i = 0; i < match.length; i += 1) {
|
||||
params[i.toString()] = match[i];
|
||||
params[this._groupNames != null ? this._groupNames[i] : i.toString()] = match[i];
|
||||
}
|
||||
|
||||
return new MatchedUrl(urlPath, [], params, [], null);
|
||||
|
@ -169,7 +169,7 @@ export class RuleSet {
|
||||
private _getRoutePath(config: RouteDefinition): RoutePath {
|
||||
if (isPresent(config.regex)) {
|
||||
if (isFunction(config.serializer)) {
|
||||
return new RegexRoutePath(config.regex, config.serializer);
|
||||
return new RegexRoutePath(config.regex, config.serializer, config.regex_group_names);
|
||||
} else {
|
||||
throw new BaseException(
|
||||
`Route provides a regex property, '${config.regex}', but no serializer property`);
|
||||
|
Reference in New Issue
Block a user