7#pragma push_macro("None")
10#include <Ultralight/CAPI/CAPI_DOMNode.h>
12#include <Ultralight/dom/detail/Properties.h>
37concept NodeOrString = std::is_convertible_v<T&&, const Node&>
38 || (std::is_convertible_v<T&&, std::string_view>
39 && !std::is_same_v<std::remove_cvref_t<T>, std::nullptr_t>);
41inline ULDOMNodeOrString NodeItem(
const Node& node);
44ULDOMNodeOrString NodeOrStringItem(T&& item) {
45 if constexpr (std::is_convertible_v<T&&, const Node&>) {
46 return NodeItem(item);
48 std::string_view text = item;
49 return {
nullptr, text.data() ? text.data() :
"", text.size() };
55template <
typename... T>
56std::array<ULDOMNodeOrString,
sizeof...(T)> NodeOrStringItems(T&&... items) {
57 return { NodeOrStringItem(std::forward<T>(items))... };
188 other.detail_.handle =
nullptr;
199 std::swap(
detail_.handle, other.detail_.handle);
211 explicit operator bool()
const {
return IsAlive(); }
255 return detail::TakeString(ulDOMNodeGetNodeName(
detail_.handle));
295 return Node(ulDOMNodeGetPreviousSibling(
detail_.handle));
376 template <
typename... T>
377 requires((detail::NodeOrString<T> && ...))
379 auto items = detail::NodeOrStringItems(std::forward<T>(nodes)...);
380 ulDOMNodeBefore(
detail_.handle, items.data(), items.size(),
nullptr);
393 template <
typename... T>
394 requires((detail::NodeOrString<T> && ...))
396 auto items = detail::NodeOrStringItems(std::forward<T>(nodes)...);
397 if (
IsEmpty() || detail::HasEmptyItem(items))
398 return detail::EmptyHandleError();
399 detail::ErrorScope error;
400 if (ulDOMNodeBefore(
detail_.handle, items.data(), items.size(), error.out()))
402 return Unexpected<Error>(error.TakeError());
417 template <
typename... T>
418 requires((detail::NodeOrString<T> && ...))
420 auto items = detail::NodeOrStringItems(std::forward<T>(nodes)...);
421 ulDOMNodeAfter(
detail_.handle, items.data(), items.size(),
nullptr);
434 template <
typename... T>
435 requires((detail::NodeOrString<T> && ...))
437 auto items = detail::NodeOrStringItems(std::forward<T>(nodes)...);
438 if (
IsEmpty() || detail::HasEmptyItem(items))
439 return detail::EmptyHandleError();
440 detail::ErrorScope error;
441 if (ulDOMNodeAfter(
detail_.handle, items.data(), items.size(), error.out()))
443 return Unexpected<Error>(error.TakeError());
459 template <
typename... T>
460 requires((detail::NodeOrString<T> && ...))
462 auto items = detail::NodeOrStringItems(std::forward<T>(nodes)...);
463 ulDOMNodeReplaceWith(
detail_.handle, items.data(), items.size(),
nullptr);
476 template <
typename... T>
477 requires((detail::NodeOrString<T> && ...))
479 auto items = detail::NodeOrStringItems(std::forward<T>(nodes)...);
480 if (
IsEmpty() || detail::HasEmptyItem(items))
481 return detail::EmptyHandleError();
482 detail::ErrorScope error;
483 if (ulDOMNodeReplaceWith(
detail_.handle, items.data(), items.size(), error.out()))
485 return Unexpected<Error>(error.TakeError());
503 return detail::EmptyHandleError();
504 detail::ErrorScope error;
505 if (ulDOMNodeRemove(
detail_.handle, error.out()))
507 return Unexpected<Error>(error.TakeError());
530 return Node(handle ? ulCreateDOMNodeRef(handle) :
nullptr);
547 ULDOMNode handle =
detail_.handle;
559static_assert(std::is_standard_layout_v<Node>,
"Node must stay standard-layout");
560static_assert(!std::is_aggregate_v<Node>,
"Node must not be an aggregate");
565inline ULDOMNodeOrString NodeItem(
const Node& node) {
566 return { node.raw(),
nullptr, 0 };
569inline ULDOMNode OwnerNodeHandle(
const void* member,
size_t member_offset) {
570 const Node* node =
reinterpret_cast<const Node*
>(
571 reinterpret_cast<const char*
>(member) - member_offset);
572 return node->detail_.handle;
576inline size_t NodePropOffset<NodeValueTag>() {
577 return offsetof(Node, nodeValue);
580inline size_t NodePropOffset<NodeTextContentTag>() {
581 return offsetof(Node, textContent);
585void NodeStringProp<Tag>::operator=(std::string_view value)
const {
586 Tag::Set(OwnerNodeHandle(
this, NodePropOffset<Tag>()), value);
590void NodeStringProp<Tag>::operator+=(std::string_view suffix)
const {
591 ULDOMNode h = OwnerNodeHandle(
this, NodePropOffset<Tag>());
592 std::string combined = Tag::Get(h);
593 combined.append(suffix);
594 Tag::Set(h, combined);
598NodeStringProp<Tag>::operator std::string()
const {
599 return Tag::Get(OwnerNodeHandle(
this, NodePropOffset<Tag>()));
608#pragma pop_macro("None")
A container for assembling DOM nodes off the page.
Definition DocumentFragment.h:43
The root of a page's DOM tree in a View or frame.
Definition Document.h:106
A handle to an element on a page.
Definition Element.h:142
A reference to an item in the DOM tree.
Definition Node.h:147
detail::NodeStorage detail_
Internal storage (not part of the API).
Definition Node.h:166
Node()
Create an empty Node.
Definition Node.h:171
Node nextSibling() const
Get the node's next sibling of any kind (nextSibling).
Definition Node.h:303
ULDOMNode LeakRef()
Give up ownership of the C handle and return it.
Definition Node.h:546
Result< void > replaceWith(Checked_t, T &&... nodes) const
Same as replaceWith(), but returns a Result with the reason for a failure (eg, a HierarchyRequestErro...
Definition Node.h:478
Node(const Node &other)
Copy constructor (both handles refer to the same node).
Definition Node.h:178
Node(Node &&other) noexcept
Move constructor (other becomes empty).
Definition Node.h:187
DocumentFragment AsFragment() const
Get this node as a DocumentFragment.
Definition DocumentFragment.h:281
Node lastChild() const
Get the node's last child of any kind (lastChild).
Definition Node.h:287
NodeList childNodes() const
Get the node's children of every kind in order (childNodes).
Definition NodeList.h:171
detail::NodeStringProp< detail::NodeTextContentTag > textContent
The text of this node and all its descendants (textContent).
Definition Node.h:161
NodeType nodeType() const
Get the kind of node (nodeType).
Definition Node.h:244
void before(T &&... nodes) const
Insert nodes and strings just before this node, in its parent (before).
Definition Node.h:378
ULDOMNode raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMNode.h> functions.
Definition Node.h:539
Document ownerDocument() const
Get the document this node belongs to (ownerDocument).
Definition Document.h:1030
static Node Adopt(ULDOMNode handle)
Wrap a C handle you own, taking ownership of it.
Definition Node.h:520
~Node()
Destroy this handle (the node itself isn't affected).
Definition Node.h:206
bool isConnected() const
Whether or not the node is in its document (isConnected).
Definition Node.h:335
void remove() const
Remove this node from its parent (remove).
Definition Node.h:493
Node parentNode() const
Get the node's parent (parentNode).
Definition Node.h:266
Node & operator=(Node other) noexcept
Assignment (copies or moves).
Definition Node.h:198
bool IsEmpty() const
Whether or not this Node is empty (it holds no handle).
Definition Node.h:216
bool IsAlive() const
Whether or not this Node is valid (it isn't empty and its page is still alive).
Definition Node.h:223
Node(ULDOMNode handle)
Definition Node.h:553
detail::NodeStringProp< detail::NodeValueTag > nodeValue
The text of a text or comment node (nodeValue).
Definition Node.h:154
bool IsSame(const Node &other) const
Whether or not two handles refer to the same node (isSameNode).
Definition Node.h:232
static Node FromBorrowed(ULDOMNode handle)
Wrap a C handle the library owns (eg, a callback argument), adding a reference.
Definition Node.h:529
Node firstChild() const
Get the node's first child of any kind (firstChild).
Definition Node.h:280
Element parentElement() const
Get the node's parent element (parentElement).
Definition Element.h:3448
bool hasChildNodes() const
Whether or not the node has any children (hasChildNodes).
Definition Node.h:324
Result< void > remove(Checked_t) const
Same as remove(), but returns a Result with the reason for a failure.
Definition Node.h:501
Node previousSibling() const
Get the node's previous sibling of any kind (previousSibling).
Definition Node.h:294
Element AsElement() const
Get this node as an Element.
Definition Element.h:3452
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...
Definition Node.h:395
void replaceWith(T &&... nodes) const
Replace this node with nodes and strings, in its parent (replaceWith).
Definition Node.h:461
void after(T &&... nodes) const
Insert nodes and strings just after this node, in its parent (after).
Definition Node.h:419
std::string nodeName() const
Get the node's name (nodeName).
Definition Node.h:254
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 ...
Definition Node.h:436
A fixed list of nodes of any kind (eg, the results of Node::childNodes()).
Definition NodeList.h:34
Direct C++ access to modify page elements and handle events.
NodeType
The kinds of DOM node (nodeType), with the web's numeric values.
Definition Node.h:66
@ Comment
A comment (<!-- -->).
Definition Node.h:73
@ CDATASection
A CDATA section (XML documents).
Definition Node.h:71
@ ProcessingInstruction
A processing instruction (XML documents).
Definition Node.h:72
@ Text
A text node.
Definition Node.h:70
@ DocumentType
A doctype (eg, <!DOCTYPE html>).
Definition Node.h:75
@ Attribute
An attribute node.
Definition Node.h:69
Expected< T, Error > Result
The result of a DOM operation that can fail (either a T or a dom::Error).
Definition Error.h:277
Root namespace for every public Ultralight type, function, and enumeration.
@ None
Definition Anchor.h:36
The type of dom::Checked.
Definition Error.h:282