| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <title>Navigating to a text fragment anchor</title> |
| Nick Burris | 97e6b87 | 2019-12-11 19:06:38 | [diff] [blame] | 3 | <script src="stash.js"></script> |
| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 4 | <script> |
| Nick Burris | 603a271 | 2019-09-06 21:16:47 | [diff] [blame] | 5 | function isInView(element) { |
| Nick Burris | c1c27e2 | 2019-11-04 23:46:41 | [diff] [blame] | 6 | let rect = element.getBoundingClientRect(); |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 7 | return rect.top >= 0 && rect.top <= window.innerHeight |
| 8 | && rect.left >= 0 && rect.left <= window.innerWidth; |
| Nick Burris | 603a271 | 2019-09-06 21:16:47 | [diff] [blame] | 9 | } |
| 10 | |
| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 11 | function checkScroll() { |
| Nick Burris | c1c27e2 | 2019-11-04 23:46:41 | [diff] [blame] | 12 | let position = 'unknown'; |
| Nick Burris | 603a271 | 2019-09-06 21:16:47 | [diff] [blame] | 13 | if (window.scrollY == 0) |
| 14 | position = 'top'; |
| 15 | else if (isInView(document.getElementById('element'))) |
| 16 | position = 'element'; |
| 17 | else if (isInView(document.getElementById('text'))) |
| 18 | position = 'text'; |
| Nick Burris | 186457c | 2019-11-06 17:55:01 | [diff] [blame] | 19 | else if (isInView(document.getElementById('more-text'))) |
| 20 | position = 'more-text'; |
| 21 | else if (isInView(document.getElementById('cross-node-context'))) |
| 22 | position = 'cross-node-context'; |
| 23 | else if (isInView(document.getElementById('text-directive-parameters'))) |
| 24 | position = 'text-directive-parameters'; |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 25 | else if (isInView(document.getElementById('shadow-parent'))) |
| 26 | position = 'shadow-parent'; |
| 27 | else if (isInView(document.getElementById('hidden'))) |
| 28 | position = 'hidden'; |
| 29 | else if (isInView(document.getElementById('horizontal-scroll')) && window.scrollX > 0) |
| 30 | position = 'horizontal-scroll'; |
| Nick Burris | 603a271 | 2019-09-06 21:16:47 | [diff] [blame] | 31 | |
| Nick Burris | 7139b3d | 2019-12-18 18:58:12 | [diff] [blame] | 32 | let target = document.querySelector(":target"); |
| 33 | |
| 34 | if (!target && position == 'shadow-parent') { |
| 35 | let shadow = document.getElementById("shadow-parent").shadowRoot.firstElementChild; |
| 36 | if (shadow.matches(":target")) { |
| 37 | target = shadow; |
| 38 | position = 'shadow'; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | let results = { |
| 43 | scrollPosition: position, |
| 44 | href: window.location.href, |
| 45 | target: target ? target.id : 'undefined' |
| 46 | }; |
| Nick Burris | 97e6b87 | 2019-12-11 19:06:38 | [diff] [blame] | 47 | |
| 48 | let key = (new URL(document.location)).searchParams.get("key"); |
| 49 | stashResults(key, results); |
| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 50 | } |
| 51 | </script> |
| 52 | <style> |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 53 | .scroll-section { |
| 54 | /* 1000px margin on top and bottom so only one section can be in view. */ |
| 55 | margin: 1000px 0px; |
| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 56 | } |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 57 | #hidden { |
| 58 | visibility: hidden; |
| Nick Burris | 29beb46 | 2019-08-27 20:02:08 | [diff] [blame] | 59 | } |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 60 | #horizontal-scroll { |
| 61 | margin-left: 2000px; |
| Nick Burris | 186457c | 2019-11-06 17:55:01 | [diff] [blame] | 62 | } |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 63 | #display-none { |
| 64 | display: none; |
| Nick Burris | 186457c | 2019-11-06 17:55:01 | [diff] [blame] | 65 | } |
| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 66 | </style> |
| Nick Burris | 603a271 | 2019-09-06 21:16:47 | [diff] [blame] | 67 | <body onload="window.requestAnimationFrame(checkScroll)"> |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 68 | <div id="element" class="scroll-section">Element</div> |
| 69 | <p id="text" class="scroll-section">This is a test page !$'()*+./:;=?@_~ &,- ネコ</p> |
| 70 | <p id="more-text" class="scroll-section">More test page text</p> |
| Nick Burris | 7139b3d | 2019-12-18 18:58:12 | [diff] [blame] | 71 | <div class="scroll-section"> |
| Nick Burris | 186457c | 2019-11-06 17:55:01 | [diff] [blame] | 72 | <div> |
| 73 | <p>prefix</p> |
| Nick Burris | 7139b3d | 2019-12-18 18:58:12 | [diff] [blame] | 74 | <p id="cross-node-context">test page</p> |
| Nick Burris | 186457c | 2019-11-06 17:55:01 | [diff] [blame] | 75 | </div> |
| 76 | <div><p>suffix</p></div> |
| 77 | </div> |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 78 | <p id="text-directive-parameters" class="scroll-section">this,is,test,page</p> |
| 79 | <div id="shadow-parent" class="scroll-section"></div> |
| 80 | <script> |
| 81 | let shadow = document.getElementById("shadow-parent").attachShadow({mode: 'open'}); |
| Nick Burris | 7139b3d | 2019-12-18 18:58:12 | [diff] [blame] | 82 | shadow.innerHTML = '<p id="shadow">shadow text</p>'; |
| Nick Burris | fb6ccd5 | 2019-11-12 03:34:29 | [diff] [blame] | 83 | </script> |
| 84 | <p id="hidden" class="scroll-section">hidden text</p> |
| 85 | <p id="horizontal-scroll" class="scroll-section">horizontally scrolled text</p> |
| 86 | <p id="display-none" class="scroll-section">display none</p> |
| Nick Burris | 3c56e31 | 2019-08-22 14:48:48 | [diff] [blame] | 87 | </body> |