Skip to content

Commit

Permalink
Highlight API: Add test coverage
Browse files Browse the repository at this point in the history
These tests are meant to verify that:
 - UAs must not define any styles for custom highlight pseudo-elements
in the default UA stylesheet.
 - A custom highlight pseudo-element inherits the styles of its
originating element.

See https://drafts.csswg.org/css-highlight-api-1/#default-styles

Bug: 1164461
Change-Id: I955f39ee185fe2282d7599c07ff1cdd7df2d7059
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3273351
Commit-Queue: Fernando Fiori <[email protected]>
Reviewed-by: Dan Clark <[email protected]>
Cr-Commit-Position: refs/heads/main@{#944166}
  • Loading branch information
ffiori authored and pull[bot] committed Apr 17, 2023
1 parent 20be97e commit 1468666
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<style>
body {
font-size: 3em;
font-weight: bolder;
}
#affected2 {
color: green;
}
</style>
<body><span>This should have default style (black).</span><span id="affected2">This should have 'highlight2' style (green).</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<title>CSS Highlight API Test: Overlapping Highlights</title>
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/">
<link rel="match" href="custom-highlight-painting-overlapping-highlights-001-ref.html">
<meta name="assert" value="UAs must not define any styles for custom highlight pseudo-elements in the default UA stylesheet.">
<style>
body {
font-size: 3em;
font-weight: bolder;
}
#affected1::highlight(highlight1) {
color: red;
}
#affected2::highlight(highlight2) {
color: green;
}
</style>
<body><span id="affected1">This should have default style (black).</span><span id="affected2">This should have 'highlight2' style (green).</span>
<script>
/*
This test is meant to verify that:
- UAs must not define any styles for custom highlight pseudo-elements in
the default UA stylesheet.
- A custom highlight pseudo-element inherits the styles of its originating
element.
In this test, highlight2 has higher priority because it was registered
later, so it is painted over highlight1. This includes painting for
span#affected1, where there is no CSS rule for highlight2. The result is
that highlight2 paints on top for span#affected1, using the originating
element's color. Thus it appears as if no highlight was painted at all for
span#affected1.
See https://drafts.csswg.org/css-highlight-api-1/#default-styles
*/

const node = document.body;
let r = new Range();
r.setStart(node, 0);
r.setEnd(node, 2);

CSS.highlights.set("highlight1", new Highlight(r));
CSS.highlights.set("highlight2", new Highlight(r));
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<style>
div {
font-size: 3em;
font-weight: bolder;
}
#affected1 {
color: green;
background-color: rgb(0, 255, 0, 0.5);
}
#affected2 {
background-color: rgba(100, 0, 100, 0.5);
}
</style>
<body><div><span id="affected1">Green on lime.</span><span id="affected2">Black on maroon.</span></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<title>CSS Highlight API Test: Overlapping Highlights</title>
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/">
<link rel="match" href="custom-highlight-painting-overlapping-highlights-002-ref.html">
<meta name="assert" value="UAs must not define any styles for custom highlight pseudo-elements in the default UA stylesheet.">
<style>
div {
font-size: 3em;
font-weight: bolder;
}
#affected1::highlight(highlight1) {
color: green;
background-color: rgb(0, 255, 0, 0.5);
}
#affected2::highlight(highlight2) {
color: red;
background-color: rgba(100, 0, 100, 0.5);
}
</style>
<body><div><span id="affected1">Green on lime.</span><span id="affected2">Black on maroon.</span></div>
<script>
/*
This test is meant to verify that:
- UAs must not define any styles for custom highlight pseudo-elements in
the default UA stylesheet.
- A custom highlight pseudo-element inherits the styles of its originating
element.
In this test, highlight1 has higher priority, so it is painted over
highlight2. This includes painting for span#affected2, where there is no CSS
rule for highlight1. The result is that highlight1 paints on top for
span#affected2, using the originating element's color (black) and background
color (transparent). Thus, span#affected2 gets text color black, and the
background remains the same color as that applied by highlight2 (maroon).
See https://drafts.csswg.org/css-highlight-api-1/#default-styles
*/

const div = document.querySelector("div");

let r = new Range();
r.setStart(div, 0);
r.setEnd(div, 2);
let r2 = new Range();
r2.setStart(div, 0);
r2.setEnd(div, 2);

let h = new Highlight(r);
h.priority = 3;
let h2 = new Highlight(r2);
h2.priority = 2;

CSS.highlights.set("highlight1", h);
CSS.highlights.set("highlight2", h2);
</script>

0 comments on commit 1468666

Please sign in to comment.