Expand all

HtmlUtils

Helper class that does various HTML-to-text transformations

Constructor

new HtmlUtils() #

Methods

allowlistHtml($el, allowlist)static #

Discards all nodes which do not match the allowlist, but keeps the text and allowlisted nodes inside them. Works in-place.

Parameters:

Name Type Description
$el jQuery
allowlist string

a jQuery selector string such as 'a, span, br'

Source:

Discards all nodes which do not match the allowlist, but keeps the text and allowlisted nodes inside them.

appendWhitespaceToBlockElements($el)static #

Adds a whitespace to block elements. This is useful if you want to convert the contents to text and don't want words that are visually separate (e.g. table cells) to be fused. Works in-place.

Parameters:

Name Type Description
$el jQuery
Source:
Adds a whitespace to block elements.

filterInvisible($jq)static #

Filters display:none and children of a node. The root element is never filtered, and generally ignored (i.e. whether the root element is visible won't affect the filtering). Works in place.

Parameters:

Name Type Description
$jq jQuery
Source:
Filters display:none and children of a node.

htmlToText(html) → {string}static #

Returns the text content of a html string. Tries to give an approximation of what would be visible if the HTML would be displayed.

Parameters:

Name Type Description
html string

Returns:

Type
string
Source:
Returns the text content of a html string.
Returns the text content of a html string, with the <a> tags left intact.

htmlToTextWithTags(html) → {string}static #

Returns the text content of a html string, with the <a>, <i>, <b> tags left intact. Tries to give an approximation of what would be visible if the HTML would be displayed.

Parameters:

Name Type Description
html string

Returns:

Type
string
Source:
Returns the text content of a html string, with the <a>, <i>, <b> tags left intact.

isJQueryOrHTMLElement(html) → {boolean}static #

Returns true of the object is a jQuery object or an HTMLElement, false otherwise

Parameters:

Name Type Description
html string | HTMLElement | jQuery

Returns:

Type
boolean
Source:
Returns true of the object is a jQuery object or an HTMLElement, false otherwise

jqueryToHtml($el) → {string}static #

Returns the HTML code for a jQuery element (only the first one if passed a set of elements). Unlike .html(), this includes HTML code for the outermost element; compare

  • $('<div>').html() // ''
  • HtmlUtils.jqueryToHtml( $('<div>') ) // '<div></div>'

Parameters:

Name Type Description
$el jQuery

Returns:

Type
string
Source:
Returns the HTML code for a jQuery element (only the first one if passed a set of elements).

makeLinkText(text, props) → {string}static #

Generates HTML code for a link.

Parameters:

Name Type Description
text string

Link text (plain text; will be sanitized)

props Object

Link attributes (should at a minimum include href; will be sanitized)

Returns:

Type
string
Source:
Generates HTML code for a link.

mergeWhitespace(html) → {string}protectedstatic #

Cleans up superfluous whitespace. Given that the results will be displayed in a HTML environment, this doesn't have any real effect. It is mostly there to make testing easier.

Parameters:

Name Type Description
html string

a HTML (or plaintext) string

Returns:

Type
string
Source:
Cleans up superfluous whitespace.

wrapAndJquerify(html) → {jQuery}static #

Returns a jQuery node which contains the given HTML (wrapped into a <div> - this is necessary since an arbitrary HTML string might not have a jQuery representation).

Parameters:

Name Type Description
html string | HTMLElement | jQuery

Returns:

Type
jQuery
Source:

Returns a jQuery node which contains the given HTML (wrapped into a <div> - this is necessary since an arbitrary HTML string might not have a jQuery representation).