Skip to content

Commit f02e03b

Browse files
committed
Ignore line anchor links with leading zeroes
Fixes: go-gitea#21722
1 parent 2ebab42 commit f02e03b

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

web_src/js/features/repo-code.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {createTippy, showTemporaryTooltip} from '../modules/tippy.js';
55
import {copyToClipboard} from './clipboard.js';
66

77
const {i18n} = window.config;
8+
export const singleRe = /^#(L|n)([1-9][0-9]*)$/;
9+
export const rangeRe = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
810

911
function 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(rangeRe);
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(singleRe);
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
}

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

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

0 commit comments

Comments
 (0)