A reference to an item in the DOM tree.
While query methods like Document::querySelector() find only elements, Node represents any item in the document hierarchy.
Use Node to walk tree relationships when your application needs to reach content that element queries skip.
Consider an element containing both text and markup:
<p id="greeting">Howdy <b>world</b>!</p>
You can step from the element to its first child and narrow the next sibling to an element:
dom::Element greeting = document.getElementById(
"greeting");
A handle to an element on a page.
Definition Element.h:142
std::string tagName() const
Get the element's tag name (tagName).
Definition Element.h:1222
A reference to an item in the DOM tree.
Definition Node.h:147
Node nextSibling() const
Get the node's next sibling of any kind (nextSibling).
Definition Node.h:303
Node firstChild() const
Get the node's first child of any kind (firstChild).
Definition Node.h:280
Element AsElement() const
Get this node as an Element.
Definition Element.h:3452
std::string nodeName() const
Get the node's name (nodeName).
Definition Node.h:254
Kinds of Nodes
Both dom::Element and dom::DocumentFragment are Nodes. Unlike on the web, dom::Document is a separate type and doesn't inherit from Node (call ownerDocument() on any node to reach its document).
nodeType() returns the kind of node, and nodeName() returns its name. Call AsElement() to narrow a node to a dom::Element (it returns an empty handle for text and comments).
Traversal methods differ in which node kinds they visit:
Like other DOM handles, a Node is always Valid, Empty, or Gone (see dom::Element).
Text and Comment Nodes
You can create standalone text and comment nodes on the document using Document::createTextNode() and Document::createComment(). Passing strings to insertion methods like Element::append(), Element::prepend(), or before() creates text nodes automatically (strings are never parsed as markup).
Assigning to nodeValue updates a text node in place, leaving sibling nodes untouched (unlike assigning to textContent on the parent):
dom::Node points = document.createTextNode(
"0");
scoreboard.
append(
"Score: ", points);
void append(T &&... nodes) const
Add nodes and strings to the end of this element, after its last child (append).
Definition Element.h:768
detail::NodeStringProp< detail::NodeValueTag > nodeValue
The text of a text or comment node (nodeValue).
Definition Node.h:154
- Note
- Whitespace between HTML tags becomes a text node (firstElementChild() skips it).
- See also
- dom::Element, dom::DocumentFragment, dom::NodeList, dom::Document::createTextNode(), dom::Document::createComment()
|
| | Node () |
| | Create an empty Node.
|
| | Node (const Node &other) |
| | Copy constructor (both handles refer to the same node).
|
| | Node (Node &&other) noexcept |
| | Move constructor (other becomes empty).
|
| Node & | operator= (Node other) noexcept |
| | Assignment (copies or moves).
|
| | ~Node () |
| | Destroy this handle (the node itself isn't affected).
|
| | operator bool () const |
| | Whether or not this Node is valid (see IsAlive()).
|
| bool | IsEmpty () const |
| | Whether or not this Node is empty (it holds no handle).
|
| bool | IsAlive () const |
| | Whether or not this Node is valid (it isn't empty and its page is still alive).
|
| bool | IsSame (const Node &other) const |
| | Whether or not two handles refer to the same node (isSameNode).
|
| NodeType | nodeType () const |
| | Get the kind of node (nodeType).
|
| std::string | nodeName () const |
| | Get the node's name (nodeName).
|
| Node | parentNode () const |
| | Get the node's parent (parentNode).
|
| Element | parentElement () const |
| | Get the node's parent element (parentElement).
|
| Node | firstChild () const |
| | Get the node's first child of any kind (firstChild).
|
| Node | lastChild () const |
| | Get the node's last child of any kind (lastChild).
|
| Node | previousSibling () const |
| | Get the node's previous sibling of any kind (previousSibling).
|
| Node | nextSibling () const |
| | Get the node's next sibling of any kind (nextSibling).
|
| NodeList | childNodes () const |
| | Get the node's children of every kind in order (childNodes).
|
| bool | hasChildNodes () const |
| | Whether or not the node has any children (hasChildNodes).
|
| bool | isConnected () const |
| | Whether or not the node is in its document (isConnected).
|
| Document | ownerDocument () const |
| | Get the document this node belongs to (ownerDocument).
|
| Element | AsElement () const |
| | Get this node as an Element.
|
| DocumentFragment | AsFragment () const |
| | Get this node as a DocumentFragment.
|
template<typename... T>
requires ((detail::NodeOrString<T> && ...)) |
| void | before (T &&... nodes) const |
| | Insert nodes and strings just before this node, in its parent (before).
|
template<typename... T>
requires ((detail::NodeOrString<T> && ...)) |
| Result< void > | before (Checked_t, T &&... nodes) const |
| | Same as before(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
|
template<typename... T>
requires ((detail::NodeOrString<T> && ...)) |
| void | after (T &&... nodes) const |
| | Insert nodes and strings just after this node, in its parent (after).
|
template<typename... T>
requires ((detail::NodeOrString<T> && ...)) |
| Result< void > | after (Checked_t, T &&... nodes) const |
| | Same as after(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
|
template<typename... T>
requires ((detail::NodeOrString<T> && ...)) |
| void | replaceWith (T &&... nodes) const |
| | Replace this node with nodes and strings, in its parent (replaceWith).
|
template<typename... T>
requires ((detail::NodeOrString<T> && ...)) |
| Result< void > | replaceWith (Checked_t, T &&... nodes) const |
| | Same as replaceWith(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
|
| void | remove () const |
| | Remove this node from its parent (remove).
|
| Result< void > | remove (Checked_t) const |
| | Same as remove(), but returns a Result with the reason for a failure.
|
| ULDOMNode | raw () const |
| | Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMNode.h> functions.
|
| ULDOMNode | LeakRef () |
| | Give up ownership of the C handle and return it.
|