blob: 5cec6aee3a2bd62723e2c345b8282b1e31cf6c86 [file] [log] [blame]
David Bokan708d75e2022-01-13 23:30:511<!doctype html>
2<title>Allow text fragments in HTML documents only</title>
3<meta charset=utf-8>
4<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
5<script src="/resources/testharness.js"></script>
6<script src="/resources/testharnessreport.js"></script>
7<script src="/resources/testdriver.js"></script>
8<script src="/resources/testdriver-vendor.js"></script>
9<script src="/common/utils.js"></script>
10<script src="stash.js"></script>
11
12<script>
13function rAF(win) {
14 return new Promise((resolve) => {
15 win.requestAnimationFrame(resolve);
16 });
17}
18
19function openPopup(url) {
20 return new Promise((resolve) => {
21 test_driver.bless('Open a URL with a text fragment directive', () => {
22 const popup = window.open(url, '_blank', 'width=300,height=300');
23 popup.onload = () => resolve(popup);
24 });
25 });
26}
27
28const test_cases = [
David Bokanbad5c482023-11-29 20:47:0229 {
30 filename: 'text-html.html',
31 expected: 'allowed',
32 },
33 {
34 filename: 'text-css.css',
35 expected: 'blocked',
36 },
37 {
38 filename: 'text-javascript.js',
39 expected: 'blocked',
40 },
41 {
42 filename: 'application-json.json',
43 expected: 'blocked',
44 },
45 {
46 filename: 'text-plain.txt',
47 expected: 'allowed',
48 },
49 {
50 filename: 'application-xml.xml',
51 expected: 'blocked',
52 },
David Bokan708d75e2022-01-13 23:30:5153];
54
55for (let test_case of test_cases) {
David Bokanbad5c482023-11-29 20:47:0256 const filename = test_case.filename;
57 const expected = test_case.expected;
58 const mediaType = filename.split('.')[0].replace('-', '/');
59
David Bokan708d75e2022-01-13 23:30:5160 promise_test(async function (t) {
David Bokanbad5c482023-11-29 20:47:0261 const popup = await openPopup(`resources/${filename}#:~:text=target`);
62
63 // The WPT server should provide the correct content-type header from the
64 // file extension.
65 assert_equals(popup.document.contentType, mediaType);
David Bokan708d75e2022-01-13 23:30:5166
67 // rAF twice in case there is any asynchronicity in scrolling to the target.
68 await rAF(popup);
69 await rAF(popup);
70
David Bokanbad5c482023-11-29 20:47:0271 const did_scroll = popup.scrollY > 0;
72 const expected_scroll = expected == 'allowed';
73 assert_equals(did_scroll, expected_scroll, 'scrolled to fragment');
74
David Bokan708d75e2022-01-13 23:30:5175 popup.close();
David Bokanbad5c482023-11-29 20:47:0276 }, `Text directive ${expected} in ${mediaType}`);
David Bokan708d75e2022-01-13 23:30:5177}
78
79</script>