Skip to content

Commit

Permalink
Ignore line anchor links with leading zeroes
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Nov 8, 2022
1 parent 2ebab42 commit f02e03b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
36 changes: 21 additions & 15 deletions web_src/js/features/repo-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {createTippy, showTemporaryTooltip} from '../modules/tippy.js';
import {copyToClipboard} from './clipboard.js';

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

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

$(window).on('hashchange', () => {
let m = window.location.hash.match(/^#(L\d+)-(L\d+)$/);
let m = window.location.hash.match(rangeRe);
let $list;
if ($('div.blame').length) {
$list = $('.code-view td.lines-code.blame-code');
Expand All @@ -159,27 +161,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(singleRe);
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 {singleRe, rangeRe} from './repo-code.js';

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

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

0 comments on commit f02e03b

Please sign in to comment.