@@ -4,6 +4,9 @@ import {invertFileFolding} from './file-fold.js';
44import { createTippy } from '../modules/tippy.js' ;
55import { copyToClipboard } from './clipboard.js' ;
66
7+ export const singleAnchorRegex = / ^ # ( L | n ) ( [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
8+ export const rangeAnchorRegex = / ^ # ( L [ 1 - 9 ] [ 0 - 9 ] * ) - ( L [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
9+
710function changeHash ( hash ) {
811 if ( window . history . pushState ) {
912 window . history . pushState ( null , null , hash ) ;
@@ -135,7 +138,7 @@ export function initRepoCodeView() {
135138 } ) ;
136139
137140 $ ( window ) . on ( 'hashchange' , ( ) => {
138- let m = window . location . hash . match ( / ^ # ( L \d + ) - ( L \d + ) $ / ) ;
141+ let m = window . location . hash . match ( rangeAnchorRegex ) ;
139142 let $list ;
140143 if ( $ ( 'div.blame' ) . length ) {
141144 $list = $ ( '.code-view td.lines-code.blame-code' ) ;
@@ -145,27 +148,31 @@ export function initRepoCodeView() {
145148 let $first ;
146149 if ( m ) {
147150 $first = $list . filter ( `[rel=${ m [ 1 ] } ]` ) ;
148- selectRange ( $list , $first , $list . filter ( `[rel=${ m [ 2 ] } ]` ) ) ;
151+ if ( $first . length ) {
152+ selectRange ( $list , $first , $list . filter ( `[rel=${ m [ 2 ] } ]` ) ) ;
149153
150- // show code view menu marker (don't show in blame page)
151- if ( $ ( 'div.blame' ) . length === 0 ) {
152- showLineButton ( ) ;
153- }
154+ // show code view menu marker (don't show in blame page)
155+ if ( $ ( 'div.blame' ) . length === 0 ) {
156+ showLineButton ( ) ;
157+ }
154158
155- $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
156- return ;
159+ $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
160+ return ;
161+ }
157162 }
158- m = window . location . hash . match ( / ^ # ( L | n ) ( \d + ) $ / ) ;
163+ m = window . location . hash . match ( singleAnchorRegex ) ;
159164 if ( m ) {
160165 $first = $list . filter ( `[rel=L${ m [ 2 ] } ]` ) ;
161- selectRange ( $list , $first ) ;
166+ if ( $first . length ) {
167+ selectRange ( $list , $first ) ;
162168
163- // show code view menu marker (don't show in blame page)
164- if ( $ ( 'div.blame' ) . length === 0 ) {
165- showLineButton ( ) ;
166- }
169+ // show code view menu marker (don't show in blame page)
170+ if ( $ ( 'div.blame' ) . length === 0 ) {
171+ showLineButton ( ) ;
172+ }
167173
168- $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
174+ $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
175+ }
169176 }
170177 } ) . trigger ( 'hashchange' ) ;
171178 }
0 commit comments