Skip to content

Commit 6afd6f5

Browse files
estellechrisdavidmillsgithub-actions[bot]Josh-Cena
authored
New module: CSS custom highlight API (#40430)
* updated to text decoration module * New module: CSS custom highlight API * revert from rebase * Update files/en-us/web/css/css_custom_highlight_api/index.md Co-authored-by: Chris Mills <[email protected]> * Update files/en-us/web/css/css_custom_highlight_api/index.md Co-authored-by: Chris Mills <[email protected]> * Apply suggestions from code review Co-authored-by: Chris Mills <[email protected]> * Update files/en-us/web/css/css_custom_highlight_api/index.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Joshua Chen <[email protected]> * make example work --------- Co-authored-by: Chris Mills <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Joshua Chen <[email protected]>
1 parent 81f3a22 commit 6afd6f5

File tree

10 files changed

+114
-1
lines changed

10 files changed

+114
-1
lines changed

files/en-us/web/api/css/highlights_static/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ CSS.highlights.set("my-custom-highlight", myCustomHighlight);
5252
## See also
5353

5454
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
55+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
5556
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)

files/en-us/web/api/css_custom_highlight_api/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,4 @@ The result is shown below. Type text within the search field to highlight matche
237237
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)
238238
- HTML [`contentEditable`](/en-US/docs/Web/HTML/Reference/Global_attributes/contenteditable) attribute
239239
- CSS {{cssxref("pseudo-elements")}}
240+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module

files/en-us/web/api/highlight/highlight/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ const highlight = new Highlight(range1, range2);
5555
## See also
5656

5757
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
58+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
5859
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)

files/en-us/web/api/highlight/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,5 @@ The following CSS code snippet demonstrates how to style the registered custom h
107107
## See also
108108

109109
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
110+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
110111
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)

files/en-us/web/api/highlight/type/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ spellErrorsHighlight.type = "spelling-error";
4646
## See also
4747

4848
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
49+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
4950
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)

files/en-us/web/api/highlightregistry/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,5 @@ CSS.highlights.set("my-custom-highlight", highlight);
105105
## See also
106106

107107
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
108+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
108109
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)

files/en-us/web/api/highlightregistry/set/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,5 @@ CSS.highlights
6565
## See also
6666

