File: same-document-tests.html

package info (click to toggle)
thunderbird 1%3A115.16.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,476,252 kB
  • sloc: cpp: 6,972,150; javascript: 5,209,211; ansic: 3,507,222; python: 1,137,609; asm: 432,531; xml: 205,149; java: 175,761; sh: 116,485; makefile: 22,152; perl: 13,971; objc: 12,561; yacc: 4,583; pascal: 2,840; lex: 1,720; ruby: 1,075; exp: 762; sql: 666; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (80 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!doctype html>
<title>Same document navigation to text fragment directives</title>
<meta charset=utf-8>
<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
  function reset() {
    location.hash = '';
    window.scrollTo(0, 0);
  }

  function isInViewport(element) {
    const viewportRect = {
      left: visualViewport.offsetLeft,
      top: visualViewport.offsetTop,
      right: visualViewport.offsetLeft + visualViewport.width,
      bottom: visualViewport.offsetTop + visualViewport.height
    };

    const elementRect = element.getBoundingClientRect();
    const elementCenter = {
      x: elementRect.left + elementRect.width / 2,
      y: elementRect.top + elementRect.height / 2
    };

    return elementCenter.x > viewportRect.left &&
           elementCenter.x < viewportRect.right &&
           elementCenter.y > viewportRect.top &&
           elementCenter.y < viewportRect.bottom;
  }

  function runTests() {
    // 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');
  }
</script>
<style>
  div {
    margin: 200vh 0 200vh 0;
  }
</style>
<body onload="runTests()">
  <div id="text">
    This is a line of text.
  </div>
  <div id="elementid">
    This div has an id: 'elementid'.
  </div>
</body>