dreamworkAPI
Ctrl K
GitHub
classDocs / HTMLNode

HTMLNode

classsharedmenusource
Object → HTMLNode

A HTML node object, representing an element, or the document itself with name #document.

Unlike XMLNode, children are kept as an ordered list of strings and nodes, like in the browser DOM.

Usage:

lua
local document = HTMLNode.fromString( [[<ul id="list"><li>One<li class="active">Two</ul>]] )

print( document:querySelector( "#list > li.active" ):getText() ) -- Two

Constructor

HTMLNode(name: string, attributes: ( table<string, string> | nil ), parent: ( HTMLNode | nil )): HTMLNode

Fields

NameTypeDescription
namestringThe tag name of the node, lowercase for HTML elements and #document for documents.
attributesnil | table<string, string>The attributes of the node, boolean attributes have an empty value.
parentHTMLNode | nilThe parent node.
childrennil | HTMLNode.Value[]The child nodes and text chunks in document order.
doctypenil | stringThe DOCTYPE of a document, e.g. html.

Static functions

createDocumentCreates an empty HTML5 document with html, head (with UTF-8 charset and optional title) and body elements.
fromStringParses a HTML string into a document node.

#HTMLNode.createDocument

sharedmenunodiscardsource
function HTMLNode.createDocument(title?: string): HTMLNode

Creates an empty HTML5 document with html, head (with UTF-8 charset and optional title) and body elements.

Parameters
NameTypeDescription
titleoptionalstringThe title of the document.
Returns
TypeNameDescription
HTMLNodedocumentThe document node.

#HTMLNode.fromString

sharedmenunodiscardsource
function HTMLNode.fromString(html_str: string, keep_whitespace?: boolean): HTMLNode

Parses a HTML string into a document node.

The parser is lenient like a browser: it never fails, tag and attribute names are case-insensitive, attribute values may be unquoted or missing, void elements (br, img, ...) have no end tag, optional end tags (p, li, td, ...) are closed implicitly, stray end tags are ignored and script/style content is kept as raw text. Missing html, head and body elements are not created.

Inside svg and math names keep their case and /> closes the element.

By default whitespace is collapsed like a browser renders it and indentation is removed, pre, textarea, script and style keep it as is.

Comments and processing instructions are skipped, HTML 4 named and numeric character references are decoded.

Parameters
NameTypeDescription
html_strstringThe HTML string to parse.
keep_whitespaceoptionalbooleanWhether to keep all whitespace in text, defaults to false.
Returns
TypeNameDescription
HTMLNodedocumentThe document node, with the elements as children.

Methods

addCreates a new element and appends it to the children of the node.
addClassAdds the class to the class attribute of the node.
appendAppends nodes or text to the end of the children, nodes are detached from their old parent first.
clearRemoves all children of the node.
closestReturns the closest element matching the CSS selector, starting with the node itself and going up through its ancestors.
detachDetaches the node from its parent.
getAttributeReturns the value of the attribute with the given name or nil if not found.
getChildReturns the child (node or text chunk) at the given index, negative indices count from the end.
getChildCountReturns the number of children (nodes and text chunks) of the node.
getElementByIdReturns the first descendant element with the given id, or nil if not found.
getElementsReturns the child elements of the node, skipping text.
getElementsByClassNameReturns all descendant elements that have all of the given whitespace-separated classes.
getElementsByTagNameReturns all descendant elements with the given tag name, * matches all elements.
getFirstElementReturns the first child element of the node, skipping text.
getInnerHTMLReturns the HTML string representation of the children of this node, like innerHTML in the browser DOM.
getTextReturns the text content of the node and all its descendants, like textContent in the browser DOM.
hasAttributeChecks whether the node has the attribute with the given name.
hasClassChecks whether the node has the given class in its class attribute.
insertInserts a node or text at the given index, nodes are detached from their old parent first.
isDocumentChecks whether the node is a document.
isEmptyChecks whether the node has no children.
isVoidChecks whether the node is a void element, which has no end tag and no children, like br or img.
iteratorReturns an iterator over the children of the node in document order, values are strings or nodes.
matchesChecks whether the node matches the CSS selector, see :querySelector() for the supported syntax.
querySelectorReturns the first descendant element matching the CSS selector, or nil if not found.
querySelectorAllReturns all descendant elements matching the CSS selector in document order, see :querySelector() for the supported syn…
removeRemoves the child at the given index or the given child node.
removeAttributeRemoves the attribute with the given name.
removeClassRemoves the class from the class attribute of the node, the attribute is removed when no classes are left.
setAttributeSets the value of the attribute with the given name, true sets a boolean attribute, nil or false removes it.
setTextReplaces all children of the node with the given text.
toStringReturns the HTML string representation of this node, like outerHTML in the browser DOM, documents include their DOCTYPE.

#HTMLNode:add

sharedmenusource
function HTMLNode:add(name: string, attributes?: table<string, string>): HTMLNode

