Files
tubestation/testing/web-platform/tests/scroll-to-text-fragment/same-document-tests.js
Andrew McCreight b7c6e0957c Bug 1898536, part 2 - Add an even more basic element fragment navigation test. r=jjaschke
This will be useful when we test the force-load-at-top variations.

Differential Revision: https://phabricator.services.mozilla.com/D211385
2024-07-02 14:13:55 +00:00

49 lines
1.9 KiB
JavaScript

function reset() {
location.hash = '';
window.scrollTo(0, 0);
}
function runTests() {
promise_test(async t => {
assert_implements(document.fragmentDirective, 'Text directive not implemented');
reset();
location.hash = 'elementid';
await t.step_wait(() => window.scrollY > 0, "Wait for scroll");
assert_true(isInViewport(document.getElementById('elementid')), 'Scrolled to text');
}, 'Basic element fragment navigation');
// Ensure a simple text directive works correctly when navigated to the
// same document using `location.hash`.
promise_test(async t => {
assert_implements(document.fragmentDirective, 'Text directive not implemented');
reset();
location.hash = ':~:text=line%20of%20text';
await t.step_wait(() => window.scrollY > 0, "Wait for scroll");
assert_true(isInViewport(document.getElementById('text')), 'Scrolled to text');
}, 'Basic text directive navigation');
// Test that we correctly fallback to the element id when we have a text
// directive that doesn't match any text in the page.
promise_test(async t => {
assert_implements(document.fragmentDirective, 'Text directive not implemented');
reset();
location.hash = 'elementid:~:text=textDoesntExist';
await t.step_wait(() => window.scrollY > 0, "Wait for scroll");
assert_true(isInViewport(document.getElementById('elementid')), 'Scrolled to `elementid`');
}, 'Basic element id fallback');
// Test that we correctly fallback to the element id when we have a text
// directive that's malformed and won't be parsed.
promise_test(async t => {
assert_implements(document.fragmentDirective, 'Text directive not implemented');
reset();
location.hash = 'elementid:~:text=,,,,,';
await t.step_wait(() => window.scrollY > 0, "Wait for scroll");
assert_true(isInViewport(document.getElementById('elementid')), 'Scrolled to `elementid`');
}, 'Malformed text directive element id fallback');
}