-
Notifications
You must be signed in to change notification settings - Fork 22.9k
New module: CSS custom highlight API #40430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0d96a30
updated to text decoration module
estelle 18ed103
Merge remote-tracking branch 'upstream/main'
estelle 9bcfb58
rebasing
estelle 22f3f28
New module: CSS custom highlight API
estelle c42b9a3
revert from rebase
estelle 1b35c18
Merge branch 'main' into chapi
estelle 74f0eb2
Update files/en-us/web/css/css_custom_highlight_api/index.md
estelle 2256f7e
Update files/en-us/web/css/css_custom_highlight_api/index.md
estelle ba05af8
Apply suggestions from code review
estelle da4a051
Update files/en-us/web/css/css_custom_highlight_api/index.md
estelle bc5abbe
Merge branch 'main' into chapi
estelle 0bc297e
Apply suggestions from code review
estelle d4c049d
make example work
estelle 7c20929
Merge branch 'main' into chapi
estelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
--- | ||
title: CSS custom highlight API | ||
slug: Web/CSS/CSS_custom_highlight_API | ||
chrisdavidmills marked this conversation as resolved.
Show resolved
Hide resolved
|
||
page-type: css-module | ||
spec-urls: https://drafts.csswg.org/css-highlight-api-1/ | ||
sidebar: cssref | ||
--- | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Custom highlight API in action | ||
chrisdavidmills marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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). | ||
|
||
```html-nolint hidden | ||
<h1>Directions</h1> | ||
<h2>Lincoln Memorial to Martin Luther King, Jr. Memorial</h2> | ||
<ol><li>Head south on Lincoln Memorial Circle</li | ||
><li>Turn right toward Independence Ave</li | ||
><li>Turn left onto Independence Ave</li | ||
><li>Turn right onto West Basin Dr</li | ||
><li>Look up when you reach 64 Independence Ave!</li> | ||
</ol> | ||
<hr /> | ||
<label | ||
>Number of steps completed: | ||
<input type="number" min="0" max="5" value="0" id="currentStep" /> | ||
</label> | ||
``` | ||
|
||
This example uses the {{cssxref("text-decoration")}} property to strike throgh the `steps` highlight range defined by our JavaScript: | ||
|
||
```css | ||
::highlight(steps) { | ||
text-decoration: line-through; | ||
color: blue; | ||
} | ||
``` | ||
|
||
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. | ||
|
||
```js | ||
const rangeToHighlight = new Range(); | ||
const list = document.querySelector("ol"); | ||
rangeToHighlight.setStart(list, 0); | ||
rangeToHighlight.setEnd(list, 0); | ||
|
||
CSS.highlights.set("steps", new Highlight(rangeToHighlight)); | ||
``` | ||
|
||
An event listener updates the end of the highlighted range when the number of completed steps changes: | ||
|
||
```js | ||
const currentPositionSlider = document.querySelector("input"); | ||
currentPositionSlider.addEventListener("change", (e) => { | ||
rangeToHighlight.setEnd(list, e.target.value); | ||
}); | ||
``` | ||
|
||
{{ EmbedLiveSample('Custom highlight API in action', 700, 300) }} | ||
chrisdavidmills marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Reference | ||
|
||
### Pseudo-elements | ||
|
||
- {{CSSXref("::highlight()")}} | ||
|
||
### Interfaces | ||
|
||
- {{domxref("Highlight")}} | ||
- {{domxref("HighlightRegistry")}} | ||
|
||
### Interface extensions | ||
|
||
This module adds properties and methods to interfaces defined in other specifications. | ||
|
||
- {{domxref("CSS")}} | ||
- {{domxref("CSS.highlights")}} | ||
|
||
## Guides | ||
|
||
- [CSS custom highlight API](/en-US/docs/Web/API/CSS_Custom_Highlight_API#concepts_and_usage) | ||
estelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- : 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. | ||
estelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Related concepts | ||
|
||
- {{CSSXref("::grammar-error")}} | ||
- {{CSSXref("::selection")}} | ||
- {{CSSXref("::spelling-error")}} | ||
- {{CSSXref("::target-text")}} | ||
- {{domxref("Range")}} Interface and {{domxref("Range.range", "Range()")}} constructor | ||
- [Text fragments](/en-US/docs/Web/URI/Reference/Fragment/Text_fragments) | ||
- {{domxref("FragmentDirective")}} Interface | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## See also | ||
|
||
- [CSS pseudo-element module](/en-US/docs/Web/CSS/CSS_pseudo-elements) | ||
- [CSS Object Model (CSSOM)](/en-US/docs/Web/API/CSS_Object_Model) APIs |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.