forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes angular#2775
- Loading branch information
Showing
22 changed files
with
1,404 additions
and
954 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
import {List, StringMap, StringMapWrapper} from 'angular2/src/facade/collection'; | ||
import { | ||
isPresent, | ||
isBlank, | ||
BaseException, | ||
RegExpWrapper, | ||
CONST_EXPR | ||
} from 'angular2/src/facade/lang'; | ||
|
||
|
||
export class ParsedUrl { | ||
constructor(public path: string, public child: ParsedUrl = null, | ||
public auxiliary: List<ParsedUrl> = CONST_EXPR([]), | ||
public params: StringMap<string, any> = null) {} | ||
|
||
toString(): string { | ||
return this.path + this._matrixParamsToString() + this._auxToString() + this._childString(); | ||
} | ||
|
||
segmentToString(): string { return this.path + this._matrixParamsToString(); } | ||
|
||
_auxToString(): string { | ||
return this.auxiliary.length > 0 ? | ||
('(' + this.auxiliary.map(sibling => sibling.toString()).join('//') + ')') : | ||
''; | ||
} | ||
|
||
private _matrixParamsToString(): string { | ||
if (isBlank(this.params)) { | ||
return ''; | ||
} | ||
|
||
return ';' + serializeParams(this.params).join(';'); | ||
} | ||
|
||
_childString(): string { return isPresent(this.child) ? ('/' + this.child.toString()) : ''; } | ||
} | ||
|
||
export class RootParsedUrl extends ParsedUrl { | ||
constructor(path: string, child: ParsedUrl = null, auxiliary: List<ParsedUrl> = CONST_EXPR([]), | ||
params: StringMap<string, any> = null) { | ||
super(path, child, auxiliary, params); | ||
} | ||
|
||
toString(): string { | ||
return this.path + this._auxToString() + this._childString() + this._queryParamsToString(); | ||
} | ||
|
||
segmentToString(): string { return this.path + this._queryParamsToString(); } | ||
|
||
private _queryParamsToString(): string { | ||
if (isBlank(this.params)) { | ||
return ''; | ||
} | ||
|
||
return '?' + serializeParams(this.params).join('&'); | ||
} | ||
} | ||
|
||
var SEGMENT_RE = RegExpWrapper.create('^[^\\/\\(\\)\\?;=&]+'); | ||
function matchUrlSegment(str: string): string { | ||
var match = RegExpWrapper.firstMatch(SEGMENT_RE, str); | ||
return isPresent(match) ? match[0] : null; | ||
} | ||
|
||
export class UrlParser { | ||
private remaining: string; | ||
|
||
peekStartsWith(str: string): boolean { return this.remaining.startsWith(str); } | ||
|
||
capture(str: string): void { | ||
if (!this.remaining.startsWith(str)) { | ||
throw new BaseException(`Expected "${str}".`); | ||
} | ||
this.remaining = this.remaining.substring(str.length); | ||
} | ||
|
||
parse(url: string): ParsedUrl { | ||
this.remaining = url; | ||
if (url == '' || url == '/') { | ||
return new ParsedUrl(''); | ||
} | ||
return this.parseRoot(); | ||
} | ||
|
||
// segment + (aux segments) + (query params) | ||
parseRoot(): ParsedUrl { | ||
if (this.peekStartsWith('/')) { | ||
this.capture('/'); | ||
} | ||
var path = matchUrlSegment(this.remaining); | ||
this.capture(path); | ||
|
||
var aux = []; | ||
if (this.peekStartsWith('(')) { | ||
aux = this.parseAuxiliaryRoutes(); | ||
} | ||
if (this.peekStartsWith(';')) { | ||
// TODO: should these params just be dropped? | ||
this.parseMatrixParams(); | ||
} | ||
var child = null; | ||
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) { | ||
this.capture('/'); | ||
child = this.parseSegment(); | ||
} | ||
var queryParams = null; | ||
if (this.peekStartsWith('?')) { | ||
queryParams = this.parseQueryParams(); | ||
} | ||
return new RootParsedUrl(path, child, aux, queryParams); | ||
} | ||
|
||
// segment + (matrix params) + (aux segments) | ||
parseSegment(): ParsedUrl { | ||
if (this.remaining.length == 0) { | ||
return null; | ||
} | ||
if (this.peekStartsWith('/')) { | ||
this.capture('/'); | ||
} | ||
var path = matchUrlSegment(this.remaining); | ||
this.capture(path); | ||
|
||
var matrixParams = null; | ||
if (this.peekStartsWith(';')) { | ||
matrixParams = this.parseMatrixParams(); | ||
} | ||
var aux = []; | ||
if (this.peekStartsWith('(')) { | ||
aux = this.parseAuxiliaryRoutes(); | ||
} | ||
var child = null; | ||
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) { | ||
this.capture('/'); | ||
child = this.parseSegment(); | ||
} | ||
return new ParsedUrl(path, child, aux, matrixParams); | ||
} | ||
|
||
parseQueryParams(): StringMap<string, any> { | ||
var params = {}; | ||
this.capture('?'); | ||
this.parseParam(params); | ||
while (this.remaining.length > 0 && this.peekStartsWith('&')) { | ||
this.capture('&'); | ||
this.parseParam(params); | ||
} | ||
return params; | ||
} | ||
|
||
parseMatrixParams(): StringMap<string, any> { | ||
var params = {}; | ||
while (this.remaining.length > 0 && this.peekStartsWith(';')) { | ||
this.capture(';'); | ||
this.parseParam(params); | ||
} | ||
return params; | ||
} | ||
|
||
parseParam(params: StringMap<string, any>): void { | ||
var key = matchUrlSegment(this.remaining); | ||
if (isBlank(key)) { | ||
return; | ||
} | ||
this.capture(key); | ||
var value: any = true; | ||
if (this.peekStartsWith('=')) { | ||
this.capture('='); | ||
var valueMatch = matchUrlSegment(this.remaining); | ||
if (isPresent(valueMatch)) { | ||
value = valueMatch; | ||
this.capture(value); | ||
} | ||
} | ||
|
||
params[key] = value; | ||
} | ||
|
||
parseAuxiliaryRoutes(): List<ParsedUrl> { | ||
var routes = []; | ||
this.capture('('); | ||
|
||
while (!this.peekStartsWith(')') && this.remaining.length > 0) { | ||
routes.push(this.parseSegment()); | ||
if (this.peekStartsWith('//')) { | ||
this.capture('//'); | ||
} | ||
} | ||
this.capture(')'); | ||
|
||
return routes; | ||
} | ||
} | ||
|
||
export var parser = new UrlParser(); | ||
|
||
export function serializeParams(paramMap: StringMap<string, any>): List<string> { | ||
var params = []; | ||
if (isPresent(paramMap)) { | ||
StringMapWrapper.forEach(paramMap, (value, key) => { | ||
if (value == true) { | ||
params.push(key); | ||
} else { | ||
params.push(key + '=' + value); | ||
} | ||
}); | ||
} | ||
return params; | ||
} |
Oops, something went wrong.