6767
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
68+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
6869
- [CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web](https://css-tricks.com/css-custom-highlight-api-early-look/)

files/en-us/web/api/range/range/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ selection.addRange(range);
7575

7676
- [The DOM interfaces index](/en-US/docs/Web/API/Document_Object_Model)
7777
- {{domxref("Document.createRange()")}}
78+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module

files/en-us/web/css/_doublecolon_highlight/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,6 @@ for (let i = 0; i < textNode.textContent.length; i++) {
124124

125125
## See also
126126

127-
- [CSS custom highlight API](/en-US/docs/Web/API/CSS_Custom_Highlight_API)
127+
- [CSS custom highlight API](/en-US/docs/Web/CSS/CSS_custom_highlight_API) module
128+
- [CSS custom highlight](/en-US/docs/Web/API/CSS_Custom_Highlight_API) API
128129
- [CSS pseudo-elements](/en-US/docs/Web/CSS/CSS_pseudo-elements) module
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: CSS custom highlight API
3+
slug: Web/CSS/CSS_custom_highlight_API
4+
page-type: css-module
5+
spec-urls: https://drafts.csswg.org/css-highlight-api-1/
6+
sidebar: cssref
7+
---
8+
9+
The **CSS custom highlight API** module provides a programmatic way to target specific ranges of text defined by range objects, without affecting the underlying DOM structure. The range objects can then be selected via `::highlight()` pseudo-elements, and have highlight styles added and removed. The features of this module can create highlight effects similar to how text editors highlight spelling or grammar errors, and code editors highlight syntax errors.
10+
11+
The CSS Custom Highlight API extends the concept of other highlight pseudo-elements such as {{cssxref('::selection')}}, {{cssxref('::spelling-error')}}, {{cssxref('::grammar-error')}}, and {{cssxref('::target-text')}} by providing a way to create arbitrary text ranges (defined as {{domxref('Range')}} objects in JavaScript) and style them via CSS, rather than being limited to browser-defined ranges.
12+
13+
## Custom highlight API in action
14+
15+
To enable styling text ranges on a webpage using the CSS Custom Highlight API, you create a {{domxref("Range")}} object, then a {{domxref("Highlight")}} object for the range. After registering the highlight using the {{domxref("HighlightRegistry.set()")}} method, you can then select the range using the {{cssxref("::highlight", "::highlight()")}} pseudo-element. The name defined in the `set()` method is used as the parameter of the `::highlight()` pseudo-element selector to select that range.The range selected by the `::highlight()` pseudo-element can be styled using a [limited number of properties](/en-US/docs/Web/CSS/::highlight#allowable_properties).
16+
17+
```html-nolint hidden
18+
<h1>Directions</h1>
19+
<h2>Lincoln Memorial to Martin Luther King, Jr. Memorial</h2>
20+
<ol><li>Head south on Lincoln Memorial Circle</li
21+
><li>Turn right toward Independence Ave</li
22+
><li>Turn left onto Independence Ave</li
23+
><li>Turn right onto West Basin Dr</li
24+
><li>Look up when you reach 64 Independence Ave!</li>
25+
</ol>
26+
<hr />
27+
<label
28+
>Number of steps completed:
29+
<input type="number" min="0" max="5" value="0" id="currentStep" />
30+
</label>
31+
```
32+
33+
This example uses the {{cssxref("text-decoration")}} property to strike throgh the `steps` highlight range defined by our JavaScript:
34+
35+
```css
36+
::highlight(steps) {
37+
text-decoration: line-through;
38+
color: blue;
39+
}
40+
```
41+
42+
We create a `Range` with a start and end node (which is the same node in this case). We then set this range as the `Highlight` using the `set()` method of the CSS `HighlightRegistry` interface.
43+
44+
```js
45+
const rangeToHighlight = new Range();
46+
const list = document.querySelector("ol");
47+
rangeToHighlight.setStart(list, 0);
48+
rangeToHighlight.setEnd(list, 0);
49+
50+
CSS.highlights.set("steps", new Highlight(rangeToHighlight));
51+
```
52+
53+
An event listener updates the end of the highlighted range when the number of completed steps changes:
54+
55+
```js
56+
const currentPositionSlider = document.querySelector("input");
57+
currentPositionSlider.addEventListener("change", (e) => {
58+
rangeToHighlight.setEnd(list, e.target.value);
59+
});
60+
```
61+
62+
{{ EmbedLiveSample('Custom highlight API in action', 700, 300) }}
63+
64+
## Reference
65+
66+
### Pseudo-elements
67+
68+
- {{CSSXref("::highlight()")}}
69+
70+
### Interfaces
71+
72+
- {{domxref("Highlight")}}
73+
- {{domxref("HighlightRegistry")}}
74+
75+
### Interface extensions
76+
77+
This module adds properties and methods to interfaces defined in other specifications.
78+
79+
- {{domxref("CSS")}}
80+
- {{domxref("CSS.highlights")}}
81+
82+
## Guides
83+
84+
- [CSS custom highlight API](/en-US/docs/Web/API/CSS_Custom_Highlight_API#concepts_and_usage)
85+
- : The concepts and usage of the CSS custom highlight API, including creating `Range` and `Highlight` objects, registering the highlights using the `HighlightRegistry`, and styling the highlights using the `::highlight()` pseudo-element.
86+
87+
## Related concepts
88+
89+
- {{CSSXref("::grammar-error")}}
90+
- {{CSSXref("::selection")}}
91+
- {{CSSXref("::spelling-error")}}
92+
- {{CSSXref("::target-text")}}
93+
- {{domxref("Range")}} Interface and {{domxref("Range.range", "Range()")}} constructor
94+
- [Text fragments](/en-US/docs/Web/URI/Reference/Fragment/Text_fragments)
95+
- {{domxref("FragmentDirective")}} Interface
96+
97+
## Specifications
98+
99+
{{Specifications}}
100+
101+
## See also
102+
103+
- [CSS pseudo-element module](/en-US/docs/Web/CSS/CSS_pseudo-elements)
104+
- [CSS Object Model (CSSOM)](/en-US/docs/Web/API/CSS_Object_Model) APIs

0 commit comments

Comments
 (0)