Skip to content

Commit 97b7e72

Browse files
better 'selector' type matching in scrollBehavior
previously a scrollBehavior selector was only checked for ^#\d when determining whether to use getElementById or querySelector for matching; this would not work as intended for chained queries (e.g. `vuejs#9, .main`) or those with CSS special characters (e.g. `#one/two`).
1 parent c0d3376 commit 97b7e72

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Diff for: src/util/scroll.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,16 @@ function isNumber (v: any): boolean {
121121
return typeof v === 'number'
122122
}
123123

124-
const hashStartsWithNumberRE = /^#\d/
124+
const selectorLooksLikeIdRE = /^#[^, ]+$/
125125

126126
function scrollToPosition (shouldScroll, position) {
127127
const isObject = typeof shouldScroll === 'object'
128128
if (isObject && typeof shouldScroll.selector === 'string') {
129+
// getElementById is used only where a selector looks like a single [id] ... this allows for use of CSS
130+
// 'special characters' that would otherwise fail in querySelector without escaping, e.g. "#one/two"
129131
// getElementById would still fail if the selector contains a more complicated query like #main[data-attr]
130132
// but at the same time, it doesn't make much sense to select an element with an id and an extra selector
131-
const el = hashStartsWithNumberRE.test(shouldScroll.selector) // $flow-disable-line
133+
const el = selectorLooksLikeIdRE.test(shouldScroll.selector) // $flow-disable-line
132134
? document.getElementById(shouldScroll.selector.slice(1)) // $flow-disable-line
133135
: document.querySelector(shouldScroll.selector)
134136

0 commit comments

Comments
 (0)