fix DOMException when using an url-hash that is not valid as css selector

This commit is contained in:
devneko 2018-02-26 16:49:33 +09:00 committed by Pooya Parsa
parent 03f89421c2
commit 5f55bf76bd

View File

@ -53,9 +53,22 @@ const scrollBehavior = function (to, from, savedPosition) {
window.$nuxt.$once('triggerScroll', () => {
// coords will be used if no selector is provided,
// or if the selector didn't match any element.
if (to.hash && document.querySelector(to.hash)) {
// scroll to anchor by returning the selector
position = { selector: to.hash }
if (to.hash) {
let hash = to.hash
// CSS.escape() is not supported with IE and Edge.
// But we can use the following polyfill to solve this problem.
// https://github.com/mathiasbynens/CSS.escape
if (typeof window.CSS !== 'undefined' && typeof window.CSS.escape !== 'undefined') {
hash = '#' + window.CSS.escape(hash.substr(1))
}
try {
if (document.querySelector(hash)) {
// scroll to anchor by returning the selector
position = { selector: hash }
}
} catch (e) {
console.warn('failed to save the scroll position. possibly this problem can be solved using a polyfill for CSS.escape().')
}
}
resolve(position)
})