Lightweight DOM-like HTML node.
HtmlNode can represent an element node, text node, comment node, or another parsed DOM node type. Some operations, such as attributes and class checks, only make sense for element nodes.
NOTE: HtmlNode objects keep the underlying native document alive while they exist.
childNodes: Array<HtmlNode>
All child nodes, including element nodes, text nodes and comments.
Example
const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc.querySelector("p");
console.log(p.childNodes.length);
children: Array<HtmlNode>
Child element nodes only.
Text nodes and comments are skipped.
Example
const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc.querySelector("p");
console.log(p.children.length);
console.log(p.children[0].tagName);
firstChild: ?HtmlNode
First child node.
Important: this can be a text node or comment, not necessarily an element.
Whitespace-only text nodes are preserved. Use JSON.stringify(node.textContent) while debugging if you need to see line breaks, tabs, and spaces explicitly.
Use children[0] or querySelector when you need an element.
Example
const doc = utils.ParseHtml("<li>text before link <a href='https://example.com'>Link</a></li>");
const li = doc.querySelector("li");
console.log(JSON.stringify(li.firstChild.textContent));
const a = li.querySelector("a");
console.log(a.getAttribute("href"));
innerText: string
Alias of textContent.
Since this parser has no browser layout engine, innerText does not emulate CSS visibility, rendered line wrapping, or layout-dependent text extraction.
isElement: boolean
Whether this node is an element node.
Element nodes are tags such as <div>, <a>, <p>, <body>.
Text nodes and comments are not element nodes.
Example
const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc ? doc.querySelector("p") : null;
if (p) {
console.log(p.isElement);
console.log(p.firstChild ? p.firstChild.isElement : null);
}
textContent: string
Text content of this node and its descendants.
Whitespace is returned as it exists in parsed text nodes.
Example
const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
if (doc) {
const p = doc.querySelector("p");
console.log(p ? p.textContent : null);
}
getElementsByTagName(tagName)
Returns all descendant elements with the specified tag name.
Use "*" to return all descendant elements.
Parameters
| Name | Type | Description |
tagName | string | Tag name, for example "a", "div", "section" or "*". |
Returns
Array<HtmlNode> —
Array of matching nodes. Empty array if nothing matches.
Example
const doc = utils.ParseHtml("<p><a href='https://example.com'>One</a><a href='/two'>Two</a></p>");
const p = doc.querySelector("p");
const links = p.getElementsByTagName("a");
console.log(links.length);
hasClass(className)
Checks whether the element has a CSS class token.
This checks class tokens, not arbitrary substrings.
For example, hasClass("album") matches class="album featured", but not class="album-list".
For non-element nodes, returns false.
Parameters
| Name | Type | Description |
className | string | Class token to check. |
Example
const doc = utils.ParseHtml("<li class='album featured'>Title</li>");
const li = doc.querySelector("li");
console.log(li.hasClass("album"));
console.log(li.hasClass("featured"));
console.log(li.hasClass("album-list"));
querySelectorAll(selector)
Returns all descendant elements matching a CSS selector.
The returned value is a regular JavaScript array.
Parameters
| Name | Type | Description |
selector | string | CSS selector. |
Returns
Array<HtmlNode> —
Array of matching nodes. Empty array if nothing matches.
Example
const doc = utils.ParseHtml("<ul><li class='album'>A</li><li class='album featured'>B</li></ul>");
const list = doc.querySelector("ul");
const albums = list.querySelectorAll(".album");
console.log(albums.length);