Skip to content

Commit 340697a

Browse files
committed
properly encode URI when stringifying navigation path (fix #347)
1 parent 1a5b163 commit 340697a

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ class Router {
568568
*/
569569

570570
_stringifyPath (path) {
571+
let fullPath = ''
571572
if (path && typeof path === 'object') {
572573
if (path.name) {
573574
const extend = Vue.util.extend
@@ -581,9 +582,9 @@ class Router {
581582
if (path.query) {
582583
params.queryParams = path.query
583584
}
584-
return this._recognizer.generate(path.name, params)
585+
fullPath = this._recognizer.generate(path.name, params)
585586
} else if (path.path) {
586-
let fullPath = path.path
587+
fullPath = path.path
587588
if (path.query) {
588589
const query = this._recognizer.generateQueryString(path.query)
589590
if (fullPath.indexOf('?') > -1) {
@@ -592,13 +593,11 @@ class Router {
592593
fullPath += query
593594
}
594595
}
595-
return fullPath
596-
} else {
597-
return ''
598596
}
599597
} else {
600-
return path ? path + '' : ''
598+
fullPath = path ? path + '' : ''
601599
}
600+
return encodeURI(fullPath)
602601
}
603602
}
604603

test/unit/specs/core.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,4 +1147,16 @@ describe('Stringify Path', function () {
11471147
}).toThrow()
11481148
})
11491149

1150+
it('encodeURI', function () {
1151+
router.map({
1152+
'/test/:id': {
1153+
name: 'a',
1154+
component: {}
1155+
}
1156+
})
1157+
expect(router._stringifyPath('/hi/你好')).toBe(encodeURI('/hi/你好'))
1158+
expect(router._stringifyPath({ path: '/hi/你好' })).toBe(encodeURI('/hi/你好'))
1159+
expect(router._stringifyPath({ name: 'a', params: { id: '你好' }})).toBe(encodeURI('/test/你好'))
1160+
})
1161+
11501162
})

0 commit comments

Comments
 (0)