fix(router): change serialize not to require parenthesis in query string to be encoded

This commit is contained in:
vsavkin 2016-06-16 16:28:35 -07:00
parent fea216db12
commit 280540e4a2
3 changed files with 22 additions and 9 deletions

View File

@ -7,6 +7,7 @@
## Bug Fixes ## Bug Fixes
* fix(router): stringify positional parameters when using routerLink * fix(router): stringify positional parameters when using routerLink
* fix(router): change serialize not to require parenthesis in query string to be encoded
## Breaking Changes ## Breaking Changes

View File

@ -115,11 +115,18 @@ function pairs<T>(obj: {[key: string]: T}): Pair<string, T>[] {
const SEGMENT_RE = /^[^\/\(\)\?;=&#]+/; const SEGMENT_RE = /^[^\/\(\)\?;=&#]+/;
function matchPathWithParams(str: string): string { function matchPathWithParams(str: string): string {
SEGMENT_RE.lastIndex = 0; SEGMENT_RE.lastIndex = 0;
var match = SEGMENT_RE.exec(str); const match = SEGMENT_RE.exec(str);
return match ? match[0] : ''; return match ? match[0] : '';
} }
const QUERY_PARAM_VALUE_RE = /^[^\(\)\?;&#]+/; const QUERY_PARAM_RE = /^[^=\?&#]+/;
function matchQueryParams(str: string): string {
QUERY_PARAM_RE.lastIndex = 0;
const match = SEGMENT_RE.exec(str);
return match ? match[0] : '';
}
const QUERY_PARAM_VALUE_RE = /^[^\?&#]+/;
function matchUrlQueryParamValue(str: string): string { function matchUrlQueryParamValue(str: string): string {
QUERY_PARAM_VALUE_RE.lastIndex = 0; QUERY_PARAM_VALUE_RE.lastIndex = 0;
const match = QUERY_PARAM_VALUE_RE.exec(str); const match = QUERY_PARAM_VALUE_RE.exec(str);
@ -188,7 +195,7 @@ class UrlParser {
} }
parseQueryParams(): {[key: string]: any} { parseQueryParams(): {[key: string]: any} {
var params: {[key: string]: any} = {}; const params: {[key: string]: any} = {};
if (this.peekStartsWith('?')) { if (this.peekStartsWith('?')) {
this.capture('?'); this.capture('?');
this.parseQueryParam(params); this.parseQueryParam(params);
@ -209,7 +216,7 @@ class UrlParser {
} }
parseMatrixParams(): {[key: string]: any} { parseMatrixParams(): {[key: string]: any} {
var params: {[key: string]: any} = {}; const params: {[key: string]: any} = {};
while (this.remaining.length > 0 && this.peekStartsWith(';')) { while (this.remaining.length > 0 && this.peekStartsWith(';')) {
this.capture(';'); this.capture(';');
this.parseParam(params); this.parseParam(params);
@ -218,15 +225,15 @@ class UrlParser {
} }
parseParam(params: {[key: string]: any}): void { parseParam(params: {[key: string]: any}): void {
var key = matchPathWithParams(this.remaining); const key = matchPathWithParams(this.remaining);
if (!key) { if (!key) {
return; return;
} }
this.capture(key); this.capture(key);
var value: any = 'true'; let value: any = 'true';
if (this.peekStartsWith('=')) { if (this.peekStartsWith('=')) {
this.capture('='); this.capture('=');
var valueMatch = matchPathWithParams(this.remaining); const valueMatch = matchPathWithParams(this.remaining);
if (valueMatch) { if (valueMatch) {
value = valueMatch; value = valueMatch;
this.capture(value); this.capture(value);
@ -237,12 +244,12 @@ class UrlParser {
} }
parseQueryParam(params: {[key: string]: any}): void { parseQueryParam(params: {[key: string]: any}): void {
var key = matchPathWithParams(this.remaining); const key = matchQueryParams(this.remaining);
if (!key) { if (!key) {
return; return;
} }
this.capture(key); this.capture(key);
var value: any = 'true'; let value: any = 'true';
if (this.peekStartsWith('=')) { if (this.peekStartsWith('=')) {
this.capture('='); this.capture('=');
var valueMatch = matchUrlQueryParamValue(this.remaining); var valueMatch = matchUrlQueryParamValue(this.remaining);

View File

@ -92,6 +92,11 @@ describe('url serializer', () => {
expect(tree.queryParams).toEqual({a: '1', b: '2'}); expect(tree.queryParams).toEqual({a: '1', b: '2'});
}); });
it("should parse query params when with parenthesis", () => {
const tree = url.parse("/one?a=(11)&b=(22)");
expect(tree.queryParams).toEqual({a: '(11)', b: '(22)'});
});
it("should parse key only query params", () => { it("should parse key only query params", () => {
const tree = url.parse("/one?a"); const tree = url.parse("/one?a");
expect(tree.queryParams).toEqual({a: 'true'}); expect(tree.queryParams).toEqual({a: 'true'});