| title | HighlightRegistry |
|---|---|
| slug | Web/API/HighlightRegistry |
| page-type | web-api-interface |
| browser-compat | api.HighlightRegistry |
{{APIRef("CSS Custom Highlight API")}}
The HighlightRegistry interface of the CSS Custom Highlight API is used to register {{domxref("Highlight")}} objects to be styled using the API.
It is accessed via {{domxref("CSS.highlights_static", "CSS.highlights")}}.
A HighlightRegistry instance is a Map-like object, in which each key is the name string for a custom highlight, and the corresponding value is the associated {{domxref("Highlight")}} object.
{{InheritanceDiagram}}
The HighlightRegistry interface doesn't inherit any properties.
- {{domxref("HighlightRegistry.size")}} {{ReadOnlyInline}}
- : Returns the number of
Highlightobjects currently registered.
- : Returns the number of
The HighlightRegistry interface doesn't inherit any methods.
- {{domxref("HighlightRegistry.clear()")}}
- : Remove all
Highlightobjects from the registry.
- : Remove all
- {{domxref("HighlightRegistry.delete()")}}
- : Remove the named
Highlightobject from the registry.
- : Remove the named
- {{domxref("HighlightRegistry.entries()")}}
- : Returns a new iterator object that contains each
Highlightobject in the registry, in insertion order.
- : Returns a new iterator object that contains each
- {{domxref("HighlightRegistry.forEach()")}}
- : Calls the given callback once for each
Highlightobject in the registry, in insertion order.
- : Calls the given callback once for each
- {{domxref("HighlightRegistry.get()")}}
- : Gets the named
Highlightobject from the registry.
- : Gets the named
- {{domxref("HighlightRegistry.has()")}}
- : Returns a boolean asserting whether a
Highlightobject is present the registry or not.
- : Returns a boolean asserting whether a
- {{domxref("HighlightRegistry.highlightsFromPoint()")}}
- : Returns an array of objects representing the custom highlights applied at a specific point within the viewport.
- {{domxref("HighlightRegistry.keys()")}}
- : An alias for {{domxref("HighlightRegistry.values()")}}.
- {{domxref("HighlightRegistry.set()")}}
- : Adds the given
Highlightobject to the registry with the given name, or updates the namedHighlightobject, if it already exists in the registry.
- : Adds the given
- {{domxref("HighlightRegistry.values()")}}
- : Returns a new iterator object that yields the
Highlightobjects in the registry, in insertion order.
- : Returns a new iterator object that yields the
The following example demonstrates how to create ranges, instantiate a new Highlight object for them, and register the highlight using the HighlightRegistry, to style it on the page:
<p id="foo">CSS Custom Highlight API</p>::highlight(my-custom-highlight) {
background-color: peachpuff;
}const text = document.getElementById("foo").firstChild;
if (!CSS.highlights) {
text.textContent =
"The CSS Custom Highlight API is not supported in this browser!";
}
// Create a couple of ranges.
const range1 = new Range();
range1.setStart(text, 0);
range1.setEnd(text, 3);
const range2 = new Range();
range2.setStart(text, 21);
range2.setEnd(text, 24);
// Create a custom highlight for these ranges.
const highlight = new Highlight(range1, range2);
// Register the ranges in the HighlightRegistry.
CSS.highlights.set("my-custom-highlight", highlight);{{ EmbedLiveSample("Registering a highlight") }}
{{Specifications}}
{{Compat}}
- {{domxref("css_custom_highlight_api", "The CSS Custom Highlight API", "", "nocode")}}
- CSS custom highlight API module
- CSS Custom Highlight API: The Future of Highlighting Text Ranges on the Web