@@ -5,6 +5,8 @@ import {createTippy, showTemporaryTooltip} from '../modules/tippy.js';
55import { copyToClipboard } from './clipboard.js' ;
66
77const { i18n} = window . config ;
8+ export const singleAnchorRegex = / ^ # ( L | n ) ( [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
9+ export const rangeAnchorRegex = / ^ # ( L [ 1 - 9 ] [ 0 - 9 ] * ) - ( L [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
810
911function changeHash ( hash ) {
1012 if ( window . history . pushState ) {
@@ -149,7 +151,7 @@ export function initRepoCodeView() {
149151 } ) ;
150152
151153 $ ( window ) . on ( 'hashchange' , ( ) => {
152- let m = window . location . hash . match ( / ^ # ( L \d + ) - ( L \d + ) $ / ) ;
154+ let m = window . location . hash . match ( rangeAnchorRegex ) ;
153155 let $list ;
154156 if ( $ ( 'div.blame' ) . length ) {
155157 $list = $ ( '.code-view td.lines-code.blame-code' ) ;
@@ -159,27 +161,31 @@ export function initRepoCodeView() {
159161 let $first ;
160162 if ( m ) {
161163 $first = $list . filter ( `[rel=${ m [ 1 ] } ]` ) ;
162- selectRange ( $list , $first , $list . filter ( `[rel=${ m [ 2 ] } ]` ) ) ;
164+ if ( $first . length ) {
165+ selectRange ( $list , $first , $list . filter ( `[rel=${ m [ 2 ] } ]` ) ) ;
163166
164- // show code view menu marker (don't show in blame page)
165- if ( $ ( 'div.blame' ) . length === 0 ) {
166- showLineButton ( ) ;
167- }
167+ // show code view menu marker (don't show in blame page)
168+ if ( $ ( 'div.blame' ) . length === 0 ) {
169+ showLineButton ( ) ;
170+ }
168171
169- $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
170- return ;
172+ $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
173+ return ;
174+ }
171175 }
172- m = window . location . hash . match ( / ^ # ( L | n ) ( \d + ) $ / ) ;
176+ m = window . location . hash . match ( singleAnchorRegex ) ;
173177 if ( m ) {
174178 $first = $list . filter ( `[rel=L${ m [ 2 ] } ]` ) ;
175- selectRange ( $list , $first ) ;
179+ if ( $first . length ) {
180+ selectRange ( $list , $first ) ;
176181
177- // show code view menu marker (don't show in blame page)
178- if ( $ ( 'div.blame' ) . length === 0 ) {
179- showLineButton ( ) ;
180- }
182+ // show code view menu marker (don't show in blame page)
183+ if ( $ ( 'div.blame' ) . length === 0 ) {
184+ showLineButton ( ) ;
185+ }
181186
182- $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
187+ $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
188+ }
183189 }
184190 } ) . trigger ( 'hashchange' ) ;
185191 }
0 commit comments