@@ -45,20 +45,27 @@ export function handleScroll (
45
45
// wait until re-render finishes before scrolling
46
46
router . app . $nextTick ( ( ) => {
47
47
const position = getScrollPosition ( )
48
- const shouldScroll = behavior . call ( router , to , from , isPop ? position : null )
48
+ const shouldScroll = behavior . call (
49
+ router ,
50
+ to ,
51
+ from ,
52
+ isPop ? position : null
53
+ )
49
54
50
55
if ( ! shouldScroll ) {
51
56
return
52
57
}
53
58
54
59
if ( typeof shouldScroll . then === 'function' ) {
55
- shouldScroll . then ( shouldScroll => {
56
- scrollToPosition ( ( shouldScroll : any ) , position )
57
- } ) . catch ( err => {
58
- if ( process . env . NODE_ENV !== 'production' ) {
59
- assert ( false , err . toString ( ) )
60
- }
61
- } )
60
+ shouldScroll
61
+ . then ( shouldScroll => {
62
+ scrollToPosition ( ( shouldScroll : any ) , position )
63
+ } )
64
+ . catch ( err => {
65
+ if ( process . env . NODE_ENV !== 'production' ) {
66
+ assert ( false , err . toString ( ) )
67
+ }
68
+ } )
62
69
} else {
63
70
scrollToPosition ( shouldScroll , position )
64
71
}
@@ -114,12 +121,22 @@ function isNumber (v: any): boolean {
114
121
return typeof v === 'number'
115
122
}
116
123
124
+ const hashStartsWithNumberRE = / ^ # \d /
125
+
117
126
function scrollToPosition ( shouldScroll , position ) {
118
127
const isObject = typeof shouldScroll === 'object'
119
128
if ( isObject && typeof shouldScroll . selector === 'string' ) {
120
- const el = document . querySelector ( shouldScroll . selector )
129
+ // getElementById would still fail if the selector contains a more complicated query like #main[data-attr]
130
+ // 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
132
+ ? document . getElementById ( shouldScroll . selector . slice ( 1 ) ) // $flow-disable-line
133
+ : document . querySelector ( shouldScroll . selector )
134
+
121
135
if ( el ) {
122
- let offset = shouldScroll . offset && typeof shouldScroll . offset === 'object' ? shouldScroll . offset : { }
136
+ let offset =
137
+ shouldScroll . offset && typeof shouldScroll . offset === 'object'
138
+ ? shouldScroll . offset
139
+ : { }
123
140
offset = normalizeOffset ( offset )
124
141
position = getElementPosition ( el , offset )
125
142
} else if ( isValidPosition ( shouldScroll ) ) {
0 commit comments