Skip to content
Merged
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
81 changes: 49 additions & 32 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,49 @@ export class HashHistory extends History {
setupScroll()
}

window.addEventListener(supportsPushState ? 'popstate' : 'hashchange', () => {
const current = this.current
if (!ensureSlash()) {
return
}
this.transitionTo(getHash(), route => {
if (supportsScroll) {
handleScroll(this.router, route, current, true)
}
if (!supportsPushState) {
replaceHash(route.fullPath)
window.addEventListener(
supportsPushState ? 'popstate' : 'hashchange',
() => {
const current = this.current
if (!ensureSlash()) {
return
}
})
})
this.transitionTo(getHash(), route => {
if (supportsScroll) {
handleScroll(this.router, route, current, true)
}
if (!supportsPushState) {
replaceHash(route.fullPath)
}
})
}
)
}

push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(location, route => {
pushHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
this.transitionTo(
location,
route => {
pushHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
},
onAbort
)
}

replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(location, route => {
replaceHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
this.transitionTo(
location,
route => {
replaceHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
},
onAbort
)
}

go (n: number) {
Expand All @@ -81,9 +92,7 @@ export class HashHistory extends History {
function checkFallback (base) {
const location = getLocation(base)
if (!/^\/#/.test(location)) {
window.location.replace(
cleanPath(base + '/#' + location)
)
window.location.replace(cleanPath(base + '/#' + location))
return true
}
}
Expand Down Expand Up @@ -112,20 +121,28 @@ export function getHash (): string {
const searchIndex = href.indexOf('?')
if (searchIndex < 0) {
const hashIndex = href.indexOf('#')
if (hashIndex > -1) href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
else href = decodeURI(href)
if (hashIndex > -1) {
href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex)
} else href = decodeURI(href)
} else {
if (searchIndex > -1) href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
if (searchIndex > -1) {
href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex)
}
}

return href
}

function getUrl (path) {
const href = window.location.href
const i = href.indexOf('#')
const base = i >= 0 ? href.slice(0, i) : href
return `${base}#${path}`
const hashPos = href.indexOf('#')
let base = hashPos > -1 ? href.slice(0, hashPos) : href

const searchPos = base.indexOf('?')
const query = searchPos > -1 ? base.slice(searchPos) : ''
base = query ? base.slice(0, searchPos) : base

return `${base}#${path + query}`
}

function pushHash (path) {
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/specs/hash-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ module.exports = {
.waitForElementVisible('#app', 1000)
.assert.containsText('.view', 'unicode: ñ')
.assert.containsText('#query-t', '%')

// correctly placing query
// https://github.com/vuejs/vue-router/issues/2125
.url('http://localhost:8080/hash-mode/?key=foo')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/?key=foo')
.url('http://localhost:8080/hash-mode?key=foo')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/?key=foo')
.url('http://localhost:8080/hash-mode?key=foo#other')
.waitForElementVisible('#app', 1000)
.assert.urlEquals('http://localhost:8080/hash-mode/#/other?key=foo')
.end()
}
}