summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scroll-to-text-fragment/iframes.sub.html
blob: eb6594c2839d30717434c63687969da7bec87e7f (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!doctype html>
<title>Navigating to text fragment directives in iframes</title>
<meta charset=utf-8>
<meta name="timeout" content="long">
<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
  function getResult(iframe) {
    return new Promise((resolve) => {
      window.addEventListener('message', (e) => {
        resolve(e.data);
      }, {once: true});
      iframe.contentWindow.postMessage('getResult', '*');
    });
  }

  function reset(iframe) {
    return new Promise((resolve) => {
      window.addEventListener('message', (e) => {
        resolve();
      }, {once: true});
      iframe.contentWindow.postMessage('reset', '*');
    });
  }

  function runTests() {
    const attribute_iframe = document.getElementById('srcattrib');
    const sameorigin_iframe = document.getElementById('sameorigin');
    const crossorigin_iframe = document.getElementById('crossorigin');

    // Check behavior that occurs when a text fragment is specified directly in
    // an iframe's src attribute. We expect the text fragment to be blocked.
    promise_test(t => new Promise(async resolve => {
        // No reset since we're checking the hash specified in the `src` attribute.
        const data = await getResult(attribute_iframe);
        resolve(data);
      }).then( data => {
        assert_equals(data.href.indexOf(':~:'), -1, 'Expected fragment directive to be stripped from the URL.');
        assert_equals(data.scrollPosition, 'top', 'Should not scroll to text fragment');
    }), 'Text fragment specified in iframe.src');

    // Check behavior when we set a text fragment using script from a
    // same-origin parent. The text fragment should be allowed because this is
    // a same-document navigation initiated by an origin that's same-origin
    // with the current document.
    promise_test(t => new Promise(async resolve => {
        await reset(sameorigin_iframe);
        sameorigin_iframe.contentWindow.location = `${sameorigin_iframe.src}#:~:text=Target`;
        const data = await getResult(sameorigin_iframe);
        resolve(data);
      }).then( data => {
        assert_equals(data.href.indexOf(':~:'), -1, 'Expected fragment directive to be stripped from the URL.');
        assert_equals(data.scrollPosition, 'target', 'Should scroll to text fragment');
    }), 'Navigate same-origin iframe via window.location');

    // Check behavior when we set a text fragment using script from a
    // cross-origin parent. The text fragment should be blocked because the
    // initiating origin is not same-origin with the current document.
    promise_test(t => new Promise(async resolve => {
        await reset(crossorigin_iframe);
        crossorigin_iframe.contentWindow.location = `${crossorigin_iframe.src}#:~:text=Target`;
        const data = await getResult(crossorigin_iframe);
        resolve(data);
      }).then( data => {
        assert_equals(data.href.indexOf(':~:'), -1, 'Expected fragment directive to be stripped from the URL.');
        assert_equals(data.scrollPosition, 'top', 'Should not to scroll to text fragment.');
    }), 'Navigate cross-origin iframe via window.location');

    // Check the element-id fallback behavior when the text is not found. We
    // should fallback to a regular element-id hash navigation.
    promise_test(t => new Promise(async resolve => {
        await reset(sameorigin_iframe);
        sameorigin_iframe.contentWindow.location = `${sameorigin_iframe.src}#elementid:~:text=NonExistentText`;
        const data = await getResult(sameorigin_iframe);
        resolve(data);
      }).then( data => {
        assert_equals(data.scrollPosition, 'elementid', 'Should scroll to the element-id anchor.');
    }), 'Non-matching text with element-id fallback');

    // Check the element-id fallback behaviour when used across origins. The
    // text fragment should be blocked across origins but the element id hash
    // should not.
    promise_test(t => new Promise(async resolve => {
        await reset(crossorigin_iframe);
        crossorigin_iframe.contentWindow.location = `${crossorigin_iframe.src}#elementid:~:text=Target%20Text`;
        const data = await getResult(crossorigin_iframe);
        resolve(data);
      }).then( data => {
        assert_equals(data.scrollPosition, 'elementid', 'Should scroll to the element-id anchor.');
    }), 'Cross-origin with element-id fallback');
  }
</script>
<style>
  iframe {
    width: 100px;
    height: 100px;
  }
</style>
<body onload="runTests()">
  <div>
    Same-Origin with text fragment in src attribute:
    <iframe id="srcattrib" src="iframe-target.html#:~:text=Target"></iframe>
  </div>
  <div>
    Same-Origin:
    <iframe id="sameorigin" src="iframe-target.html"></iframe>
  </div>
  <div>
    Cross-Origin:
    <iframe id="crossorigin" src="http://{{hosts[][www]}}:{{ports[http][0]}}/scroll-to-text-fragment/iframe-target.html"></iframe>
  </div>
</body>