Diff options
-rw-r--r--assets/cgit.js5
-rwxr-xr-xtests/t0005-markup.sh8
2 files changed, 11 insertions, 2 deletions
diff --git a/assets/cgit.js b/assets/cgit.js
index d09423f..dfe4e3d 100644
--- a/assets/cgit.js
+++ b/assets/cgit.js
@@ -153,9 +153,10 @@ function place_bar() {
table.appendChild(bar);
// A browser only scrolls to a fragment that names a real id, which a range
- // never does, so bring the start of one into view here.
+ // never does, so bring the start of one into view here. The scroll margin
+ // on the anchors centres it the way it centres a single line.
if (match[2])
- first.scrollIntoView({ block: "center" });
+ first.scrollIntoView();
}
document.addEventListener("DOMContentLoaded", place_bar, false);
diff --git a/tests/t0005-markup.sh b/tests/t0005-markup.sh
index 1a8c5f2..f93392d 100755
--- a/tests/t0005-markup.sh
+++ b/tests/t0005-markup.sh
@@ -168,6 +168,14 @@ test_expect_success 'the script walks the ages from a list that holds still' '
! grep "getElementsByClassName" "$TEST_OUTPUT_DIRECTORY/../assets/cgit.js"
'
+# The line anchors carry a scroll margin of half the window, which centres a
+# line scrolled to the top of the view. Asking scrollIntoView to centre it as
+# well centres the margin too and drops a range three quarters of the way down.
+test_expect_success 'a line range scrolls where a single line does' '
+ grep "^ scroll-margin-top: calc(50vh - 1em);$" "$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" &&
+ grep "first.scrollIntoView();" "$TEST_OUTPUT_DIRECTORY/../assets/cgit.js"
+'
+
# The rest of the hooks need a tag, a symlink, a submodule, a trailer, a lone
# carriage return, clone urls, an empty repository and a second page of
# repositories, so they get a commit and a config of their own after the checks