Highlight text in the DOM by specifying character offset and length ranges, ideal for backend search results, index-based highlighting, and line ranges.
Use this file to discover all available pages before exploring further.
markRanges() highlights text by specifying character start positions and lengths within the context’s text content. It is especially useful for highlighting search results from a backend or full-text index, where you already know the exact byte or character offsets of matches. It also supports a line-range mode via the markLines option for highlighting entire lines by number.
start (number) — zero-based character offset into the context’s concatenated text content where the highlight begins. When markLines is true, this becomes a one-based line number instead.
length (number) — number of characters to highlight. When markLines is true, this is the number of lines to highlight starting from start.
Start positions are counted across the full text content of the context, including whitespace. Ranges that are out of bounds or cover only whitespace are reported via the noMatch callback.
A CSS selector string or array of selectors for DOM elements that should be excluded from the marking operation. Descendants of excluded elements are also skipped.
When true, switches to line-range mode: the start property of each range object is interpreted as a one-based line number (minimum 1), and length is the number of consecutive lines to highlight. Line boundaries are determined by newlines and <br> elements, which are handled correctly. Useful for highlighting specific lines in a <pre> block or any element that uses <br> for line formatting.
When using the Highlight API, controls whether a single range spanning element boundaries is created (true) or separate per-node ranges are created (false).
When true or an options object, marks inside shadow DOM roots attached to elements within the context. Pass an object with a style property to inject CSS into shadow roots.
nodeOrArray (Text | Text[]) — the text node that contains the range or is part of it. An array of nodes when the Highlight API is used with rangeAcrossElements.
range (object) — the current { start, length } range object from your input array.
matchString (string) — the text content matched by this range.
index (number) — the zero-based index of the current range in the input array. Note: not fully reliable if a range is skipped due to whitespace-only content.
The function must return true to highlight or false to skip.
A callback invoked after each mark element is created or range is registered. If a range spans multiple elements, it is called once per created element.
each: (elementOrRange, range, rangeInfo) => {}
Parameters:
elementOrRange (HTMLElement | StaticRange | Range) — the created mark element or Highlight API range object.
range (object) — the { start, length } range object that produced this element.
rangeInfo (object):
matchStart (boolean) — true on the first element created for this range (useful when a range spans multiple elements).
count (number) — the total number of ranges highlighted so far.
A callback invoked for each range that could not be highlighted — for example because the range is out of bounds, has invalid values, or covers only whitespace.
noMatch: (range) => {}
Parameters:
range (object) — the { start, length } range object that failed to match.
Character positions in start include all whitespace characters (spaces, newlines, tabs). When computing offsets from backend results, make sure your indexing matches the browser’s textContent representation of the context element.
Use the markLines option together with a <pre> or <code> element to highlight entire lines of code by line number without needing to compute character offsets yourself.