Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<!doctype html>
<meta name="author" title="Fernando Fiori" href="mailto:ffiori@microsoft.com">
<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.">
<link rel="help" href="https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/highlight/HighlightsFromPointsExplainer.md">
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/#interactions">
<link rel="help" href="https://drafts.csswg.org/cssom-view/#dom-range-getclientrects">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
Expand Down Expand Up @@ -38,7 +39,7 @@
// Get x and y coordinates between characters '0' and '1' on the first line (not highlighted).
x = rect.left + characterWidth;
y = rect.top + characterHeight / 2;
highlights = CSS.highlights.highlightsFromPoint(x, y);
highlight_hit_results = CSS.highlights.highlightsFromPoint(x, y);
assert_equals(highlight_hit_results.length, 0, 'CSS.highlights.highlightsFromPoint() returns an empty array when the coordinates provided are outside of the highlighted ranges');

// Get x and y coordinates between characters '2' and '3' on the first line.
Expand Down
62 changes: 29 additions & 33 deletions css/css-highlight-api/HighlightRegistry-highlightsFromPoint.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<!doctype html>
<meta name="author" title="Fernando Fiori" href="mailto:ffiori@microsoft.com">
<meta name="assert" content="HighlightRegistry.highlightsFromPoint returns the Highlights present at the coordinates provided as argument in the right order.">
<link rel="help" href="https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/highlight/HighlightsFromPointsExplainer.md">
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/#interactions">
<link rel="help" href="https://drafts.csswg.org/cssom-view/#dom-range-getclientrects">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
Expand Down Expand Up @@ -55,7 +56,7 @@
// Get x and y coordinates between '0' and '1'.
x = rect.left + characterWidth;
y = rect.top + rect.height / 2;
highlights = CSS.highlights.highlightsFromPoint(x, y);
highlight_hit_results = CSS.highlights.highlightsFromPoint(x, y);
assert_equals(highlight_hit_results.length, 0, 'CSS.highlights.highlightsFromPoint() returns an empty array when the coordinates provided point at no Highlights');

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

const textNode = document.querySelector("#example-span");

// Create a valid StaticRange, add it to a highlight, then invalidate it
// by removing the start container from the document.
// by shortening its text container below the retained end offset.
const tempSpan = document.createElement("span");
tempSpan.textContent = "temporary";
document.body.appendChild(tempSpan);
Expand All @@ -183,36 +182,33 @@
endOffset: 9
});

const validRange = new Range();
validRange.setStart(textNode.childNodes[0], 0);
validRange.setEnd(textNode.childNodes[0], 10);
const liveRange = new Range();
liveRange.selectNodeContents(tempSpan);

const highlight = new Highlight(staticRange, validRange);
const highlight = new Highlight(staticRange, liveRange);
CSS.highlights.set("example-highlight", highlight);

const rect = tempSpan.getBoundingClientRect();
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;

// Verify the highlight is returned before invalidation.
let results = CSS.highlights.highlightsFromPoint(x, y);
assert_equals(results.length, 1,
'highlightsFromPoint() returns highlight before StaticRange is invalidated');

// Invalidate the StaticRange by removing its start container.
document.body.removeChild(tempSpan);

// The point now has no content, so check at the valid range's position.
const validRect = textNode.getBoundingClientRect();
const characterWidth = validRect.width / textNode.textContent.length;
const x2 = validRect.left + 3 * characterWidth;
const y2 = validRect.top + validRect.height / 2;

results = CSS.highlights.highlightsFromPoint(x2, y2);
assert_equals(results.length, 1,
'highlightsFromPoint() still returns the highlight via the valid range');
assert_array_equals(results[0].ranges, [validRange],
'the invalid StaticRange is not included in the returned ranges');
try {
let rect = liveRange.getClientRects()[0];
let x = rect.left + rect.width / 2;
let y = rect.top + rect.height / 2;
let results = CSS.highlights.highlightsFromPoint(x, y);
assert_equals(results.length, 1);
assert_array_equals(results[0].ranges, [staticRange, liveRange],
'both overlapping ranges are returned before invalidation');

// The StaticRange retains endOffset 9, which is now out of bounds.
tempSpan.firstChild.data = "x";
rect = liveRange.getClientRects()[0];
x = rect.left + rect.width / 2;
y = rect.top + rect.height / 2;
results = CSS.highlights.highlightsFromPoint(x, y);
assert_equals(results.length, 1);
assert_array_equals(results[0].ranges, [liveRange],
'the invalid StaticRange is omitted at overlapping geometry');
} finally {
CSS.highlights.clear();
tempSpan.remove();
}
}, 'CSS.highlights.highlightsFromPoint() skips invalid StaticRanges.');

test(() => {
Expand Down
Loading