Skip to content

Commit 40d5e6d

Browse files
dandclarkchromium-wpt-export-bot
authored andcommitted
Highlight API: Don't paint unstyled custom highlights
The custom highlight API spec [1] currently states that an unstyled custom highlight should inherit the styles of its originating element. The Blink implementation follows this. However, this behavior can lead to some unintuitive results where unstyled highlights can affect the painting of the originating element, or paint over other highlights. This issue was pointed out in [2], and spec changes to fix this are pending editor review. A goal of these changes are that unstyled custom highlights do not affect rendering at all. This change updates the custom highlights implementation to achieve this by giving them a default 'transparent' foreground and background color. Some of our tests were validating the earlier unintuitive behavior involving overlapping ranges. These are updated, along with the addition of a new test to validate that a single unstyled highlight doesn't affect rendering. [1] https://drafts.csswg.org/css-highlight-api-1/#default-styles [2] w3c/csswg-drafts#6779 Bug: 1295271 Change-Id: Id7dc877c90c08240fbe79edeeeec551cdc5508c1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3449336 Reviewed-by: Delan Azabani Reviewed-by: Rune Lillesveen Commit-Queue: Dan Clark Cr-Commit-Position: refs/heads/main@{#969007}
1 parent 99e671c commit 40d5e6d

6 files changed

+49
-16
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
>
2+
<meta charset="UTF-8">
3+
<style>
4+
div {
5+
color: #00000080;
6+
}
7+
style>
8+
<body>
9+
<div>foobardiv>
10+
body>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
>
2+
<meta charset="UTF-8">
3+
<title>CSS Highlight API Test: Inheritancetitle>
4+
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/">
5+
<link rel="match" href="custom-highlight-painting-inheritance-003-ref.html">
6+
<meta name="assert" value="Unstyled highlights should not be visible">
7+
<style>
8+
div {
9+
color: #00000080;
10+
background-color: red;
11+
width: 0;
12+
}
13+
style>
14+
<body>
15+
<div id="target">foobardiv>
16+
<script>
17+
const node = document.getElementById("target");
18+
let r = new Range();
19+
r.setStart(node.firstChild, 0);
20+
r.setEnd(node.firstChild, 3);
21+
CSS.highlights.set("div-highlight", new Highlight(r));
22+
script>
23+
body>

css/css-highlight-api/painting/custom-highlight-painting-overlapping-highlights-001-ref.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
body {
55
font-size: 3em;
66
font-weight: bolder;
7+
8+
}
9+
#affected1 {
10+
color: red;
711
}
812
#affected2 {
913
color: green;
1014
}
1115
style>
12-
<body><span>This should have default style (black).span><span id="affected2">This should have 'highlight2' style (green).span>
16+
<body><span id="affected1">This should have 'highlight1' style (red).span><span id="affected2">This should have 'highlight2' style (green).span>

css/css-highlight-api/painting/custom-highlight-painting-overlapping-highlights-001.html

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@
1616
color: green;
1717
}
1818
style>
19-
<body><span id="affected1">This should have default style (black).span><span id="affected2">This should have 'highlight2' style (green).span>
19+
<body><span id="affected1">This should have 'highlight1' style (red).span><span id="affected2">This should have 'highlight2' style (green).span>
2020
<script>
2121
/*
2222
This test is meant to verify that:
2323
- UAs must not define any styles for custom highlight pseudo-elements in
2424
the default UA stylesheet.
25-
- A custom highlight pseudo-element inherits the styles of its originating
26-
element.
25+
- UAs do not paint unstyled custom highlights.
2726
2827
In this test, highlight2 has higher priority because it was registered
2928
later, so it is painted over highlight1. This includes painting for
30-
span#affected1, where there is no CSS rule for highlight2. The result is
31-
that highlight2 paints on top for span#affected1, using the originating
32-
element's color. Thus it appears as if no highlight was painted at all for
33-
span#affected1.
29+
span#affected1, where there is no CSS rule for highlight2. But since unstyled
30+
custom highlights are not painted, span#affected1 is still painted with the
31+
styles for highlight1.
3432
3533
See https://drafts.csswg.org/css-highlight-api-1/#default-styles
3634
*/

css/css-highlight-api/painting/custom-highlight-painting-overlapping-highlights-002-ref.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
background-color: rgb(0, 255, 0, 0.5);
1111
}
1212
#affected2 {
13+
color: red;
1314
background-color: rgba(100, 0, 100, 0.5);
1415
}
1516
style>
16-
<body><div><span id="affected1">Green on lime.span><span id="affected2">Black on maroon.span>div>
17+
<body><div><span id="affected1">Green on lime.span><span id="affected2">Red on maroon.span>div>

css/css-highlight-api/painting/custom-highlight-painting-overlapping-highlights-002.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@
1818
background-color: rgba(100, 0, 100, 0.5);
1919
}
2020
style>
21-
<body><div><span id="affected1">Green on lime.span><span id="affected2">Black on maroon.span>div>
21+
<body><div><span id="affected1">Green on lime.span><span id="affected2">Red on maroon.span>div>
2222
<script>
2323
/*
2424
This test is meant to verify that:
2525
- UAs must not define any styles for custom highlight pseudo-elements in
2626
the default UA stylesheet.
27-
- A custom highlight pseudo-element inherits the styles of its originating
28-
element.
27+
- UAs do not paint unstyled custom highlights.
2928
3029
In this test, highlight1 has higher priority, so it is painted over
3130
highlight2. This includes painting for span#affected2, where there is no CSS
32-
rule for highlight1. The result is that highlight1 paints on top for
33-
span#affected2, using the originating element's color (black) and background
34-
color (transparent). Thus, span#affected2 gets text color black, and the
35-
background remains the same color as that applied by highlight2 (maroon).
31+
rule for highlight1. But since unstyled custom highlights are not painted,
32+
span#affected2 is still painted with the styles for highlight2.
3633
3734
See https://drafts.csswg.org/css-highlight-api-1/#default-styles
3835
*/

0 commit comments

Comments
 (0)