X Tutup
Skip to content

Commit 4902244

Browse files
Alexander Bachmannbtford
authored andcommitted
fix(router): allow forward slashes in query parameters
Closes angular#7824
1 parent 8db97b0 commit 4902244

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

modules/angular2/src/router/url_parser.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ function matchUrlSegment(str: string): string {
8484
var match = RegExpWrapper.firstMatch(SEGMENT_RE, str);
8585
return isPresent(match) ? match[0] : '';
8686
}
87+
var QUERY_PARAM_VALUE_RE = RegExpWrapper.create('^[^\\(\\)\\?;&#]+');
88+
function matchUrlQueryParamValue(str: string): string {
89+
var match = RegExpWrapper.firstMatch(QUERY_PARAM_VALUE_RE, str);
90+
return isPresent(match) ? match[0] : '';
91+
}
8792

8893
export class UrlParser {
8994
private _remaining: string;
@@ -163,10 +168,10 @@ export class UrlParser {
163168
parseQueryParams(): {[key: string]: any} {
164169
var params: {[key: string]: any} = {};
165170
this.capture('?');
166-
this.parseParam(params);
171+
this.parseQueryParam(params);
167172
while (this._remaining.length > 0 && this.peekStartsWith('&')) {
168173
this.capture('&');
169-
this.parseParam(params);
174+
this.parseQueryParam(params);
170175
}
171176
return params;
172177
}
@@ -199,6 +204,25 @@ export class UrlParser {
199204
params[key] = value;
200205
}
201206

207+
parseQueryParam(params: {[key: string]: any}): void {
208+
var key = matchUrlSegment(this._remaining);
209+
if (isBlank(key)) {
210+
return;
211+
}
212+
this.capture(key);
213+
var value: any = true;
214+
if (this.peekStartsWith('=')) {
215+
this.capture('=');
216+
var valueMatch = matchUrlQueryParamValue(this._remaining);
217+
if (isPresent(valueMatch)) {
218+
value = valueMatch;
219+
this.capture(value);
220+
}
221+
}
222+
223+
params[key] = value;
224+
}
225+
202226
parseAuxiliaryRoutes(): Url[] {
203227
var routes: Url[] = [];
204228
this.capture('(');

modules/angular2/test/router/url_parser_spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,12 @@ export function main() {
124124
var url = urlParser.parse('hello/there;sort=asc(modal)?friend=true');
125125
expect(url.toString()).toEqual('hello/there;sort=asc(modal)?friend=true');
126126
});
127+
it('should allow slashes within query parameters', () => {
128+
var url = urlParser.parse(
129+
'hello?code=4/B8o0n_Y7XZTb-pVKBw5daZyGAUbMljyLf7uNgTy6ja8&scope=https://www.googleapis.com/auth/analytics');
130+
expect(url.toString())
131+
.toEqual(
132+
'hello?code=4/B8o0n_Y7XZTb-pVKBw5daZyGAUbMljyLf7uNgTy6ja8&scope=https://www.googleapis.com/auth/analytics');
133+
});
127134
});
128135
}

0 commit comments

Comments
 (0)
X Tutup