gecko-dev/testing/web-platform/tests/scroll-to-text-fragment/scroll-to-text-fragment-after-DOMContentLoaded-target.html
Jan-Niklas Jaeschke 95ead9dbe5 Bug 1898321 - Text Fragments: Correctly find text fragments if there are DOM changes during page load. r=farre,dom-core
Before this patch, the first call to `Document::ScrollToRef()` would attempt to find all text directives in the document, and, if found, highlight + scroll to them. Then, it would clear the list of uninvoked directives.

However, `ScrollToRef()` is called multiple times during a page load, and the DOM can change (e.g. if `DOMContentLoaded` does some changes).

This patch fixes this by only removing the text directives which were found in the document from the list of uninvoked directives.
This way, subsequent calls to `ScrollToRef()` would ensure that all text directives are highlighted, and the first text directive is scrolled to.
Additionally, all remaining uninvoked text directives at the end of a page load (ie. text directives that are not on the page) are cleared.

A test was added as well.

Differential Revision: https://phabricator.services.mozilla.com/D211260
2024-06-19 17:16:39 +00:00

25 lines
659 B
HTML

<!DOCTYPE html>
<title>
Navigating to a text fragment which is only available after `DOMContentLoaded`
</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", () => {
document.body.innerHTML = `<div style="margin-top: 200vh">After DOMContentLoaded</div>`;
});
</script>
</body>