@@ -4,6 +4,9 @@ import {invertFileFolding} from './file-fold.js';
4
4
import { createTippy } from '../modules/tippy.js' ;
5
5
import { copyToClipboard } from './clipboard.js' ;
6
6
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
+
7
10
function changeHash ( hash ) {
8
11
if ( window . history . pushState ) {
9
12
window . history . pushState ( null , null , hash ) ;
@@ -135,7 +138,7 @@ export function initRepoCodeView() {
135
138
} ) ;
136
139
137
140
$ ( window ) . on ( 'hashchange' , ( ) => {
138
- let m = window . location . hash . match ( / ^ # ( L \d + ) - ( L \d + ) $ / ) ;
141
+ let m = window . location . hash . match ( rangeAnchorRegex ) ;
139
142
let $list ;
140
143
if ( $ ( 'div.blame' ) . length ) {
141
144
$list = $ ( '.code-view td.lines-code.blame-code' ) ;
@@ -145,27 +148,31 @@ export function initRepoCodeView() {
145
148
let $first ;
146
149
if ( m ) {
147
150
$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 ] } ]` ) ) ;
149
153
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
+ }
154
158
155
- $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
156
- return ;
159
+ $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
160
+ return ;
161
+ }
157
162
}
158
- m = window . location . hash . match ( / ^ # ( L | n ) ( \d + ) $ / ) ;
163
+ m = window . location . hash . match ( singleAnchorRegex ) ;
159
164
if ( m ) {
160
165
$first = $list . filter ( `[rel=L${ m [ 2 ] } ]` ) ;
161
- selectRange ( $list , $first ) ;
166
+ if ( $first . length ) {
167
+ selectRange ( $list , $first ) ;
162
168
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
+ }
167
173
168
- $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
174
+ $ ( 'html, body' ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
175
+ }
169
176
}
170
177
} ) . trigger ( 'hashchange' ) ;
171
178
}
0 commit comments