|
Ultralight C++ API 2.0.0
|
#include <Ultralight/dom/Element.h>
A handle to an element on a page.
dom::Element is a safe handle to an element on a page, providing direct access to document content and events from C++. Its syntax matches JavaScript's DOM, and it doesn't require JavaScript to be enabled.
You can store handles in native objects to update your interface when application state changes, or attach callbacks to respond to user interactions.
This example updates an element and listens for a click:
Copying a handle creates another reference to the same element rather than duplicating the element itself.
A handle keeps its element alive in memory for as long as its page lives, even after calling remove() to take it out of the document tree.
A handle's status depends on whether it holds an element and whether its page remains active:
Every call is safe in any state, so you can chain operations without null checks:
A handle whose page is gone never becomes valid again, even if that page comes back from the back-forward cache. Every DOM handle follows these lifetime and safety rules.
To find out why an operation failed, pass dom::Checked as an extra argument to return a dom::Result holding a dom::Error. A misspelled selector yields an empty handle, so subsequent calls do nothing silently unless Config::diagnostics.dom is set to DiagnosticsLevel::Strict.
You can narrow a generic element to a specific control type using helper methods like AsInput(), AsTextArea(), and AsSelect(). An As*() view returns an empty handle if the element has a different tag.
Converting an element to an input gives access to tag-specific properties:
Static Public Member Functions | |
| static Element | Adopt (ULDOMElement handle) |
| Wrap a C handle you own, taking ownership of it. | |
| static Element | FromBorrowed (ULDOMElement handle) |
| Wrap a C handle the library owns (eg, a callback argument), adding a reference. | |
| Static Public Member Functions inherited from Node | |
| static Node | Adopt (ULDOMNode handle) |
| Wrap a C handle you own, taking ownership of it. | |
| static Node | FromBorrowed (ULDOMNode handle) |
| Wrap a C handle the library owns (eg, a callback argument), adding a reference. | |
Public Member Functions | |
| Element () | |
| Create an empty Element. | |
| Element (const Element &other) | |
| Copy constructor (both handles refer to the same element). | |
| Element (Element &&other) noexcept | |
| Move constructor (other becomes empty). | |
| Element & | operator= (Element other) noexcept |
| Assignment (copies or moves). | |
| Element | querySelector (std::string_view selectors) const |
| Find the first element below this one that matches a CSS selector (querySelector). | |
| Result< Element > | querySelector (std::string_view selectors, Checked_t) const |
| Same as querySelector(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector). | |
| ElementList | querySelectorAll (std::string_view selectors) const |
| Find every element below this one that matches a CSS selector (querySelectorAll). | |
| Result< ElementList > | querySelectorAll (std::string_view selectors, Checked_t) const |
| Same as querySelectorAll(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector). | |
| bool | matches (std::string_view selectors) const |
| Whether or not this element matches a CSS selector (matches). | |
| Result< bool > | matches (std::string_view selectors, Checked_t) const |
| Same as matches(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector). | |
| Element | closest (std::string_view selectors) const |
| Find the closest ancestor that matches a CSS selector, starting with this element itself (closest). | |
| Result< Element > | closest (std::string_view selectors, Checked_t) const |
| Same as closest(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector). | |
| std::optional< std::string > | getAttribute (std::string_view name) const |
| Get an attribute's value (getAttribute). | |
| bool | hasAttribute (std::string_view name) const |
| Whether or not the element has an attribute (hasAttribute). | |
| void | setAttribute (std::string_view name, std::string_view new_value) const |
| Set an attribute's value (setAttribute). | |
| void | removeAttribute (std::string_view name) const |
| Remove an attribute (removeAttribute). | |
| bool | toggleAttribute (std::string_view name) const |
| Toggle an attribute (toggleAttribute). | |
| bool | toggleAttribute (std::string_view name, bool force) const |
| Add or remove an attribute (toggleAttribute with force). | |
| std::vector< std::string > | getAttributeNames () const |
| Get the names of the element's attributes in order (getAttributeNames). | |
| bool | contains (const Element &other) const |
| Whether or not another element is this element or one of its descendants (contains). | |
| ElementList | children () const |
| Get the element's child elements in order (children). | |
| Element | appendChild (const Element &child) const |
| Add an element as the last child of this element (appendChild). | |
| Result< Element > | appendChild (const Element &child, Checked_t) const |
| Same as appendChild(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle). | |
| Node | appendChild (const Node &child) const |
| Add a node of any kind as the last child of this element (appendChild). | |
| Result< Node > | appendChild (const Node &child, Checked_t) const |
| Same as appendChild() with a Node, but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle). | |
| void | appendChild (const DocumentFragment &fragment) const |
| Move all of a fragment's children to the end of this element (appendChild). | |
| Result< void > | appendChild (const DocumentFragment &fragment, Checked_t) const |
| Same as appendChild() with a fragment, but returns a Result with the reason for a failure. | |
| template<typename... T> requires ((detail::NodeOrString<T> && ...)) | |
| void | append (T &&... nodes) const |
| Add nodes and strings to the end of this element, after its last child (append). | |
| template<typename... T> requires ((detail::NodeOrString<T> && ...)) | |
| Result< void > | append (Checked_t, T &&... nodes) const |
| Same as append(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle). | |
| template<typename... T> requires ((detail::NodeOrString<T> && ...)) | |
| void | prepend (T &&... nodes) const |
| Add nodes and strings to the start of this element, before its first child (prepend). | |
| template<typename... T> requires ((detail::NodeOrString<T> && ...)) | |
| Result< void > | prepend (Checked_t, T &&... nodes) const |
| Same as prepend(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle). | |
| template<typename... T> requires ((detail::NodeOrString<T> && ...)) | |
| void | replaceChildren (T &&... new_children) const |
| Replace all of this element's children with nodes and strings (replaceChildren). | |
| template<typename... T> requires ((detail::NodeOrString<T> && ...)) | |
| Result< void > | replaceChildren (Checked_t, T &&... new_children) const |
| Same as replaceChildren(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle). | |
| Element | insertBefore (const Element &child, const Element &ref_child) const |
| Insert an element before one of this element's children (insertBefore). | |
| Result< Element > | insertBefore (const Element &child, const Element &ref_child, Checked_t) const |
| Same as insertBefore(), but returns a Result with the reason for a failure (eg, a NotFoundError when ref_child isn't a child of this element). | |
| Node | insertBefore (const Node &child, const Node &ref_child) const |
| Insert a node of any kind before one of this element's children (insertBefore). | |
| Result< Node > | insertBefore (const Node &child, const Node &ref_child, Checked_t) const |
| Same as insertBefore() with Nodes, but returns a Result with the reason for a failure (eg, a NotFoundError when ref_child isn't a child of this element). | |
| Element | replaceChild (const Element &new_child, const Element &old_child) const |
| Replace one of this element's children with another element (replaceChild). | |
| Result< Element > | replaceChild (const Element &new_child, const Element &old_child, Checked_t) const |
| Same as replaceChild(), but returns a Result with the reason for a failure (eg, a NotFoundError when old_child isn't a child of this element). | |
| Node | replaceChild (const Node &new_child, const Node &old_child) const |
| Replace one of this element's children, of any kind, with a node of any kind (replaceChild). | |
| Result< Node > | replaceChild (const Node &new_child, const Node &old_child, Checked_t) const |
| Same as replaceChild() with Nodes, but returns a Result with the reason for a failure (eg, a NotFoundError when old_child isn't a child of this element). | |
| Element | removeChild (const Element &child) const |
| Remove one of this element's children (removeChild). | |
| Result< Element > | removeChild (const Element &child, Checked_t) const |
| Same as removeChild(), but returns a Result with the reason for a failure (eg, a NotFoundError when child isn't a child of this element). | |
| Node | removeChild (const Node &child) const |
| Remove one of this element's children, of any kind (removeChild). | |
| Result< Node > | removeChild (const Node &child, Checked_t) const |
| Same as removeChild() with a Node, but returns a Result with the reason for a failure (eg, a NotFoundError when child isn't a child of this element). | |
| Element | cloneNode (bool deep=false) const |
| Copy this element (cloneNode). | |
| void | insertAdjacentHTML (std::string_view position, std::string_view html) const |
| Parse markup and insert it relative to this element (insertAdjacentHTML). | |
| Result< void > | insertAdjacentHTML (std::string_view position, std::string_view html, Checked_t) const |
| Same as insertAdjacentHTML(), but returns a Result with the reason for a failure (eg, a SyntaxError for an unknown position). | |
| Element | insertAdjacentElement (std::string_view position, const Element &other) const |
| Insert an element relative to this element (insertAdjacentElement). | |
| Result< Element > | insertAdjacentElement (std::string_view position, const Element &other, Checked_t) const |
| Same as insertAdjacentElement(), but returns a Result with the reason for a failure (eg, a SyntaxError for an unknown position). | |
| void | insertAdjacentText (std::string_view position, std::string_view text) const |
| Insert text relative to this element (insertAdjacentText). | |
| Result< void > | insertAdjacentText (std::string_view position, std::string_view text, Checked_t) const |
| Same as insertAdjacentText(), but returns a Result with the reason for a failure (eg, a SyntaxError for an unknown position). | |
| std::string | tagName () const |
| Get the element's tag name (tagName). | |
| Element | parentElement () const |
| Get the element's parent element (parentElement). | |
| Element | firstElementChild () const |
| Get the element's first child element (firstElementChild). | |
| Element | lastElementChild () const |
| Get the element's last child element (lastElementChild). | |
| Element | nextElementSibling () const |
| Get the element's next sibling element (nextElementSibling). | |
| Element | previousElementSibling () const |
| Get the element's previous sibling element (previousElementSibling). | |
| size_t | childElementCount () const |
| Get the number of the element's child elements (childElementCount). | |
| Document | ownerDocument () const |
| Get the document this element belongs to (ownerDocument). | |
| DOMRect | getBoundingClientRect () const |
| Get the element's bounding rectangle relative to the viewport (getBoundingClientRect). | |
| int | offsetWidth () const |
| Get the element's width in CSS pixels, including its padding and borders (offsetWidth). | |
| int | offsetHeight () const |
| Get the element's height in CSS pixels, including its padding and borders (offsetHeight). | |
| int | offsetTop () const |
| Get the distance from the element's top border edge to its offsetParent()'s top padding edge, in CSS pixels (offsetTop). | |
| int | offsetLeft () const |
| Get the distance from the element's left border edge to its offsetParent()'s left padding edge, in CSS pixels (offsetLeft). | |
| Element | offsetParent () const |
| Get the element that offsetTop() and offsetLeft() measure from (offsetParent). | |
| int | clientWidth () const |
| Get the element's inner width in CSS pixels, including its padding but not its borders or scrollbar (clientWidth). | |
| int | clientHeight () const |
| Get the element's inner height in CSS pixels, including its padding but not its borders or scrollbar (clientHeight). | |
| int | clientTop () const |
| Get the width of the element's top border, in CSS pixels (clientTop). | |
| int | clientLeft () const |
| Get the width of the element's left border, in CSS pixels (clientLeft). | |
| int | scrollWidth () const |
| Get the width of the element's content in CSS pixels, including any part scrolled out of view (scrollWidth). | |
| int | scrollHeight () const |
| Get the height of the element's content in CSS pixels, including any part scrolled out of view (scrollHeight). | |
| void | scrollIntoView (bool align_to_top=true) const |
| Scroll the element's ancestors so the element is visible (scrollIntoView). | |
| void | scrollTo (double x, double y) const |
| Scroll the element's content to a position (scrollTo). | |
| void | scrollBy (double x, double y) const |
| Scroll the element's content by an offset (scrollBy). | |
| void | focus () const |
| Give the element keyboard focus (focus). | |
| void | blur () const |
| Remove keyboard focus from the element (blur). | |
| void | click () const |
| Click the element (click). | |
| bool | dispatchEvent (std::string_view type, const EventInit &init={}) const |
| Dispatch a synthetic event to this element (dispatchEvent). | |
| bool | dispatchCustomEvent (std::string_view type, std::string_view event_detail, const EventInit &init={}) const |
| Dispatch a synthetic CustomEvent with a string payload to this element. | |
| template<typename F> | |
| EventListener | addEventListener (std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const |
| Listen for an event on this element (addEventListener). | |
| template<typename F> | |
| EventListener | addEventListener (std::string_view type, F &&callback, EventListenerFlags flags) const |
| Listen for an event on this element, with options as flags (eg, dom::Once | dom::Capture). | |
| template<typename C, typename M> requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) | |
| EventListener | addEventListener (std::string_view type, C &receiver, M method, const AddEventListenerOptions &options={}) const |
| Listen by calling a member function on an object you keep alive. | |
| template<typename C, typename M> requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) | |
| EventListener | addEventListener (std::string_view type, C &receiver, M method, EventListenerFlags flags) const |
| Listen by calling a member function on an object you keep alive, with options as flags. | |
| template<typename H, typename M> requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) | |
| EventListener | addEventListener (std::string_view type, H holder, M method, const AddEventListenerOptions &options={}) const |
| Listen by calling a member function through a smart pointer that's checked before each call. | |
| template<typename H, typename M> requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) | |
| EventListener | addEventListener (std::string_view type, H holder, M method, EventListenerFlags flags) const |
| Listen by calling a member function through a smart pointer, with options as flags. | |
| template<typename F> | |
| EventListener | On (std::string_view selector, std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const |
| Listen for an event on the elements inside this one that match a CSS selector, including elements added later (event delegation). | |
| template<typename F> | |
| EventListener | On (std::string_view selector, std::string_view type, F &&callback, EventListenerFlags flags) const |
| Listen for an event on matching elements, with options as flags (see the options overload). | |
| template<typename F> | |
| Result< EventListener > | On (std::string_view selector, std::string_view type, F &&callback, const AddEventListenerOptions &options, Checked_t) const |
| Same as On(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector). | |
| template<typename F> | |
| Result< EventListener > | On (std::string_view selector, std::string_view type, F &&callback, Checked_t) const |
| Same as On() with default options, but returns a Result with the reason for a failure. | |
| template<typename C, typename M> requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) | |
| EventListener | On (std::string_view selector, std::string_view type, C &receiver, M method, const AddEventListenerOptions &options={}) const |
| Listen for an event on matching elements by calling a member function on an object you keep alive (see the callable overload). | |
| template<typename C, typename M> requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) | |
| EventListener | On (std::string_view selector, std::string_view type, C &receiver, M method, EventListenerFlags flags) const |
| Listen for an event on matching elements by calling a member function on an object you keep alive, with options as flags. | |
| template<typename H, typename M> requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) | |
| EventListener | On (std::string_view selector, std::string_view type, H holder, M method, const AddEventListenerOptions &options={}) const |
| Listen for an event on matching elements by calling a member function through a smart pointer that's checked before each call (see the callable overload). | |
| template<typename H, typename M> requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) | |
| EventListener | On (std::string_view selector, std::string_view type, H holder, M method, EventListenerFlags flags) const |
| Listen for an event on matching elements by calling a member function through a smart pointer, with options as flags. | |
| HTMLInputElement | AsInput () const |
| Get this element as an <input> (HTMLInputElement). | |
| HTMLTextAreaElement | AsTextArea () const |
| Get this element as a <textarea> (HTMLTextAreaElement). | |
| HTMLSelectElement | AsSelect () const |
| Get this element as a <select> (HTMLSelectElement). | |
| HTMLOptionElement | AsOption () const |
| Get this element as an <option> (HTMLOptionElement). | |
| HTMLFormElement | AsForm () const |
| Get this element as a <form> (HTMLFormElement). | |
| HTMLIFrameElement | AsIFrame () const |
| Get this element as an <iframe> (HTMLIFrameElement). | |
| HTMLImageElement | AsImage () const |
| Get this element as an <img> (HTMLImageElement). | |
| HTMLAnchorElement | AsAnchor () const |
| Get this element as an <a> (HTMLAnchorElement). | |
| ULDOMElement | raw () const |
| Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMElement.h> functions. | |
| ULDOMElement | LeakRef () |
| Give up ownership of the C handle and return it. | |
| Public Member Functions inherited from Node | |
| 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. | |
Public Attributes | |
| detail::StyleProxy | style |
| The element's inline style (style). | |
| detail::ClassListProxy | classList |
| The element's classes (classList). | |
| detail::DatasetProxy | dataset |
| The element's data-* attributes by camelCase name (dataset). | |
| detail::StringProp< detail::TextContentTag > | textContent |
| The text of this element and all its descendants (textContent). | |
| detail::StringProp< detail::InnerTextTag > | innerText |
| The element's text as it's rendered (innerText). | |
| detail::StringProp< detail::InnerHTMLTag > | innerHTML |
| The markup of the element's children (innerHTML). | |
| detail::StringProp< detail::OuterHTMLTag > | outerHTML |
| The markup of the element and its children (outerHTML). | |
| detail::StringProp< detail::IdTag > | id |
| The element's id attribute (id). | |
| detail::StringProp< detail::ClassNameTag > | className |
| The element's class attribute as one string (className). | |
| detail::StringProp< detail::TitleTag > | title |
| The element's title attribute (title). | |
| detail::BoolProp< detail::HiddenTag > | hidden |
| Whether or not the element has a hidden attribute (hidden). | |
| detail::IntProp< detail::TabIndexTag > | tabIndex |
| The element's position in the keyboard focus order (tabIndex). | |
| detail::StringProp< detail::ValueTag > | value |
| The current value of a form control (value). | |
| detail::StringProp< detail::NameTag > | name |
| The element's name attribute (name). | |
| detail::BoolProp< detail::CheckedTag > | checked |
| Whether or not a checkbox or radio button is checked (checked). | |
| detail::BoolProp< detail::DisabledTag > | disabled |
| Whether or not the element has a disabled attribute (disabled). | |
| detail::ValueProp< detail::SelectedIndexTag > | selectedIndex |
| The index of the selected option in a <select> (selectedIndex), as a std::optional<size_t>. | |
| detail::ValueProp< detail::ScrollTopTag > | scrollTop |
| How far the element's content is scrolled down, in CSS pixels (scrollTop). | |
| detail::ValueProp< detail::ScrollLeftTag > | scrollLeft |
| How far the element's content is scrolled right, in CSS pixels (scrollLeft). | |
| Public Attributes inherited from Node | |
| detail::NodeStringProp< detail::NodeValueTag > | nodeValue |
| The text of a text or comment node (nodeValue). | |
| detail::NodeStringProp< detail::NodeTextContentTag > | textContent |
| The text of this node and all its descendants (textContent). | |
| detail::NodeStorage | detail_ |
| Internal storage (not part of the API). | |
Protected Member Functions | |
| Element (ULDOMElement handle) | |
| bool | TagIs (const char *upper_tag) const |
| Whether or not this element's tag name is upper_tag. | |
| Protected Member Functions inherited from Node | |
| Node (ULDOMNode handle) | |
|
inline |
Create an empty Element.
|
inline |
Copy constructor (both handles refer to the same element).
| other | The Element to copy. |
|
inlinenoexcept |
Move constructor (other becomes empty).
| other | The Element to move from. |
|
inlineexplicitprotected |
|
inline |
Listen by calling a member function on an object you keep alive.
| type | The event type to listen for. |
| receiver | The object to call method on. |
| method | The member function to call, taking (dom::Event) or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen by calling a member function on an object you keep alive, with options as flags.
| type | The event type to listen for. |
| receiver | The object to call method on. |
| method | The member function to call, taking (dom::Event) or (). |
| flags | The listener's options (see EventListenerFlags). |
|
inline |
Listen for an event on this element (addEventListener).
The callback can take the event or nothing:
The callback runs on the Renderer's thread while the event is dispatched (eg, inside View::FireMouseEvent() or dispatchEvent()). It can change the DOM and add or remove listeners, its own included.
| type | The event type to listen for (eg, click). |
| callback | The callable to run on each event, taking (dom::Event) or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen for an event on this element, with options as flags (eg, dom::Once | dom::Capture).
| type | The event type to listen for. |
| callback | The callable to run on each event, taking (dom::Event) or (). |
| flags | The listener's options (see EventListenerFlags). |
|
inline |
Listen by calling a member function through a smart pointer that's checked before each call.
A std::shared_ptr keeps the object alive for as long as the listener exists. A std::weak_ptr doesn't, and events are skipped once the object is gone.
| type | The event type to listen for. |
| holder | A std::shared_ptr, a std::weak_ptr, or another smart pointer with a HolderTraits specialization (see LockableHolder). |
| method | The member function to call, taking (dom::Event) or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen by calling a member function through a smart pointer, with options as flags.
| type | The event type to listen for. |
| holder | A std::shared_ptr, a std::weak_ptr, or another smart pointer with a HolderTraits specialization (see LockableHolder). |
| method | The member function to call, taking (dom::Event) or (). |
| flags | The listener's options (see EventListenerFlags). |
|
inlinestatic |
|
inlinenodiscard |
Same as append(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
dom::Checked comes first here, before the nodes (item.append(dom::Checked, icon, "x")).
|
inline |
Add nodes and strings to the end of this element, after its last child (append).
Pass any mix of nodes (including elements and fragments) and strings. Each string is added as a new text node (it's never parsed as markup), and a fragment adds its children and is left empty:
| nodes | The nodes and strings to add, in order. A node that's already in a document moves. |
|
inline |
Move all of a fragment's children to the end of this element (appendChild).
The fragment is empty afterward. This returns nothing, where JavaScript returns the empty fragment.
This is defined in <Ultralight/dom/DocumentFragment.h>. Include that header (or <Ultralight/DOM.h>) to call it.
| fragment | The fragment whose children to move. |
|
inlinenodiscard |
Same as appendChild() with a fragment, but returns a Result with the reason for a failure.
Add an element as the last child of this element (appendChild).
If child is already in a document, it moves from its old position. An element from another document (eg, an iframe's) moves into this one.
| child | The element to add. |
Same as appendChild(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
Add a node of any kind as the last child of this element (appendChild).
Use this for text and comment nodes (eg, from Document::createTextNode()). An Element argument goes to the overload that returns an Element, and a DocumentFragment argument to the overload that returns nothing.
| child | The node to add. If it's already in a document, it moves. |
Same as appendChild() with a Node, but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
|
inline |
Get this element as an <a> (HTMLAnchorElement).
|
inline |
Get this element as a <form> (HTMLFormElement).
|
inline |
Get this element as an <iframe> (HTMLIFrameElement).
|
inline |
Get this element as an <img> (HTMLImageElement).
|
inline |
Get this element as an <input> (HTMLInputElement).
|
inline |
Get this element as an <option> (HTMLOptionElement).
|
inline |
Get this element as a <select> (HTMLSelectElement).
|
inline |
Get this element as a <textarea> (HTMLTextAreaElement).
|
inline |
Remove keyboard focus from the element (blur).
Does nothing if it doesn't have focus.
|
inline |
Get the number of the element's child elements (childElementCount).
|
inline |
Get the element's child elements in order (children).
This is defined in <Ultralight/dom/ElementList.h>. Include that header (or <Ultralight/DOM.h>) to call it.
|
inline |
Click the element (click).
This fires one click event and runs the element's click behavior. For example, a checkbox toggles and fires input and change, a button activates, and a link navigates. There's no mouse movement and no mousedown or mouseup, like the web. The event's isTrusted is false.
|
inline |
Get the element's inner height in CSS pixels, including its padding but not its borders or scrollbar (clientHeight).
|
inline |
Get the width of the element's left border, in CSS pixels (clientLeft).
|
inline |
Get the width of the element's top border, in CSS pixels (clientTop).
|
inline |
Get the element's inner width in CSS pixels, including its padding but not its borders or scrollbar (clientWidth).
|
inline |
Copy this element (cloneNode).
The copy has the same attributes and isn't in the document.
| deep | True to also copy the element's descendants. |
|
inline |
Find the closest ancestor that matches a CSS selector, starting with this element itself (closest).
| selectors | One or more CSS selectors (eg, .item > a[href]). |
|
inline |
Whether or not another element is this element or one of its descendants (contains).
This takes an element only (the web's contains() takes any node).
| other | The element to look for. |
|
inline |
Dispatch a synthetic CustomEvent with a string payload to this element.
This works like dispatchEvent(). Listeners read the payload as the event's detail. Page JavaScript gets it as a string, and a native listener gets it from Event::AsCustom() (this works with JavaScript disabled too). To send structured data, pass JSON and parse it in the listener.
| type | The event type (eg, game-saved). An empty type dispatches nothing. |
| event_detail | The payload as UTF-8 text. It ends at the first null character, and text that isn't valid UTF-8 arrives as no payload. |
| init | The event's bubbles, cancelable, and composed flags (see EventInit). They're all false by default. |
|
inline |
Dispatch a synthetic event to this element (dispatchEvent).
Every listener (native and page JavaScript) runs before this returns. The event's isTrusted is false.
A default action that depends only on the event type still runs, so a synthetic click follows a link. Other default actions don't run. A synthetic click doesn't toggle a checkbox, and a synthetic submit doesn't submit its form (use click() or HTMLFormElement::requestSubmit() for those).
| type | The event type (eg, click). An empty type dispatches nothing. |
| init | The event's bubbles, cancelable, and composed flags (see EventInit). They're all false by default. |
|
inline |
Get the element's first child element (firstElementChild).
|
inline |
Give the element keyboard focus (focus).
This scrolls the element into view, like the web. It does nothing if the element can't take focus or isn't in the document.
|
inlinestatic |
|
inline |
Get an attribute's value (getAttribute).
| name | The attribute name (eg, href). HTML elements ignore its case. |
|
inline |
Get the names of the element's attributes in order (getAttributeNames).
|
inline |
Get the element's bounding rectangle relative to the viewport (getBoundingClientRect).
|
inline |
Whether or not the element has an attribute (hasAttribute).
| name | The attribute name (eg, href). HTML elements ignore its case. |
Insert an element relative to this element (insertAdjacentElement).
| position | Where to insert. beforebegin and afterend insert before and after this element, and afterbegin and beforeend insert before its first child and after its last child. Case doesn't matter. |
| other | The element to insert. If it's already in a document, it moves. |
|
inlinenodiscard |
Same as insertAdjacentElement(), but returns a Result with the reason for a failure (eg, a SyntaxError for an unknown position).
|
inline |
Parse markup and insert it relative to this element (insertAdjacentHTML).
| position | Where to insert. beforebegin and afterend insert before and after this element, and afterbegin and beforeend insert before its first child and after its last child. Case doesn't matter. |
| html | The markup to insert. Scripts in it never run. |
|
inlinenodiscard |
Same as insertAdjacentHTML(), but returns a Result with the reason for a failure (eg, a SyntaxError for an unknown position).
|
inline |
Insert text relative to this element (insertAdjacentText).
The text is never parsed as markup, so this is safe for untrusted text.
| position | Where to insert. beforebegin and afterend insert before and after this element, and afterbegin and beforeend insert before its first child and after its last child. Case doesn't matter. |
| text | The text to insert. |
|
inlinenodiscard |
Same as insertAdjacentText(), but returns a Result with the reason for a failure (eg, a SyntaxError for an unknown position).
Insert an element before one of this element's children (insertBefore).
| child | The element to insert. If it's already in a document, it moves. |
| ref_child | The child to insert before. Pass an empty Element to add child at the end. |
|
inlinenodiscard |
Same as insertBefore(), but returns a Result with the reason for a failure (eg, a NotFoundError when ref_child isn't a child of this element).
Insert a node of any kind before one of this element's children (insertBefore).
Use this for text and comment nodes, or a reference child that isn't an element. With two Element arguments, the overload that returns an Element is used.
| child | The node to insert. If it's already in a document, it moves. |
| ref_child | The child to insert before. Pass an empty Node to add child at the end. |
|
inlinenodiscard |
Same as insertBefore() with Nodes, but returns a Result with the reason for a failure (eg, a NotFoundError when ref_child isn't a child of this element).
|
inline |
Get the element's last child element (lastElementChild).
|
inline |
Give up ownership of the C handle and return it.
This Element becomes empty.
|
inline |
Whether or not this element matches a CSS selector (matches).
| selectors | One or more CSS selectors (eg, .item > a[href]). |
|
inline |
Get the element's next sibling element (nextElementSibling).
|
inline |
Get the element's height in CSS pixels, including its padding and borders (offsetHeight).
|
inline |
Get the distance from the element's left border edge to its offsetParent()'s left padding edge, in CSS pixels (offsetLeft).
|
inline |
Get the element that offsetTop() and offsetLeft() measure from (offsetParent).
This is the nearest positioned ancestor (or a table cell or table around the element), or the <body> if there's none.
|
inline |
Get the distance from the element's top border edge to its offsetParent()'s top padding edge, in CSS pixels (offsetTop).
|
inline |
Get the element's width in CSS pixels, including its padding and borders (offsetWidth).
|
inline |
Listen for an event on matching elements by calling a member function on an object you keep alive (see the callable overload).
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| receiver | The object to call method on. |
| method | The member function to call, taking (dom::Event, dom::Element), (dom::Element), or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen for an event on matching elements by calling a member function on an object you keep alive, with options as flags.
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| receiver | The object to call method on. |
| method | The member function to call, taking (dom::Event, dom::Element), (dom::Element), or (). |
| flags | The listener's options (see EventListenerFlags). |
|
inlinenodiscard |
Same as On() with default options, but returns a Result with the reason for a failure.
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| callback | The callable to run for each match, taking (dom::Event, dom::Element), (dom::Element), or (). |
|
inlinenodiscard |
Same as On(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector).
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| callback | The callable to run for each match, taking (dom::Event, dom::Element), (dom::Element), or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen for an event on the elements inside this one that match a CSS selector, including elements added later (event delegation).
One listener on this element covers the whole subtree. When an event reaches this element, the library checks the event's target and its ancestors (up to and including this element) against selector. The callback runs with the closest match, and doesn't run if nothing matches:
Events that don't bubble (eg, focus) reach this element only in the capture phase, so pass { .capture = true } (or dom::Capture) for them.
| selector | A CSS selector (eg, tr.item). A malformed selector adds nothing and logs a warning. |
| type | The event type to listen for. |
| callback | The callable to run for each match, taking (dom::Event, dom::Element), (dom::Element), or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen for an event on matching elements, with options as flags (see the options overload).
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| callback | The callable to run for each match, taking (dom::Event, dom::Element), (dom::Element), or (). |
| flags | The listener's options (see EventListenerFlags). |
|
inline |
Listen for an event on matching elements by calling a member function through a smart pointer that's checked before each call (see the callable overload).
A std::shared_ptr keeps the object alive for as long as the listener exists. A std::weak_ptr doesn't, and events are skipped once the object is gone.
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| holder | A std::shared_ptr, a std::weak_ptr, or another smart pointer with a HolderTraits specialization (see LockableHolder). |
| method | The member function to call, taking (dom::Event, dom::Element), (dom::Element), or (). |
| options | The listener's options (see AddEventListenerOptions). |
|
inline |
Listen for an event on matching elements by calling a member function through a smart pointer, with options as flags.
| selector | A CSS selector (eg, tr.item). |
| type | The event type to listen for. |
| holder | A std::shared_ptr, a std::weak_ptr, or another smart pointer with a HolderTraits specialization (see LockableHolder). |
| method | The member function to call, taking (dom::Event, dom::Element), (dom::Element), or (). |
| flags | The listener's options (see EventListenerFlags). |
|
inline |
Get the document this element belongs to (ownerDocument).
This is defined in <Ultralight/dom/Document.h>. Include that header (or <Ultralight/DOM.h>) to call it.
|
inline |
Get the element's parent element (parentElement).
|
inlinenodiscard |
Same as prepend(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
dom::Checked comes first here, before the nodes (item.prepend(dom::Checked, icon, "x")).
|
inline |
Add nodes and strings to the start of this element, before its first child (prepend).
Works like append().
| nodes | The nodes and strings to add, in order. A node that's already in a document moves. |
|
inline |
Get the element's previous sibling element (previousElementSibling).
|
inline |
Find the first element below this one that matches a CSS selector (querySelector).
| selectors | One or more CSS selectors (eg, .item > a[href]). |
Same as querySelector(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector).
|
inline |
Find every element below this one that matches a CSS selector (querySelectorAll).
This is defined in <Ultralight/dom/ElementList.h>. Include that header (or <Ultralight/DOM.h>) to call it.
| selectors | One or more CSS selectors (eg, .item > a[href]). |
|
inlinenodiscard |
Same as querySelectorAll(), but returns a Result with the reason for a failure (eg, a SyntaxError for a malformed selector).
|
inline |
|
inline |
Remove an attribute (removeAttribute).
Does nothing if the element doesn't have it.
| name | The attribute name (eg, href). |
Remove one of this element's children (removeChild).
| child | The child to remove. |
Same as removeChild(), but returns a Result with the reason for a failure (eg, a NotFoundError when child isn't a child of this element).
Same as removeChild() with a Node, but returns a Result with the reason for a failure (eg, a NotFoundError when child isn't a child of this element).
Replace one of this element's children with another element (replaceChild).
| new_child | The element to put in its place (it comes first, as on the web). If it's already in a document, it moves. |
| old_child | The child to replace. |
|
inlinenodiscard |
Same as replaceChild(), but returns a Result with the reason for a failure (eg, a NotFoundError when old_child isn't a child of this element).
Replace one of this element's children, of any kind, with a node of any kind (replaceChild).
With two Element arguments, the overload that returns an Element is used.
| new_child | The node to put in its place (it comes first, as on the web). If it's already in a document, it moves. |
| old_child | The child to replace. |
|
inlinenodiscard |
Same as replaceChild() with Nodes, but returns a Result with the reason for a failure (eg, a NotFoundError when old_child isn't a child of this element).
|
inlinenodiscard |
Same as replaceChildren(), but returns a Result with the reason for a failure (eg, a HierarchyRequestError for a cycle).
dom::Checked comes first here, before the nodes:
|
inline |
Replace all of this element's children with nodes and strings (replaceChildren).
Pass any mix of nodes (including elements and fragments) and strings, like append(). With no arguments, this removes all the children.
| new_children | The nodes and strings to add, in order. A node that's already in a document moves. |
|
inline |
Scroll the element's content by an offset (scrollBy).
The content scrolls the same way as with scrollTo().
| x | The horizontal offset in CSS pixels. A value that isn't finite counts as 0. |
| y | The vertical offset in CSS pixels. A value that isn't finite counts as 0. |
|
inline |
Get the height of the element's content in CSS pixels, including any part scrolled out of view (scrollHeight).
|
inline |
Scroll the element's ancestors so the element is visible (scrollIntoView).
Scrolling is instant (CSS scroll-behavior is ignored). This does nothing if the element isn't displayed.
| align_to_top | True to line up the element's top with the top of the visible area, false to line up the bottoms. |
|
inline |
Scroll the element's content to a position (scrollTo).
The content scrolls at once (CSS scroll-behavior is ignored), and the position stays within the scrollable area. This does nothing if the element isn't displayed or has nothing to scroll.
| x | The horizontal position in CSS pixels (any fraction is dropped). A value that isn't finite (eg, NaN) counts as 0. |
| y | The vertical position in CSS pixels (any fraction is dropped). A value that isn't finite counts as 0. |
|
inline |
Get the width of the element's content in CSS pixels, including any part scrolled out of view (scrollWidth).
|
inline |
Set an attribute's value (setAttribute).
| name | The attribute name (eg, href). HTML elements store it in lowercase. |
| new_value | The value to set. |
|
inlineprotected |
Whether or not this element's tag name is upper_tag.
| upper_tag | The tag name to compare with, in uppercase (eg, INPUT). |
|
inline |
Get the element's tag name (tagName).
|
inline |
Toggle an attribute (toggleAttribute).
This removes the attribute if the element has it, or adds it with an empty value if not.
| name | The attribute name (eg, hidden). |
|
inline |
Add or remove an attribute (toggleAttribute with force).
| name | The attribute name (eg, hidden). |
| force | True to add the attribute (with an empty value if it's missing), false to remove it. |
| detail::BoolProp<detail::CheckedTag> checked |
Whether or not a checkbox or radio button is checked (checked).
Other elements read false.
| detail::ClassListProxy classList |
The element's classes (classList).
Use contains(), add(), remove(), toggle(), replace(), length(), and item(). add() and remove() take several classes at once:
| detail::StringProp<detail::ClassNameTag> className |
The element's class attribute as one string (className).
Use classList to work with single classes.
| detail::DatasetProxy dataset |
The element's data-* attributes by camelCase name (dataset).
| detail::BoolProp<detail::DisabledTag> disabled |
| detail::BoolProp<detail::HiddenTag> hidden |
Whether or not the element has a hidden attribute (hidden).
Assign to add or remove it.
A hidden element isn't displayed, unless a CSS rule gives it a display value.
| detail::StringProp<detail::IdTag> id |
The element's id attribute (id).
Reads as an empty string if there's none.
| detail::StringProp<detail::InnerHTMLTag> innerHTML |
The markup of the element's children (innerHTML).
Assign to replace the children with the parsed markup.
| detail::StringProp<detail::InnerTextTag> innerText |
The element's text as it's rendered (innerText).
Assign to replace the element's children with the text. Each line break becomes a <br> element.
| detail::StringProp<detail::NameTag> name |
The element's name attribute (name).
Reads as an empty string if there's none.
Form controls use it as the field name when their form is submitted.
| detail::StringProp<detail::OuterHTMLTag> outerHTML |
The markup of the element and its children (outerHTML).
Assign to replace the element with the parsed markup (scripts in it never run). This handle then refers to the removed element.
| detail::ValueProp<detail::ScrollLeftTag> scrollLeft |
How far the element's content is scrolled right, in CSS pixels (scrollLeft).
Works like scrollTop.
| detail::ValueProp<detail::ScrollTopTag> scrollTop |
How far the element's content is scrolled down, in CSS pixels (scrollTop).
Assign to scroll. The position is clamped to the scrollable range, and scrolling is instant (CSS scroll-behavior is ignored). For the root <html> element (<body> in a quirks-mode page), this scrolls the page.
| detail::ValueProp<detail::SelectedIndexTag> selectedIndex |
The index of the selected option in a <select> (selectedIndex), as a std::optional<size_t>.
It reads std::nullopt when no option is selected (the web reads -1), and other elements read std::nullopt and ignore writes. Assigning std::nullopt or an index that's out of range clears the selection. Assigning fires no change event.
| detail::StyleProxy style |
The element's inline style (style).
Set a property through its member, by name, or all at once with cssText:
A value that doesn't parse is ignored and the old value stays, like the web. That includes a number the property doesn't take (eg, StyleValue::Px() for opacity), which also logs a warning. A mistyped numeric literal (eg, "50pxx") is a compile error instead. An empty string, an Empty dom::StyleValue (or {}), or an unset Color removes the property, and an Invalid StyleValue or an invalid Color is ignored.
Reading a member (or a property by name) gives you its value as a string. AsStyleValue() and AsColor() convert it. A property that isn't set gives an Empty StyleValue or an unset Color, and a value that doesn't convert gives an Invalid StyleValue or an invalid Color:
| detail::IntProp<detail::TabIndexTag> tabIndex |
The element's position in the keyboard focus order (tabIndex).
This reads the tabindex attribute. Without it, elements that take focus by default (eg, links and form controls) read 0 and the rest read -1. Assign to set the attribute (-1 makes the element focusable with focus() but skipped by the Tab key).
| detail::StringProp<detail::TextContentTag> textContent |
The text of this element and all its descendants (textContent).
Assign to replace the element's children with the text (an empty string removes them).
| detail::StringProp<detail::TitleTag> title |
The element's title attribute (title).
Reads as an empty string if there's none.
| detail::StringProp<detail::ValueTag> value |
The current value of a form control (value).
This works on <input>, <textarea>, <select>, and <option>. Other elements read an empty string and ignore writes.