gecko-dev/testing/web-platform/tests/scroll-to-text-fragment/scroll-to-text-fragment-scroll-to-center-target.html
Jan-Niklas Jaeschke 5ce2766d95 Bug 1906134 - Text Fragments: Ensure to scroll the first text directive on a page into the center of the view. r=emilio
Before this patch, it could happen that `PresShell::GoToAnchor()` was called while loading a document.
This method did not know about text fragments before,
and would scroll the anchor (ie., the first text directive) to the top of the view.
This was noticeable for pages that took longer to load, where the text directive was scrolled to the center first, and then to the top.

This patch saves the desired vertical scroll position as a member in `PresShell` and makes `GoToAnchor()` scroll to the center as well.

Additionally, this patch updates the test file target document to make it more mobile-friendly.

Differential Revision: https://phabricator.services.mozilla.com/D216039
2024-07-16 09:16:48 +00:00

26 lines
721 B
HTML

<!DOCTYPE html>
<title>
Ensuring a text directive is scrolled to the center of the view port instead of the top.
</title>
<script src="stash.js"></script>
<script>
function checkScroll() {
const results = {hasScrolled: window.scrollY != 0};
let key = (new URL(document.location)).searchParams.get("key");
stashResultsThenClose(key, results);
};
window.onload = () => {
window.requestAnimationFrame(function() {
window.requestAnimationFrame(checkScroll);
})
}
</script>
<body>
<script>
document.addEventListener("DOMContentLoaded", () => {
// trigger a layout flush
_ = document.body.getBoundingClientRect();
});
</script>
<div style="margin-top: 20vh; margin-bottom: 100vh">Scroll to me</div>
</body>