Skip to content

Commit 296318c

Browse files
committed
[css-highlight-api-1] Add note recommending authors to only use one name to style a highlight and add an example showcasing why using multiple names is problematic.
1 parent cce9156 commit 296318c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

css-highlight-api-1/Overview.bs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,40 @@ Registering Custom Highlights</h3>
233233
[=custom highlight name=] that is a valid CSS [=identifier=]. Using a name that is not a valid
234234
identifier can make the highlight hard, and in some cases impossible, to style via CSS.
235235

236+
Note: It is possible to [=register=] a [=custom highlight=] with more than one [=custom highlight name=].
237+
However, using more than one name to style a highlight will assign the highlight multiple different sets
238+
of styles, without a way to control the stacking order of conflicting styles within these sets
239+
during [[#painting|painting]]. This may be limiting for authors and may cause confusing painting behavior
240+
(see the [[#styling-problems-with-multiple-names-per-highlight|example]] below for more context). Therefore,
241+
<b>it is recommended that authors only use one name per highlight during styling</b>.
242+
<div class=example id=styling-problems-with-multiple-names-per-highlight>
243+
<xmp highlight=html>
244+
<style>
245+
div::highlight(bar) {
246+
color: red;
247+
}
248+
div::highlight(foo) {
249+
color: green;
250+
}
251+
</style>
252+
<body><div>abc</div>
253+
<script>
254+
let r = new Range();
255+
r.setStart(div, 0);
256+
r.setEnd(div, 1);
257+
let h = new Highlight(r);
258+
CSS.highlights.set('foo', h);
259+
CSS.highlights.set('bar', h);
260+
</script>
261+
</xmp>
262+
In the example above, the same [=custom highlight=] object is [=registered=] under the names 'foo' and 'bar'.
263+
Since each of the [=style rules=] target the same highlight and have the same [=specificity=], authors may
264+
expect the last rule to win in cascading order and the highlighted content to be green. However, each highlight
265+
name gets an independent set of highlight styles, and the highlight will be painted once per name. In
266+
this case, because 'foo' was registered before 'bar', the highlight will be first painted with 'foo''s
267+
color (green) and then with 'bar''s color (red). As a result, the highlighted content will appear red.
268+
</div>
269+
236270
<h2 id=styling-highlights>
237271
Styling Custom Highlights</h2>
238272

0 commit comments

Comments
 (0)