2 <title>Navigating to a text fragment anchor
</title>
3 <script src=
"stash.js"></script>
5 function isInView(element
) {
6 let rect
= element
.getBoundingClientRect();
7 return rect
.top
>= 0 && rect
.top
<= window
.innerHeight
8 && rect
.left
>= 0 && rect
.left
<= window
.innerWidth
;
11 function checkScroll() {
12 let position
= 'unknown';
13 if (window
.scrollY
== 0)
15 else if (isInView(document
.getElementById('element')))
17 else if (isInView(document
.getElementById('text')))
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';
25 else if (isInView(document
.getElementById('shadow-parent')))
26 position
= 'shadow-parent';
27 else if (isInView(document
.getElementById('hidden')))
29 else if (isInView(document
.getElementById('horizontal-scroll')) && window
.scrollX
> 0)
30 position
= 'horizontal-scroll';
32 let target
= document
.querySelector(":target");
34 if (!target
&& position
== 'shadow-parent') {
35 let shadow
= document
.getElementById("shadow-parent").shadowRoot
.firstElementChild
;
36 if (shadow
.matches(":target")) {
43 scrollPosition
: position
,
44 href
: window
.location
.href
,
45 target
: target
? target
.id
: 'undefined'
48 let key
= (new URL(document
.location
)).searchParams
.get("key");
49 stashResultsThenClose(key
, results
);
52 // Ensure two animation frames on load to test the fallback to element anchor,
53 // which gets queued for the next frame if the text fragment is not found.
54 window
.onload = function() {
55 window
.requestAnimationFrame(function() {
56 window
.requestAnimationFrame(checkScroll
);
62 /*
1000px margin on top and bottom so only one section can be in view. */
76 <div id=
"element" class=
"scroll-section">Element
</div>
77 <p id=
"text" class=
"scroll-section">
78 This is a test page !$'()*+./:;=?@_~ &,-
ネコ
80 foo foo foo bar bar bar
82 <p id=
"more-text" class=
"scroll-section">More test page text
</p>
83 <div class=
"scroll-section">
86 <p id=
"cross-node-context">test page
</p>
88 <div><p>suffix
</p></div>
90 <p id=
"text-directive-parameters" class=
"scroll-section">this,is,test,page
</p>
91 <div id=
"shadow-parent" class=
"scroll-section"></div>
93 let shadow
= document
.getElementById("shadow-parent").attachShadow({mode
: 'open'});
94 shadow
.innerHTML
= '<p id="shadow">shadow text</p>';
96 <p id=
"hidden" class=
"scroll-section">hidden text
</p>
97 <p id=
"horizontal-scroll" class=
"scroll-section">horizontally scrolled text
</p>
98 <p id=
"display-none" class=
"scroll-section">display none
</p>