Skip to content

Commit a69aed0

Browse files
silverwindlunny
andcommitted
Ignore line anchor links with leading zeroes (go-gitea#21728)
Fixes: go-gitea#21722 Co-authored-by: Lunny Xiao <[email protected]>
1 parent 169eeee commit a69aed0

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

Diff for: web_src/js/features/repo-code.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {invertFileFolding} from './file-fold.js';
44
import {createTippy} from '../modules/tippy.js';
55
import {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+
710
function 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
}

Diff for: 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 {singleAnchorRegex, rangeAnchorRegex} from './repo-code.js';
3+
4+
test('singleAnchorRegex', () => {
5+
expect(singleAnchorRegex.test('#L0')).toEqual(false);
6+
expect(singleAnchorRegex.test('#L1')).toEqual(true);
7+
expect(singleAnchorRegex.test('#L01')).toEqual(false);
8+
expect(singleAnchorRegex.test('#n0')).toEqual(false);
9+
expect(singleAnchorRegex.test('#n1')).toEqual(true);
10+
expect(singleAnchorRegex.test('#n01')).toEqual(false);
11+
});
12+
13+
test('rangeAnchorRegex', () => {
14+
expect(rangeAnchorRegex.test('#L0-L10')).toEqual(false);
15+
expect(rangeAnchorRegex.test('#L1-L10')).toEqual(true);
16+
expect(rangeAnchorRegex.test('#L01-L10')).toEqual(false);
17+
expect(rangeAnchorRegex.test('#L1-L01')).toEqual(false);
18+
});

0 commit comments

Comments
 (0)