Namespace: Elements:findTextNodes

Elements:findTextNodes

Methods

findTextNodes(node, filteropt, nullable, onlyFirstLevelopt, nullable) → {Array.<Node>}

Extracts all pure text nodes from an Element, starting in the element itself.

Think of this function as a sort of find() where the result are not nodes, that query selectors can find, but pure text nodes. So you'll get a set of recursively discovered text nodes without tags, representing the pure text content of an element.

If you define to set onlyFirstLevel, you'll be able to retrieve all text on the first level of an element not included in any tag (paragraph contents without special formats as b/i/em/strong for example).

Parameters:
Name Type Attributes Default Description
node HTMLElement

the element to search for text nodes inside

filter function <optional>
<nullable>
null

a filter function to restrict the returned set, gets called with the textNode (you can access the parent via .parentNode)

onlyFirstLevel Boolean <optional>
<nullable>
false

defines if the function should only return text nodes from the very first level of children

Source:
Throws:

error if node is not a usable HTML element

Returns:
a list of text nodes
Type
Array.<Node>
Example
const styledSentence = createElement('<div>arigatou <p>gozaimasu <span>deshita</span></p> mr. roboto<p>!<span>!!</span></p></div>');
findTextNodes(styledSentence).length;
=> 6
findTextNodes(styledSentence, null, true).length;
=> 2
findTextNodes(styledSentence, textNode => textNode.textContent.length < 9).length;
=> 3
findTextNodes(styledSentence).map(node => node.textContent).join('');
	=> 'arigatou gozaimasu deshita mr. roboto!!!';