Skip to content

fix(router-link): active class with encoded params #3125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/route-matching/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const router = new VueRouter({
// asterisk can match anything
{ path: '/asterisk/*' },
// make part of the path optional by wrapping with parens and add "?"
{ path: '/optional-group/(foo/)?bar' }
{ path: '/optional-group/(foo/)?bar' },
// processing routes with special characters
{ path: '/special/:word', name: 'special' }
]
})

Expand All @@ -41,6 +43,8 @@ new Vue({
<li><router-link to="/asterisk/foo/bar">/asterisk/foo/bar</router-link></li>
<li><router-link to="/optional-group/bar">/optional-group/bar</router-link></li>
<li><router-link to="/optional-group/foo/bar">/optional-group/foo/bar</router-link></li>
<li><router-link :to="encodeURI('/special/tést1')">/special/tést1</router-link></li>
<li><router-link :to="{ name: 'special', params: {word: 'tést2'} }">/special/tést2</router-link></li>
</ul>
<p>Route context</p>
<pre>{{ JSON.stringify($route, null, 2) }}</pre>
Expand Down
2 changes: 1 addition & 1 deletion src/history/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ export function getLocation (base: string): string {
if (base && path.toLowerCase().indexOf(base.toLowerCase()) === 0) {
path = path.slice(base.length)
}
return (path || '/') + window.location.search + window.location.hash
return encodeURI((path || '/')) + window.location.search + window.location.hash
}
58 changes: 57 additions & 1 deletion test/e2e/specs/route-matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
browser
.url('http://localhost:8080/route-matching/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 10)
.assert.count('li a', 12)
.assert.evaluate(
function () {
var route = JSON.parse(document.querySelector('pre').textContent)
Expand Down Expand Up @@ -176,6 +176,62 @@ module.exports = {
null,
'/optional-group/foo/bar'
)

.click('li:nth-child(11) a')
.assert.evaluate(
function () {
var route = JSON.parse(document.querySelector('pre').textContent)
return (
route.matched.length === 1 &&
route.matched[0].path === '/special/:word' &&
route.fullPath === encodeURI('/special/tést1') &&
JSON.stringify(route.params) ===
JSON.stringify({
word: 'tést1'
})
)
},
null,
'/optional-group/special/tést1'
)

.click('li:nth-child(12) a')
.assert.evaluate(
function () {
var route = JSON.parse(document.querySelector('pre').textContent)
return (
route.matched.length === 1 &&
route.matched[0].path === '/special/:word' &&
route.fullPath === encodeURI('/special/tést2') &&
JSON.stringify(route.params) ===
JSON.stringify({
word: 'tést2'
})
)
},
null,
'/optional-group/special/tést2'
)

.url('http://localhost:8080/route-matching/special/tést1')
.waitForElementVisible('#app', 1000)
.assert.evaluate(
function () {
return document.querySelector('li:nth-child(11) a').classList.contains('router-link-active')
},
null,
'/optional-group/special/tést1 init active router'
)

.url('http://localhost:8080/route-matching/special/tést2')
.waitForElementVisible('#app', 1000)
.assert.evaluate(
function () {
return document.querySelector('li:nth-child(12) a').classList.contains('router-link-active')
},
null,
'/optional-group/special/tést2 init active router'
)
.end()
}
}