Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore line anchor links with leading zeroes (#21728) #21776

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions web_src/js/features/repo-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {invertFileFolding} from './file-fold.js';
import {createTippy} from '../modules/tippy.js';
import {copyToClipboard} from './clipboard.js';

export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;

function changeHash(hash) {
if (window.history.pushState) {
window.history.pushState(null, null, hash);
Expand Down Expand Up @@ -135,7 +138,7 @@ export function initRepoCodeView() {
});

$(window).on('hashchange', () => {
let m = window.location.hash.match(/^#(L\d+)-(L\d+)$/);
let m = window.location.hash.match(rangeAnchorRegex);
let $list;
if ($('div.blame').length) {
$list = $('.code-view td.lines-code.blame-code');
Expand All @@ -145,27 +148,31 @@ export function initRepoCodeView() {
let $first;
if (m) {
$first = $list.filter(`[rel=${m[1]}]`);
selectRange($list, $first, $list.filter(`[rel=${m[2]}]`));
if ($first.length) {
selectRange($list, $first, $list.filter(`[rel=${m[2]}]`));

// show code view menu marker (don't show in blame page)
if ($('div.blame').length === 0) {
showLineButton();
}
// show code view menu marker (don't show in blame page)
if ($('div.blame').length === 0) {
showLineButton();
}

$('html, body').scrollTop($first.offset().top - 200);
return;
$('html, body').scrollTop($first.offset().top - 200);
return;
}
}
m = window.location.hash.match(/^#(L|n)(\d+)$/);
m = window.location.hash.match(singleAnchorRegex);
if (m) {
$first = $list.filter(`[rel=L${m[2]}]`);
selectRange($list, $first);
if ($first.length) {
selectRange($list, $first);

// show code view menu marker (don't show in blame page)
if ($('div.blame').length === 0) {
showLineButton();
}
// show code view menu marker (don't show in blame page)
if ($('div.blame').length === 0) {
showLineButton();
}

$('html, body').scrollTop($first.offset().top - 200);
$('html, body').scrollTop($first.offset().top - 200);
}
}
}).trigger('hashchange');
}
Expand Down
18 changes: 18 additions & 0 deletions web_src/js/features/repo-code.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {test, expect} from 'vitest';
import {singleAnchorRegex, rangeAnchorRegex} from './repo-code.js';

test('singleAnchorRegex', () => {
expect(singleAnchorRegex.test('#L0')).toEqual(false);
expect(singleAnchorRegex.test('#L1')).toEqual(true);
expect(singleAnchorRegex.test('#L01')).toEqual(false);
expect(singleAnchorRegex.test('#n0')).toEqual(false);
expect(singleAnchorRegex.test('#n1')).toEqual(true);
expect(singleAnchorRegex.test('#n01')).toEqual(false);
});

test('rangeAnchorRegex', () => {
expect(rangeAnchorRegex.test('#L0-L10')).toEqual(false);
expect(rangeAnchorRegex.test('#L1-L10')).toEqual(true);
expect(rangeAnchorRegex.test('#L01-L10')).toEqual(false);
expect(rangeAnchorRegex.test('#L1-L01')).toEqual(false);
});