Creates a new element and appends it to the children of the node.

Parameters
NameTypeDescription
namestringThe tag name of the new element.
attributesoptionaltable<string, string>The attributes of the new element.
Returns
TypeNameDescription
HTMLNodechildThe new element.

#HTMLNode:addClass

sharedmenusource
function HTMLNode:addClass(name: string): HTMLNode

Adds the class to the class attribute of the node.

Parameters
NameTypeDescription
namestringThe class name.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:append

sharedmenusource
function HTMLNode:append(...: HTMLNode.Value | any): HTMLNode

Appends nodes or text to the end of the children, nodes are detached from their old parent first.

Usage:

lua
local paragraph = HTMLNode( "p" ):append( "Hello ", HTMLNode( "b" ):append( "world" ), "!" )
print( paragraph ) -- <p>Hello <b>world</b>!</p>
Parameters
NameTypeDescription
...HTMLNode.Value | anyThe nodes or text to append, other values are converted to strings.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:clear

sharedmenusource
function HTMLNode:clear(): HTMLNode

Removes all children of the node.

Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:closest

sharedmenunodiscardsource
function HTMLNode:closest(selector: string): HTMLNode | nil

Returns the closest element matching the CSS selector, starting with the node itself and going up through its ancestors.

Parameters
NameTypeDescription
selectorstringThe CSS selector.
Returns
TypeNameDescription
HTMLNode | nilelement

#HTMLNode:detach

sharedmenusource
function HTMLNode:detach(): HTMLNode

Detaches the node from its parent.

Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:getAttribute

sharedmenunodiscardsource
function HTMLNode:getAttribute(name: string): nil | string

Returns the value of the attribute with the given name or nil if not found.

Parameters
NameTypeDescription
namestringThe name of the attribute to get.
Returns
TypeNameDescription
nil | stringvalueThe value of the attribute or nil if not found.

#HTMLNode:getChild

sharedmenunodiscardsource
function HTMLNode:getChild(index: integer): nil | HTMLNode.Value

Returns the child (node or text chunk) at the given index, negative indices count from the end.

Parameters
NameTypeDescription
indexintegerThe index of the child.
Returns
TypeNameDescription
nil | HTMLNode.Valuechild

#HTMLNode:getChildCount

sharedmenunodiscardsource
function HTMLNode:getChildCount(): integer

Returns the number of children (nodes and text chunks) of the node.

Returns
TypeNameDescription
integercount

#HTMLNode:getElementById

sharedmenunodiscardsource
function HTMLNode:getElementById(id: string): HTMLNode | nil

Returns the first descendant element with the given id, or nil if not found.

Parameters
NameTypeDescription
idstringThe id of the element.
Returns
TypeNameDescription
HTMLNode | nilelement

#HTMLNode:getElements

sharedmenunodiscardsource
function HTMLNode:getElements(): HTMLNode[], integer

Returns the child elements of the node, skipping text.

Returns
TypeNameDescription
HTMLNode[]elements
integercount

#HTMLNode:getElementsByClassName

sharedmenunodiscardsource
function HTMLNode:getElementsByClassName(names: string): HTMLNode[], integer

Returns all descendant elements that have all of the given whitespace-separated classes.

Parameters
NameTypeDescription
namesstringThe class names, separated by whitespace.
Returns
TypeNameDescription
HTMLNode[]elements
integercount

#HTMLNode:getElementsByTagName

sharedmenunodiscardsource
function HTMLNode:getElementsByTagName(name: string): HTMLNode[], integer

Returns all descendant elements with the given tag name, * matches all elements.

Parameters
NameTypeDescription
namestringThe tag name, case-insensitive.
Returns
TypeNameDescription
HTMLNode[]elements
integercount

#HTMLNode:getFirstElement

sharedmenunodiscardsource
function HTMLNode:getFirstElement(): HTMLNode | nil

Returns the first child element of the node, skipping text.

Returns
TypeNameDescription
HTMLNode | nilelement

#HTMLNode:getInnerHTML

sharedmenunodiscardsource
function HTMLNode:getInnerHTML(): string

Returns the HTML string representation of the children of this node, like innerHTML in the browser DOM.

Returns
TypeNameDescription
stringstrThe HTML string representation of the children.

#HTMLNode:getText

sharedmenunodiscardsource
function HTMLNode:getText(): string

Returns the text content of the node and all its descendants, like textContent in the browser DOM.

Returns
TypeNameDescription
stringtext

#HTMLNode:hasAttribute

sharedmenunodiscardsource
function HTMLNode:hasAttribute(name: string): boolean

Checks whether the node has the attribute with the given name.

Parameters
NameTypeDescription
namestringThe name of the attribute.
Returns
TypeNameDescription
booleanhas_attribute

#HTMLNode:hasClass

sharedmenunodiscardsource
function HTMLNode:hasClass(name: string): boolean

Checks whether the node has the given class in its class attribute.

