8000 fix query double encoding (fix #378) · vuejs/vue-router@af3fe87 · GitHub
[go: up one dir, main page]

Skip to content

Commit af3fe87

Browse files
committed
fix query double encoding (fix #378)
1 parent db0f54f commit af3fe87

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

src/index.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class Router {
304304
*/
305305

306306
stringifyPath (path) {
307-
let fullPath = ''
307+
let generatedPath = ''
308308
if (path && typeof path === 'object') {
309309
if (path.name) {
310310
const extend = Vue.util.extend
@@ -315,26 +315,23 @@ class Router {
315315
const params = currentParams
316316
? extend(extend({}, currentParams), targetParams)
317317
: targetParams
318-
if (path.query) {
319-
params.queryParams = path.query
320-
}
321-
fullPath = encodeURI(this._recognizer.generate(path.name, params))
318+
generatedPath = encodeURI(this._recognizer.generate(path.name, params))
322319
} else if (path.path) {
323-
fullPath = path.path
324-
if (path.query) {
325-
const query = this._recognizer.generateQueryString(path.query)
326-
if (fullPath.indexOf('?') > -1) {
327-
fullPath += '&' + query.slice(1)
328-
} else {
329-
fullPath += query
330-
}
320+
generatedPath = encodeURI(path.path)
321+
}
322+
if (path.query) {
323+
// note: the generated query string is pre-URL-encoded by the recognizer
324+
const query = this._recognizer.generateQueryString(path.query)
325+
if (generatedPath.indexOf('?') > -1) {
326+
generatedPath += '&' + query.slice(1)
327+
} else {
328+
generatedPath += query
331329
}
332-
10000 fullPath = encodeURI(fullPath)
333330
}
334331
} else {
335-
fullPath = encodeURI(path ? path + '' : '')
332+
generatedPath = encodeURI(path ? path + '' : '')
336333
}
337-
return fullPath
334+
return generatedPath
338335
}
339336

340337
// Internal methods ======================================

test/unit/specs/core.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,9 +1152,10 @@ describe('Stringify Path', function () {
11521152

11531153
it('object path', function () {
11541154
expect(router.stringifyPath({ path: '/hi' })).toBe('/hi')
1155-
expect(router.stringifyPath({ path: '/hi', query: { a: 1 } })).toBe('/hi?a=1')
1156-
expect(router.stringifyPath({ path: '/hi', query: { a: 1, b: 2 } })).toBe('/hi?a=1&b=2')
1157-
expect(router.stringifyPath({ path: '/hi?c=3', query: { a: 1, b: 2 } })).toBe('/hi?c=3&a=1&b=2')
1155+
expect(router.stringifyPath({ path: '/hi', query: { a: 1 }})).toBe('/hi?a=1')
1156+
expect(router.stringifyPath({ path: '/hi', query: { a: 1, b: 2 }})).toBe('/hi?a=1&b=2')
1157+
expect(router.stringifyPath({ path: '/hi?c=3', query: { a: 1, b: 2 }})).toBe('/hi?c=3&a=1&b=2')
1158+
expect(router.stringifyPath({ path: '/hi', query: { a: '/c' }})).toBe('/hi?a=%2Fc')
11581159
})
11591160

11601161
it('named route', function () {
@@ -1165,8 +1166,10 @@ describe('Stringify Path', function () {
11651166
}
11661167
})
11671168
expect(router.stringifyPath({ name: 'a' })).toBe('/test/:id')
1168-
expect(router.stringifyPath({ name: 'a', params: { id: 0 } })).toBe('/test/0')
1169-
expect(router.stringifyPath({ name: 'a', params: { id: 'hi' } })).toBe('/test/hi')
1169+
expect(router.stringifyPath({ name: 'a', params: { id: 0 }})).toBe('/test/0')
1170+
expect(router.stringifyPath({ name: 'a', params: { id: 'hi' }})).toBe('/test/hi')
1171+
expect(router.stringifyPath({ name: 'a', params: { id: '你好' }})).toBe('/test/' + encodeURIComponent('你好'))
1172+
expect(router.stringifyPath({ name: 'a', params: { id: 'hi' }, query: { b: '/c' }})< 4830 span class=pl-kos>).toBe('/test/hi?b=%2Fc')
11701173
})
11711174

11721175
it('named route not found should throw error', function () {

0 commit comments

Comments
 (0)
0