Skip to content

Commit e609ef9

Browse files
authored
Ignore line anchor links with leading zeroes (#21728) (#21777)
1 parent f321cdc commit e609ef9

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

web_src/js/features/repo-code.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import $ from 'jquery';
22
import {svg} from '../svg.js';
33
import {invertFileFolding} from './file-fold.js';
44

5+
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
6+
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
7+
58
function changeHash(hash) {
69
if (window.history.pushState) {
710
window.history.pushState(null, null, hash);
@@ -114,7 +117,7 @@ export function initRepoCodeView() {
114117
});
115118

116119
$(window).on('hashchange', () => {
117-
let m = window.location.hash.match(/^#(L\d+)-(L\d+)$/);
120+
let m = window.location.hash.match(rangeAnchorRegex);
118121
let $list;
119122
if ($('div.blame').length) {
120123
$list = $('.code-view td.lines-code.blame-code');
@@ -124,27 +127,31 @@ export function initRepoCodeView() {
124127
let $first;
125128
if (m) {
126129
$first = $list.filter(`[rel=${m[1]}]`);
127-
selectRange($list, $first, $list.filter(`[rel=${m[2]}]`));
130+
if ($first.length) {
131+
selectRange($list, $first, $list.filter(`[rel=${m[2]}]`));
128132

129-
// show code view menu marker (don't show in blame page)
130-
if ($('div.blame').length === 0) {
131-
showLineButton();
132-
}
133+
// show code view menu marker (don't show in blame page)
134+
if ($('div.blame').length === 0) {
135+
showLineButton();
136+
}
133137

134-
$('html, body').scrollTop($first.offset().top - 200);
135-
return;
138+
$('html, body').scrollTop($first.offset().top - 200);
139+
return;
140+
}
136141
}
137-
m = window.location.hash.match(/^#(L|n)(\d+)$/);
142+
m = window.location.hash.match(singleAnchorRegex);
138143
if (m) {
139144
$first = $list.filter(`[rel=L${m[2]}]`);
140-
selectRange($list, $first);
145+
if ($first.length) {
146+
selectRange($list, $first);
141147

142-
// show code view menu marker (don't show in blame page)
143-
if ($('div.blame').length === 0) {
144-
showLineButton();
145-
}
148+
// show code view menu marker (don't show in blame page)
149+
if ($('div.blame').length === 0) {
150+
showLineButton();
151+
}
146152

147-
$('html, body').scrollTop($first.offset().top - 200);
153+
$('html, body').scrollTop($first.offset().top - 200);
154+
}
148155
}
149156
}).trigger('hashchange');
150157
}

web_src/js/features/repo-code.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {singleAnchorRegex, rangeAnchorRegex} from './repo-code.js';
2+
3+
test('singleAnchorRegex', () => {
4+
expect(singleAnchorRegex.test('#L0')).toEqual(false);
5+
expect(singleAnchorRegex.test('#L1')).toEqual(true);
6+
expect(singleAnchorRegex.test('#L01')).toEqual(false);
7+
expect(singleAnchorRegex.test('#n0')).toEqual(false);
8+
expect(singleAnchorRegex.test('#n1')).toEqual(true);
9+
expect(singleAnchorRegex.test('#n01')).toEqual(false);
10+
});
11+
12+
test('rangeAnchorRegex', () => {
13+
expect(rangeAnchorRegex.test('#L0-L10')).toEqual(false);
14+
expect(rangeAnchorRegex.test('#L1-L10')).toEqual(true);
15+
expect(rangeAnchorRegex.test('#L01-L10')).toEqual(false);
16+
expect(rangeAnchorRegex.test('#L1-L01')).toEqual(false);
17+
});

0 commit comments

Comments
 (0)