Skip to content

Commit 560d11d

Browse files
mestevezdeisterposva
andauthoredSep 29, 2020
fix: remove duplicated decodeURIComponent (#3323)
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
1 parent 1c2e47b commit 560d11d

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed
 

‎examples/basic/app.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Home = { template: '<div>home</div>' }
3535
const Foo = { template: '<div>foo</div>' }
3636
const Bar = { template: '<div>bar</div>' }
3737
const Unicode = { template: '<div>unicode</div>' }
38+
const Query = { template: '<div>query: "{{ $route.params.q }}"</div>' }
3839

3940
// 3. Create the router
4041
const router = new VueRouter({
@@ -44,7 +45,8 @@ const router = new VueRouter({
4445
{ path: '/', component: Home },
4546
{ path: '/foo', component: Foo },
4647
{ path: '/bar', component: Bar },
47-
{ path: '/é', component: Unicode }
48+
{ path: '/é', component: Unicode },
49+
{ path: '/query/:q', component: Query }
4850
]
4951
})
5052

@@ -83,6 +85,7 @@ const vueInstance = new Vue({
8385
</li>
8486
</router-link>
8587
<li><router-link to="/foo" replace>/foo (replace)</router-link></li>
88+
<li><router-link to="/query/A%25">/query/A%</router-link></li>
8689
<li><router-link to="/?delay=200">/ (delay of 500ms)</router-link></li>
8790
<li><router-link to="/foo?delay=200">/foo (delay of 500ms)</router-link></li>
8891
</ul>

‎examples/hash-mode/app.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Home = { template: '<div>home</div>' }
3535
const Foo = { template: '<div>foo</div>' }
3636
const Bar = { template: '<div>bar</div>' }
3737
const Unicode = { template: '<div>unicode: {{ $route.params.unicode }}</div>' }
38+
const Query = { template: '<div>query: "{{ $route.params.q }}"</div>' }
3839

3940
// 3. Create the router
4041
const router = new VueRouter({
@@ -45,7 +46,8 @@ const router = new VueRouter({
4546
{ path: '/foo', component: Foo },
4647
{ path: '/bar', component: Bar },
4748
{ path: '/é', component: Unicode },
48-
{ path: '/é/:unicode', component: Unicode }
49+
{ path: '/é/:unicode', component: Unicode },
50+
{ path: '/query/:q', component: Query }
4951
]
5052
})
5153

@@ -66,6 +68,7 @@ const vueInstance = new Vue({
6668
<li><router-link to="/é/ñ">/é/ñ</router-link></li>
6769
<li><router-link to="/é/ñ?t=%25ñ">/é/ñ?t=%ñ</router-link></li>
6870
<li><router-link to="/é/ñ#é">/é/ñ#é</router-link></li>
71+
<li><router-link to="/query/A%25">/query/A%</router-link></li>
6972
</ul>
7073
<pre id="query-t">{{ $route.query.t }}</pre>
7174
<pre id="hash">{{ $route.hash }}</pre>

‎src/create-matcher.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function matchRoute (
175175
path: string,
176176
params: Object
177177
): boolean {
178-
const m = path.match(regex)
178+
const m = decodeURI(path).match(regex)
179179

180180
if (!m) {
181181
return false
@@ -185,10 +185,9 @@ function matchRoute (
185185

186186
for (let i = 1, len = m.length; i < len; ++i) {
187187
const key = regex.keys[i - 1]
188-
const val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i]
189188
if (key) {
190189
// Fix #1994: using * with props: true generates a param named 0
191-
params[key.name || 'pathMatch'] = val
190+
params[key.name || 'pathMatch'] = m[i]
192191
}
193192
}
194193

‎src/history/hash.js

-12
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,6 @@ export function getHash (): string {
124124
if (index < 0) return ''
125125

126126
href = href.slice(index + 1)
127-
// decode the hash but not the search or hash
128-
// as search(query) is already decoded
129-
// https://github.com/vuejs/vue-router/issues/2708
130-
const searchIndex = href.indexOf('?')
131-
if (searchIndex < 0) {
132-
const hashIndex = href.indexOf('#')
133-
if (hashIndex > -1) {
134-
href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
135-
} else href = decodeURI(href)
136-
} else {
137-
href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
138-
}
139127

140128
return href
141129
}

‎src/history/html5.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class HTML5History extends History {
8686
}
8787

8888
export function getLocation (base: string): string {
89-
let path = decodeURI(window.location.pathname)
89+
let path = window.location.pathname
9090
if (base && path.toLowerCase().indexOf(base.toLowerCase()) === 0) {
9191
path = path.slice(base.length)
9292
}

‎test/e2e/specs/basic.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = {
99
browser
1010
.url('http://localhost:8080/basic/')
1111
.waitForElementVisible('#app', 1000)
12-
.assert.count('li', 11)
13-
.assert.count('li a', 11)
12+
.assert.count('li', 12)
13+
.assert.count('li a', 12)
1414
// assert correct href with base
1515
.assert.attributeContains('li:nth-child(1) a', 'href', '/basic/')
1616
.assert.attributeContains('li:nth-child(2) a', 'href', '/basic/foo')
@@ -20,6 +20,7 @@ module.exports = {
2020
.assert.attributeContains('li:nth-child(6) a', 'href', '/basic/%C3%A9?t=%25%C3%B1')
2121
.assert.attributeContains('li:nth-child(7) a', 'href', '/basic/%C3%A9#%25%C3%B1')
2222
.assert.attributeContains('li:nth-child(8) a', 'href', '/basic/foo')
23+
.assert.attributeContains('li:nth-child(10) a', 'href', '/basic/query/A%')
2324
.assert.containsText('.view', 'home')
2425

2526
.click('li:nth-child(2) a')
@@ -70,6 +71,15 @@ module.exports = {
7071
.assert.cssClassPresent('li:nth-child(8)', 'exact-active')
7172
.assert.attributeEquals('li:nth-child(8) a', 'class', '')
7273

74+
// encoded percentage as path param
75+
// https://github.com/vuejs/vue-router/issues/2725
76+
.url('http://localhost:8080/basic/query/A%25')
77+
.waitForElementVisible('#app', 1000)
78+
.assert.containsText('.view', 'query: "A%"')
79+
.click('li:nth-child(10) a')
80+
.assert.urlEquals('http://localhost:8080/basic/query/A%25')
81+
.assert.containsText('.view', 'query: "A%"')
82+
7383
// Listener cleanup
7484
.assert.containsText('#popstate-count', '1 popstate listeners')
7585
.click('#unmount')
@@ -84,8 +94,8 @@ module.exports = {
8494
.waitForElementVisible('#app', 1000)
8595
.assert.containsText('.view', 'home')
8696
// go to foo with a delay
97+
.click('li:nth-child(12) a')
8798
.click('li:nth-child(11) a')
88-
.click('li:nth-child(10) a')
8999
.waitFor(300)
90100
// we should stay at /basic after the delay
91101
.assert.urlEquals('http://localhost:8080/basic/?delay=200')

‎test/e2e/specs/hash-mode.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ module.exports = {
99
browser
1010
.url('http://localhost:8080/hash-mode/')
1111
.waitForElementVisible('#app', 1000)
12-
.assert.count('li', 8)
13-
.assert.count('li a', 7)
12+
.assert.count('li', 9)
13+
.assert.count('li a', 8)
1414
.assert.attributeContains('li:nth-child(1) a', 'href', '/hash-mode/#/')
1515
.assert.attributeContains('li:nth-child(2) a', 'href', '/hash-mode/#/foo')
1616
.assert.attributeContains('li:nth-child(3) a', 'href', '/hash-mode/#/bar')
1717
.assert.attributeContains('li:nth-child(5) a', 'href', '/hash-mode/#/%C3%A9')
1818
.assert.attributeContains('li:nth-child(6) a', 'href', '/hash-mode/#/%C3%A9/%C3%B1')
1919
.assert.attributeContains('li:nth-child(7) a', 'href', '/hash-mode/#/%C3%A9/%C3%B1?t=%25%C3%B1')
20+
.assert.attributeContains('li:nth-child(9) a', 'href', '/hash-mode/#/query/A%')
2021
.assert.containsText('.view', 'home')
2122

2223
.click('li:nth-child(2) a')
@@ -58,6 +59,15 @@ module.exports = {
5859
.assert.containsText('.view', 'unicode: ñ')
5960
.assert.containsText('#query-t', '%')
6061

62+
// percentage as path param
63+
// https://github.com/vuejs/vue-router/issues/2725
64+
.url('http://localhost:8080/hash-mode/#/query/A%25')
65+
.waitForElementVisible('#app', 1000)
66+
.assert.containsText('.view', 'query: "A%"')
67+
.click('li:nth-child(9) a')
68+
.assert.urlEquals('http://localhost:8080/hash-mode/#/query/A%25')
69+
.assert.containsText('.view', 'query: "A%"')
70+
6171
// Listener cleanup
6272
.assert.containsText('#popstate-count', '1 popstate listeners')
6373
.click('#unmount')

0 commit comments

Comments
 (0)
Please sign in to comment.