| David Bokan | 708d75e | 2022-01-13 23:30:51 | [diff] [blame] | 1 | <!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> |
| 13 | function rAF(win) { |
| 14 | return new Promise((resolve) => { |
| 15 | win.requestAnimationFrame(resolve); |
| 16 | }); |
| 17 | } |
| 18 | |
| 19 | function 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 | |
| 28 | const test_cases = [ |
| David Bokan | bad5c48 | 2023-11-29 20:47:02 | [diff] [blame] | 29 | { |
| 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 Bokan | 708d75e | 2022-01-13 23:30:51 | [diff] [blame] | 53 | ]; |
| 54 | |
| 55 | for (let test_case of test_cases) { |
| David Bokan | bad5c48 | 2023-11-29 20:47:02 | [diff] [blame] | 56 | const filename = test_case.filename; |
| 57 | const expected = test_case.expected; |
| 58 | const mediaType = filename.split('.')[0].replace('-', '/'); |
| 59 | |
| David Bokan | 708d75e | 2022-01-13 23:30:51 | [diff] [blame] | 60 | promise_test(async function (t) { |
| David Bokan | bad5c48 | 2023-11-29 20:47:02 | [diff] [blame] | 61 | 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 Bokan | 708d75e | 2022-01-13 23:30:51 | [diff] [blame] | 66 | |
| 67 | // rAF twice in case there is any asynchronicity in scrolling to the target. |
| 68 | await rAF(popup); |
| 69 | await rAF(popup); |
| 70 | |
| David Bokan | bad5c48 | 2023-11-29 20:47:02 | [diff] [blame] | 71 | const did_scroll = popup.scrollY > 0; |
| 72 | const expected_scroll = expected == 'allowed'; |
| 73 | assert_equals(did_scroll, expected_scroll, 'scrolled to fragment'); |
| 74 | |
| David Bokan | 708d75e | 2022-01-13 23:30:51 | [diff] [blame] | 75 | popup.close(); |
| David Bokan | bad5c48 | 2023-11-29 20:47:02 | [diff] [blame] | 76 | }, `Text directive ${expected} in ${mediaType}`); |
| David Bokan | 708d75e | 2022-01-13 23:30:51 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | </script> |