| <!doctype html> |
| <title>Navigating to a text fragment anchor</title> |
| <meta name="timeout" content="long"> |
| <script src="/resources/testharness.js"></script> |
| <script src="/resources/testharnessreport.js"></script> |
| <script src="/resources/testdriver.js"></script> |
| <script src="/resources/testdriver-vendor.js"></script> |
| <script> |
| let test_cases = [ |
| { fragment: '#', expect_position: 'top' }, |
| { fragment: '#:~:text=test', expect_position: 'text' }, |
| { fragment: '#:~:text=this,page', expect_position: 'text' }, |
| { fragment: '#:~:text=this-,is,test', expect_position: 'text' }, |
| { fragment: '#:~:text=this-,is,test,-page', expect_position: 'text' }, |
| { fragment: '#:~:text=this-,is,page,-none', expect_position: 'top' }, |
| { fragment: '#:~:text=this,test,-page', expect_position: 'text' }, |
| { fragment: '#:~:text=this%20is%20a%20test%20page', expect_position: 'text' }, |
| { fragment: '#:~:text=this&text=test,page', expect_position: 'text' }, |
| { fragment: '#:~:text=tes&text=age', expect_position: 'top' }, |
| { fragment: '#pagestate:~:text=test', expect_position: 'text' }, |
| { fragment: '#pagestate:~:text=nomatch', expect_position: 'top' }, |
| { fragment: '#element:~:text=nomatch', expect_position: 'element' }, |
| { fragment: '#element:~:directive', expect_position: 'element' }, |
| ]; |
| |
| test(t => { |
| assert_equals(typeof(window.location.fragmentDirective), 'object', 'window.location.fragmentDirective is defined'); |
| }, 'Scroll to text is feature detectable via window.location.fragmentDirective'); |
| |
| for (const test_case of test_cases) { |
| promise_test(t => new Promise(resolve => { |
| let channel = new BroadcastChannel('scroll-to-text-fragment'); |
| channel.addEventListener("message", e => { |
| resolve(e.data.scrollPosition); |
| }, {once: true}); |
| |
| test_driver.bless('Open a URL with a text fragment anchor', () => { |
| window.open('scroll-to-text-fragment-target.html' + test_case.fragment, '_blank', 'noopener'); |
| }); |
| }).then(scrollPosition => { |
| assert_equals(scrollPosition, test_case.expect_position, |
| 'Expected ' + test_case.fragment + ' to scroll to ' + test_case.expect_position); |
| }), 'Test navigation with text fragment anchor ' + test_case.fragment); |
| } |
| </script> |