Documentation/HtmlDocument
classWORKER

HtmlDocument

jsdoc/objects/html.js:1

Object returned by utils.ParseHtml

Lightweight DOM-like HTML document backed by the native HTML parser.

Notes:
- This is not a full browser DOM.
- Scripts, CSS, layout, external resources and browser events are not processed.
- innerText is currently an alias of textContent.

propertyWORKERreadonly

body

jsdoc/objects/html.js:55
body: ?HtmlNode

The document <body> element.

Example

let doc = utils.ParseHtml("<html><body><p>Hello</p></body></html>");
if (doc && doc.body) console.log(doc.body.innerHTML); // "<p>Hello</p>"
propertyWORKERreadonly

documentElement

jsdoc/objects/html.js:29
documentElement: ?HtmlNode

Root document element, usually the <html> element.

Example

let doc = utils.ParseHtml("<html><body>Hello</body></html>");
if (doc) console.log(doc.documentElement.tagName); // "html"
propertyWORKERreadonly

innerHTML

jsdoc/objects/html.js:93
innerHTML: string

Serialized HTML markup inside the document body.
If the document has no body, this falls back to the root element.

Example

let doc = utils.ParseHtml("<html><body><p>Hello <b>world</b></p></body></html>");
if (doc) console.log(doc.innerHTML); // "<p>Hello <b>world</b></p>"
propertyWORKERreadonly

innerText

jsdoc/objects/html.js:83
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.

propertyWORKERreadonly

outerHTML

jsdoc/objects/html.js:107
outerHTML: string

Serialized outer HTML of the root element.

Example

let doc = utils.ParseHtml("<html><body><p>Hello</p></body></html>");
if (doc) console.log(doc.outerHTML); // "<html><head></head><body><p>Hello</p></body></html>"
propertyWORKERreadonly

root

jsdoc/objects/html.js:15
root: ?HtmlNode

Root document element, usually the <html> element.
Alias of documentElement.

Example

let doc = utils.ParseHtml("<html><body>Hello</body></html>");
if (doc) console.log(doc.root.tagName); // "html"
propertyWORKERreadonly

textContent

jsdoc/objects/html.js:68
textContent: string

Text content of the document body.
If the document has no body, this falls back to the root element text.
Whitespace is returned as it exists in the parsed text nodes.

Example

let doc = utils.ParseHtml("<html><body><p>Hello <b>world</b></p></body></html>");
if (doc) console.log(doc.textContent); // "Hello world"
methodWORKER

getElementsByTagName

jsdoc/objects/html.js:151
getElementsByTagName(tagName)

Returns all descendant elements with the specified tag name.
Use "*" to return all descendant elements.

Parameters

NameTypeDescription
tagNamestring

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'>Link</a></p>");
const links = doc ? doc.getElementsByTagName("a") : [];
console.log(links.length); // 1
methodWORKER

querySelector

jsdoc/objects/html.js:120
querySelector(selector)

Returns the first element matching a CSS selector.

Parameters

NameTypeDescription
selectorstring

CSS selector.

Returns

?HtmlNode —

First matching node, or null if nothing matches.

Example

let doc = utils.ParseHtml("<html><body><p class='name'>Test Artist</p></body></html>");
let node = doc ? doc.querySelector(".name") : null;
if (node) console.log(node.textContent); // "Test Artist"
methodWORKER

querySelectorAll

jsdoc/objects/html.js:135
querySelectorAll(selector)

Returns all elements matching a CSS selector.
The returned value is a regular JavaScript array.

Parameters

NameTypeDescription
selectorstring

CSS selector.

Returns

Array<HtmlNode> —

Array of matching nodes. Empty array if nothing matches.

Example

let doc = utils.ParseHtml("<ul><li class='album'>A</li><li class='album'>B</li></ul>");
let albums = doc ? doc.querySelectorAll(".album") : [];
console.log(albums.length); // 2