Parameters
NameTypeDescription
namestringThe class name.
Returns
TypeNameDescription
booleanhas_class

#HTMLNode:insert

sharedmenusource
function HTMLNode:insert(index: integer, value: HTMLNode.Value | any): HTMLNode

Inserts a node or text at the given index, nodes are detached from their old parent first.

Parameters
NameTypeDescription
indexintegerThe index to insert at, clamped to the children range, negative indices count from the end.
valueHTMLNode.Value | anyThe node or text to insert, other values are converted to strings.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:isDocument

sharedmenunodiscardsource
function HTMLNode:isDocument(): boolean

Checks whether the node is a document.

Returns
TypeNameDescription
booleanis_document

#HTMLNode:isEmpty

sharedmenunodiscardsource
function HTMLNode:isEmpty(): boolean

Checks whether the node has no children.

Returns
TypeNameDescription
booleanis_empty

#HTMLNode:isVoid

sharedmenunodiscardsource
function HTMLNode:isVoid(): boolean

Checks whether the node is a void element, which has no end tag and no children, like br or img.

Returns
TypeNameDescription
booleanis_void

#HTMLNode:iterator

sharedmenunodiscardsource
function HTMLNode:iterator(): fun(): ( ( integer | nil ), HTMLNode.Value )

Returns an iterator over the children of the node in document order, values are strings or nodes.

Returns
TypeNameDescription
fun(): ( ( integer | nil ), HTMLNode.Value )fnThe iterator function.

#HTMLNode:matches

sharedmenunodiscardsource
function HTMLNode:matches(selector: string): boolean

Checks whether the node matches the CSS selector, see :querySelector() for the supported syntax.

Parameters
NameTypeDescription
selectorstringThe CSS selector.
Returns
TypeNameDescription
booleanis_matching

#HTMLNode:querySelector

sharedmenunodiscardsource
function HTMLNode:querySelector(selector: string): HTMLNode | nil

Returns the first descendant element matching the CSS selector, or nil if not found.

Supported: type, *, #id, .class, attributes [a], [a=v], [a~=v], [a|=v], [a^=v], [a$=v], [a*=v] with optional i flag, combinators , >, +, ~, selector lists ,, pseudo-classes :first-child, :last-child, :only-child, :nth-child(), :nth-last-child(), the -of-type variants, :empty, :root and :not().

Parameters
NameTypeDescription
selectorstringThe CSS selector.
Returns
TypeNameDescription
HTMLNode | nilelement

#HTMLNode:querySelectorAll

sharedmenunodiscardsource
function HTMLNode:querySelectorAll(selector: string): HTMLNode[], integer

Returns all descendant elements matching the CSS selector in document order, see :querySelector() for the supported syntax.

Parameters
NameTypeDescription
selectorstringThe CSS selector.
Returns
TypeNameDescription
HTMLNode[]elements
integercount

#HTMLNode:remove

sharedmenusource
function HTMLNode:remove(child: integer | HTMLNode): nil | HTMLNode.Value

Removes the child at the given index or the given child node.

Parameters
NameTypeDescription
childinteger | HTMLNodeThe index of the child or the child node.
Returns
TypeNameDescription
nil | HTMLNode.ValueremovedThe removed child, or nil if not found.

#HTMLNode:removeAttribute

sharedmenusource
function HTMLNode:removeAttribute(name: string): HTMLNode

Removes the attribute with the given name.

Parameters
NameTypeDescription
namestringThe name of the attribute to remove.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:removeClass

sharedmenusource
function HTMLNode:removeClass(name: string): HTMLNode

Removes the class from the class attribute of the node, the attribute is removed when no classes are left.

Parameters
NameTypeDescription
namestringThe class name.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:setAttribute

sharedmenusource
function HTMLNode:setAttribute(name: string, value: any): HTMLNode

Sets the value of the attribute with the given name, true sets a boolean attribute, nil or false removes it.

Parameters
NameTypeDescription
namestringThe name of the attribute to set.
valueanyThe value of the attribute.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:setText

sharedmenusource
function HTMLNode:setText(text: string | any): HTMLNode

Replaces all children of the node with the given text.

Parameters
NameTypeDescription
textstring | anyThe text, other values are converted to strings.
Returns
TypeNameDescription
HTMLNodeselfThe node itself, for chaining.

#HTMLNode:toString

sharedmenunodiscardsource
function HTMLNode:toString(pretty?: boolean, level?: integer): string

Returns the HTML string representation of this node, like outerHTML in the browser DOM, documents include their DOCTYPE.

Pretty formatting puts block elements on separate lines and trims the text next to them, so use compact output for exact data.

Parameters
NameTypeDescription
prettyoptionalbooleanWhether to use pretty formatting (indentation and newlines).
leveloptionalintegerThe indentation level.
Returns
TypeNameDescription
stringstrThe HTML string representation of this node.
Generated from the dreamwork meta branch.