Class: TextHighlighter

TextHighlighter

new TextHighlighter(element, options)

Creates TextHighlighter instance and binds to given DOM elements.

Parameters:
Name Type Argument Description
element HTMLElement

DOM element to which highlighted will be applied.

options object <optional>

additional options.

Properties
Name Type Description
color string

highlight color.

highlightedClass string

class added to highlight, 'highlighted' by default.

contextClass string

class added to element to which highlighter is applied, 'highlighter-context' by default.

onRemoveHighlight function

function called before highlight is removed. Highlight is passed as param. Function should return true if highlight should be removed, or false - to prevent removal.

onBeforeHighlight function

function called before highlight is created. Range object is passed as param. Function should return true to continue processing, or false - to prevent highlighting.

onAfterHighlight function

function called after highlight is created. Array of created wrappers is passed as param.

Source:

Methods

<static> createWrapper(options) → {HTMLElement}

Creates wrapper for highlights. TextHighlighter instance calls this method each time it needs to create highlights and pass options retrieved in constructor.

Parameters:
Name Type Description
options object

the same object as in TextHighlighter constructor.

Source:
Returns:
Type
HTMLElement

deserializeHighlights(json) → {Array}

Deserializes highlights.

Parameters:
Name Type Description
json object

JSON object with highlights definition.

Source:
Throws:
exception when can't parse JSON or JSON has invalid structure.
Returns:
  • array of deserialized highlights.
Type
Array

destroy()

Permanently disables highlighting. Unbinds events and remove context element class.

Source:

doHighlight(keepRange)

Highlights current range.

Parameters:
Name Type Description
keepRange boolean

Don't remove range after highlighting. Default: false.

Source:

find(text, caseSensitive)

Finds and highlights given text.

Parameters:
Name Type Argument Description
text string

text to search for

caseSensitive boolean <optional>

if set to true, performs case sensitive search (default: true)

Source:

flattenNestedHighlights(highlights)

Flattens highlights structure. Note: this method changes input highlights - their order and number after calling this method may change.

Parameters:
Name Type Description
highlights Array

highlights to flatten.

Source:

getColor() → {string}

Returns highlighting color.

Source:
Returns:
Type
string

getHighlights(params) → {Array}

Returns highlights from given container.

Parameters:
Name Type Description
params
Properties
Name Type Argument Description
container HTMLElement <optional>

return highlights from this element. Default: the element the highlighter is applied to.

andSelf boolean <optional>

if set to true and container is a highlight itself, add container to returned results. Default: true.

grouped boolean <optional>

if set to true, highlights are grouped in logical groups of highlights added in the same moment. Each group is an object which has got array of highlights, 'toString' method and 'timestamp' property. Default: false.

Source:
Returns:
  • array of highlights.
Type
Array

highlightRange(range, wrapper) → {Array}

Highlights range. Wraps text of given range object in wrapper element.

Parameters:
Name Type Description
range Range
wrapper HTMLElement
Source:
Returns:
  • array of created highlights.
Type
Array

isHighlight(el) → {boolean}

Returns true if element is a highlight. All highlights have 'data-highlighted' attribute.

Parameters:
Name Type Description
el

element to check.

Source:
Returns:
Type
boolean

mergeSiblingHighlights(highlights)

Merges sibling highlights and normalizes descendant text nodes. Note: this method changes input highlights - their order and number after calling this method may change.

Parameters:
Name Type Description
highlights
Source:

normalizeHighlights(highlights) → {Array}

Normalizes highlights. Ensures that highlighting is done with use of the smallest possible number of wrapping HTML elements. Flattens highlights structure and merges sibling highlights. Normalizes text nodes within highlights.

Parameters:
Name Type Description
highlights Array

highlights to normalize.

Source:
Returns:
  • array of normalized highlights. Order and number of returned highlights may be different than input highlights.
Type
Array

removeHighlights(element)

Removes highlights from element. If element is a highlight itself, it is removed as well. If no element is given, all highlights all removed.

Parameters:
Name Type Argument Description
element HTMLElement <optional>

element to remove highlights from

Source:

serializeHighlights() → {string}

Serializes all highlights in the element the highlighter is applied to.

Source:
Returns:
  • stringified JSON with highlights definition
Type
string

setColor(color)

Sets highlighting color.

Parameters:
Name Type Description
color string

valid CSS color.

Source: