|
1 | 1 | <!doctype html> |
2 | 2 | <meta name="author" title="Fernando Fiori" href="mailto:ffiori@microsoft.com"> |
3 | 3 | <meta name="assert" content="HighlightRegistry.highlightsFromPoint returns the Highlights present at the coordinates provided as argument in the right order."> |
4 | | -<link rel="help" href="https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/highlight/HighlightsFromPointsExplainer.md"> |
| 4 | +<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/#interactions"> |
| 5 | +<link rel="help" href="https://drafts.csswg.org/cssom-view/#dom-range-getclientrects"> |
5 | 6 | <script src="/resources/testharness.js"></script> |
6 | 7 | <script src="/resources/testharnessreport.js"></script> |
7 | 8 | <style> |
|
55 | 56 | // Get x and y coordinates between '0' and '1'. |
56 | 57 | x = rect.left + characterWidth; |
57 | 58 | y = rect.top + rect.height / 2; |
58 | | - highlights = CSS.highlights.highlightsFromPoint(x, y); |
| 59 | + highlight_hit_results = CSS.highlights.highlightsFromPoint(x, y); |
59 | 60 | assert_equals(highlight_hit_results.length, 0, 'CSS.highlights.highlightsFromPoint() returns an empty array when the coordinates provided point at no Highlights'); |
60 | 61 |
|
61 | 62 | // Get x and y coordinates between '2' and '3'. |
|
168 | 169 | test(() => { |
169 | 170 | CSS.highlights.clear(); |
170 | 171 |
|
171 | | - const textNode = document.querySelector("#example-span"); |
172 | | - |
173 | 172 | // Create a valid StaticRange, add it to a highlight, then invalidate it |
174 | | - // by removing the start container from the document. |
| 173 | + // by shortening its text container below the retained end offset. |
175 | 174 | const tempSpan = document.createElement("span"); |
176 | 175 | tempSpan.textContent = "temporary"; |
177 | 176 | document.body.appendChild(tempSpan); |
|
183 | 182 | endOffset: 9 |
184 | 183 | }); |
185 | 184 |
|
186 | | - const validRange = new Range(); |
187 | | - validRange.setStart(textNode.childNodes[0], 0); |
188 | | - validRange.setEnd(textNode.childNodes[0], 10); |
| 185 | + const liveRange = new Range(); |
| 186 | + liveRange.selectNodeContents(tempSpan); |
189 | 187 |
|
190 | | - const highlight = new Highlight(staticRange, validRange); |
| 188 | + const highlight = new Highlight(staticRange, liveRange); |
191 | 189 | CSS.highlights.set("example-highlight", highlight); |
192 | | - |
193 | | - const rect = tempSpan.getBoundingClientRect(); |
194 | | - const x = rect.left + rect.width / 2; |
195 | | - const y = rect.top + rect.height / 2; |
196 | | - |
197 | | - // Verify the highlight is returned before invalidation. |
198 | | - let results = CSS.highlights.highlightsFromPoint(x, y); |
199 | | - assert_equals(results.length, 1, |
200 | | - 'highlightsFromPoint() returns highlight before StaticRange is invalidated'); |
201 | | - |
202 | | - // Invalidate the StaticRange by removing its start container. |
203 | | - document.body.removeChild(tempSpan); |
204 | | - |
205 | | - // The point now has no content, so check at the valid range's position. |
206 | | - const validRect = textNode.getBoundingClientRect(); |
207 | | - const characterWidth = validRect.width / textNode.textContent.length; |
208 | | - const x2 = validRect.left + 3 * characterWidth; |
209 | | - const y2 = validRect.top + validRect.height / 2; |
210 | | - |
211 | | - results = CSS.highlights.highlightsFromPoint(x2, y2); |
212 | | - assert_equals(results.length, 1, |
213 | | - 'highlightsFromPoint() still returns the highlight via the valid range'); |
214 | | - assert_array_equals(results[0].ranges, [validRange], |
215 | | - 'the invalid StaticRange is not included in the returned ranges'); |
| 190 | + try { |
| 191 | + let rect = liveRange.getClientRects()[0]; |
| 192 | + let x = rect.left + rect.width / 2; |
| 193 | + let y = rect.top + rect.height / 2; |
| 194 | + let results = CSS.highlights.highlightsFromPoint(x, y); |
| 195 | + assert_equals(results.length, 1); |
| 196 | + assert_array_equals(results[0].ranges, [staticRange, liveRange], |
| 197 | + 'both overlapping ranges are returned before invalidation'); |
| 198 | + |
| 199 | + // The StaticRange retains endOffset 9, which is now out of bounds. |
| 200 | + tempSpan.firstChild.data = "x"; |
| 201 | + rect = liveRange.getClientRects()[0]; |
| 202 | + x = rect.left + rect.width / 2; |
| 203 | + y = rect.top + rect.height / 2; |
| 204 | + results = CSS.highlights.highlightsFromPoint(x, y); |
| 205 | + assert_equals(results.length, 1); |
| 206 | + assert_array_equals(results[0].ranges, [liveRange], |
| 207 | + 'the invalid StaticRange is omitted at overlapping geometry'); |
| 208 | + } finally { |
| 209 | + CSS.highlights.clear(); |
| 210 | + tempSpan.remove(); |
| 211 | + } |
216 | 212 | }, 'CSS.highlights.highlightsFromPoint() skips invalid StaticRanges.'); |
217 | 213 |
|
218 | 214 | test(() => { |
|
0 commit comments