@@ -45,20 +45,27 @@ export function handleScroll (
4545 // wait until re-render finishes before scrolling
4646 router . app . $nextTick ( ( ) => {
4747 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+ )
4954
5055 if ( ! shouldScroll ) {
5156 return
5257 }
5358
5459 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+ } )
6269 } else {
6370 scrollToPosition ( shouldScroll , position )
6471 }
@@ -114,12 +121,22 @@ function isNumber (v: any): boolean {
114121 return typeof v === 'number'
115122}
116123
124+ const hashStartsWithNumberRE = / ^ # \d /
125+
117126function scrollToPosition ( shouldScroll , position ) {
118127 const isObject = typeof shouldScroll === 'object'
119128 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+
121135 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+ : { }
123140 offset = normalizeOffset ( offset )
124141 position = getElementPosition ( el , offset )
125142 } else if ( isValidPosition ( shouldScroll ) ) {
0 commit comments