Skip to content

Commit 2e4366e

Browse files
ffiorichromium-wpt-export-bot
authored andcommitted
[highlights] Fix highlightsFromPoint WPT assertions
Correct two no-hit checks that assigned to an unused variable while asserting stale results. Make the invalid StaticRange test retain overlapping live geometry and invalidate the static offset directly, so an incorrectly returned range cannot pass unnoticed. Replace explainer links with the normative Highlight API and CSSOM View sections. Bug: 527784934 Change-Id: I32a624ebd033ce910f04bf2a6c9334eddca6b6fb
1 parent 82c2a43 commit 2e4366e

2 files changed

Lines changed: 32 additions & 35 deletions

File tree

css/css-highlight-api/HighlightRegistry-highlightsFromPoint-ranges.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<!doctype html>
22
<meta name="author" title="Fernando Fiori" href="mailto:ffiori@microsoft.com">
33
<meta name="assert" content="HighlightRegistry.highlightsFromPoint returns the Highlights and their corresponding Ranges and StaticRanges present at the coordinates provided as argument in the right order in multi-line text.">
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">
56
<script src="/resources/testharness.js"></script>
67
<script src="/resources/testharnessreport.js"></script>
78
<style>
@@ -38,7 +39,7 @@
3839
// Get x and y coordinates between characters '0' and '1' on the first line (not highlighted).
3940
x = rect.left + characterWidth;
4041
y = rect.top + characterHeight / 2;
41-
highlights = CSS.highlights.highlightsFromPoint(x, y);
42+
highlight_hit_results = CSS.highlights.highlightsFromPoint(x, y);
4243
assert_equals(highlight_hit_results.length, 0, 'CSS.highlights.highlightsFromPoint() returns an empty array when the coordinates provided are outside of the highlighted ranges');
4344

4445
// Get x and y coordinates between characters '2' and '3' on the first line.

css/css-highlight-api/HighlightRegistry-highlightsFromPoint.html

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<!doctype html>
22
<meta name="author" title="Fernando Fiori" href="mailto:ffiori@microsoft.com">
33
<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">
56
<script src="/resources/testharness.js"></script>
67
<script src="/resources/testharnessreport.js"></script>
78
<style>
@@ -55,7 +56,7 @@
5556
// Get x and y coordinates between '0' and '1'.
5657
x = rect.left + characterWidth;
5758
y = rect.top + rect.height / 2;
58-
highlights = CSS.highlights.highlightsFromPoint(x, y);
59+
highlight_hit_results = CSS.highlights.highlightsFromPoint(x, y);
5960
assert_equals(highlight_hit_results.length, 0, 'CSS.highlights.highlightsFromPoint() returns an empty array when the coordinates provided point at no Highlights');
6061

6162
// Get x and y coordinates between '2' and '3'.
@@ -168,10 +169,8 @@
168169
test(() => {
169170
CSS.highlights.clear();
170171

171-
const textNode = document.querySelector("#example-span");
172-
173172
// 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.
175174
const tempSpan = document.createElement("span");
176175
tempSpan.textContent = "temporary";
177176
document.body.appendChild(tempSpan);
@@ -183,36 +182,33 @@
183182
endOffset: 9
184183
});
185184

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);
189187

190-
const highlight = new Highlight(staticRange, validRange);
188+
const highlight = new Highlight(staticRange, liveRange);
191189
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+
}
216212
}, 'CSS.highlights.highlightsFromPoint() skips invalid StaticRanges.');
217213

218214
test(() => {

0 commit comments

Comments
 (0)