3.0.1 • Published 4 years ago

dom-highlight-range v3.0.1

Weekly downloads
6
License
CC0-1.0
Repository
github
Last release
4 years ago

dom-highlight-range

Wrap each text node in a given DOM Range with a <mark> element. Breaks start and/or end node if needed. Returns a function that cleans up the created highlight (not a perfect undo: split text nodes are not merged again).

Parameters:

  • range: a DOM Range object. Note that as highlighting modifies the DOM, the range may be unusable afterwards.
  • tagName: the element used to wrap text nodes. Defaults to 'mark'.
  • attributes: an Object defining any attributes to be set on the wrapper elements.

Example usage

import highlightRange from 'dom-highlight-range';

// Highlight the text currently selected by the user, if any.
const selection = window.getSelection();
if (!selection.isCollapsed) {
        const range = selection.getRangeAt(0);
        const removeHighlights = highlightRange(range, 'span', { class: 'some-CSS-class' });
        // Running removeHighlights() would remove the highlight again.
}
3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

5 years ago

1.0.1

8 years ago

1.0.0

8 years ago