Skip to content

Commit 3f91065

Browse files
committed
make query stringification more comformant to RFC3986 (close #1027)
1 parent 9a672d6 commit 3f91065

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/util/query.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
import { warn } from './warn'
44

5-
const encode = encodeURIComponent
5+
const encodeReserveRE = /[!'()*]/g
6+
const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16)
7+
const commaRE = /%2C/g
8+
9+
// fixed encodeURIComponent which is more comformant to RFC3986:
10+
// - escapes [!'()*]
11+
// - preserve commas
12+
const encode = str => encodeURIComponent(str)
13+
.replace(encodeReserveRE, encodeReserveReplacer)
14+
.replace(commaRE, ',')
15+
616
const decode = decodeURIComponent
717

818
export function resolveQuery (

test/unit/specs/navigation-callback.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Router from '../../../src/index'
22

3-
fdescribe('Programmatic navigation callbacks', () => {
3+
describe('Programmatic navigation callbacks', () => {
44
let calls = []
55
let router, spy1, spy2
66

test/unit/specs/query.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,17 @@ describe('Query utils', () => {
1919
arr: [1, 2]
2020
})).toBe('?foo=bar&baz=qux&arr=1&arr=2')
2121
})
22+
23+
it('should escape reserved chars', () => {
24+
expect(stringifyQuery({
25+
a: '*()!'
26+
})).toBe('?a=%2a%28%29%21')
27+
})
28+
29+
it('should preserve commas', () => {
30+
expect(stringifyQuery({
31+
list: '1,2,3'
32+
})).toBe('?list=1,2,3')
33+
})
2234
})
2335
})

0 commit comments

Comments
 (0)