|
Ultralight C++ API 2.0.0
|
Direct C++ access to modify page elements and handle events.
#include <Ultralight/DOM.h>
The DOM API connects your application to the HTML pages a View loads, letting native code update the user interface and respond to user input.
Names and behavior follow the web's DOM, so page script ports over nearly line for line.
This example configures a DOM-ready hook and a click listener on a View before loading a page:
You can interact with a View through persistent listeners or a direct document handle:
Recommended types and headers for common tasks:
| Task | Starting Point |
|---|---|
| Finding and changing elements | dom::Document, dom::Element |
| Listening for events | dom::Element::addEventListener() |
| Viewport and window events | dom::Window |
| Inspecting call failures | dom::Error |
| Passing elements to JavaScript | <Ultralight/dom/JSInterop.h> |
Every DOM class follows these common conventions:
Namespaces | |
| namespace | data |
| Data-binding API that connects native C++ data to HTML and CSS markup. | |
Classes | |
| class | AbortController |
| A controller that removes a group of DOM event listeners together. More... | |
| class | AbortSignal |
| A signal that removes event listeners when an AbortController aborts. More... | |
| struct | AddEventListenerOptions |
| Options for adding an event listener, like the web's options object for addEventListener(). More... | |
| struct | AttachOptions |
| Options for Triggers::AttachTo(). More... | |
| struct | Checked_t |
| The type of dom::Checked. More... | |
| class | ComputedStyle |
| A read-only view of an element's computed style (getComputedStyle). More... | |
| struct | CSSPropertyName |
| A CSS property name checked when your code compiles (the type of dom::css). More... | |
| class | CssValue |
| Compile-time wrapper for CSS string literals assigned to element styles. More... | |
| class | CustomEvent |
| An event with a string payload (CustomEvent). More... | |
| class | Document |
| The root of a page's DOM tree in a View or frame. More... | |
| class | DocumentFragment |
| A container for assembling DOM nodes off the page. More... | |
| struct | DOMRect |
| A rectangle in CSS pixels, relative to the viewport (DOMRect). More... | |
| class | Element |
| A handle to an element on a page. More... | |
| class | ElementList |
| A fixed list of DOM elements returned by a query or an Element::children() call. More... | |
| class | Error |
| The reason a DOM operation failed. More... | |
| class | Event |
| A page event (such as a click or key press) received by a listener callback. More... | |
| struct | EventInit |
| Options for an event you dispatch yourself (the web's EventInit). More... | |
| class | EventListener |
| A handle to a registered DOM event listener. More... | |
| class | FocusEvent |
| A focus event (FocusEvent). More... | |
| class | HTMLAnchorElement |
| An <a> element (HTMLAnchorElement). More... | |
| class | HTMLFormElement |
| A <form> element (HTMLFormElement). More... | |
| class | HTMLIFrameElement |
| An <iframe> element (HTMLIFrameElement). More... | |
| class | HTMLImageElement |
| An <img> element (HTMLImageElement). More... | |
| class | HTMLInputElement |
| An <input> element (HTMLInputElement). More... | |
| class | HTMLOptionElement |
| An <option> element (HTMLOptionElement). More... | |
| class | HTMLSelectElement |
| A <select> element (HTMLSelectElement). More... | |
| class | HTMLTextAreaElement |
| A <textarea> element (HTMLTextAreaElement). More... | |
| struct | InjectionRequest |
| Information passed to a DOM triggers filter callback. More... | |
| class | InputEvent |
| An edit to an editable element (InputEvent). More... | |
| class | KeyboardEvent |
| A keyboard event (KeyboardEvent). More... | |
| class | MouseEvent |
| A mouse event (MouseEvent). More... | |
| class | Node |
| A reference to an item in the DOM tree. More... | |
| class | NodeList |
| A fixed list of nodes of any kind (eg, the results of Node::childNodes()). More... | |
| class | Range |
| A handle to a live range (a span of a page between two boundary points). More... | |
| class | Selection |
| A handle to the active user selection or caret in a document. More... | |
| struct | StyleValue |
| A numeric CSS value and its unit. More... | |
| class | SubmitEvent |
| A form submission event (SubmitEvent). More... | |
| class | Triggers |
| A set of DOM event listeners and lifecycle hooks that persists across page navigations. More... | |
| struct | ValidityState |
| The ways a form control fails its constraints (ValidityState). More... | |
| class | WheelEvent |
| A wheel event (WheelEvent). More... | |
| class | Window |
| The viewport of a DOM document. More... | |
Concepts | |
| concept | LockableHolder |
| Whether or not the DOM API can hold an object through H (ignoring const and references), ie. | |
Typedefs | |
| template<typename T, typename E = Error> | |
| using | Expected = ultralight::detail::Expected<T, E> |
| A value of type T or an error of type E (like std::expected). | |
| template<typename T> | |
| using | Result = Expected<T, Error> |
| The result of a DOM operation that can fail (either a T or a dom::Error). | |
Enumerations | |
| enum class | SelectionDirection : uint8_t { None , Forward , Backward } |
| The direction of a text control's selection (selectionDirection). More... | |
| enum class | SelectionMode : uint8_t { Preserve , Select , Start , End } |
| Where the selection goes after HTMLInputElement::setRangeText() replaces text (selectMode). More... | |
| enum class | ErrorType : unsigned { None = 0 , Unknown , SyntaxError , InvalidCharacterError , HierarchyRequestError , NotFoundError , NoModificationAllowedError , InvalidStateError , IndexSizeError , NotSupportedError , TypeError , InvalidNodeTypeError , WrongDocumentError } |
| The kinds of DOM exception a dom::Error can hold (the web's DOMException names, plus TypeError). More... | |
| enum class | EventListenerFlags : unsigned { None = 0 , Capture = 1u << 0 , Once = 1u << 1 , Passive = 1u << 2 , NotPassive = 1u << 3 } |
| Options for adding an event listener, as flags (addEventListener() and On()). More... | |
| enum class | NodeType : uint8_t { None = 0 , Element = 1 , Attribute = 2 , Text = 3 , CDATASection = 4 , ProcessingInstruction = 7 , Comment = 8 , Document = 9 , DocumentType = 10 , DocumentFragment = 11 } |
| The kinds of DOM node (nodeType), with the web's numeric values. More... | |
| enum class | StyleUnit : unsigned { Number = 0 , Percent , Px , Em , Rem , Vw , Vh , Deg , Ms , S , Empty } |
| The unit of a StyleValue (the same values as the C API's ULDOMStyleUnit). More... | |
| enum class | AttachFlags : unsigned { None = 0 , AllFrames = 1u << 1 } |
| The flags for Triggers::AttachTo() (the typed form of ULDOMTriggersAttachFlags). More... | |
Functions | |
| ComputedStyle | getComputedStyle (const Element &element) |
| Get an element's computed style (getComputedStyle). | |
| template<typename T> requires std::is_default_constructible_v<T> | |
| T | OrEmpty (Result< T > result) |
| Get a Result's value, or a default-constructed T if it holds an error. | |
| constexpr EventListenerFlags | operator| (EventListenerFlags a, EventListenerFlags b) |
| constexpr EventListenerFlags & | operator|= (EventListenerFlags &a, EventListenerFlags b) |
| js::Value | ToJS (const js::Context &context, const Element &element) |
| Get an element's JavaScript object (the same object the page's scripts see for it). | |
| Element | FromJS (const js::Value &value) |
| Get the element a JavaScript value refers to (the inverse of ToJS()). | |
| js::Context | GetJSContext (const Document &document) |
| Get the JavaScript context of a document (the context its page's scripts run in). | |
| constexpr AttachFlags | operator| (AttachFlags a, AttachFlags b) |
| constexpr AttachFlags & | operator|= (AttachFlags &a, AttachFlags b) |
| template<typename F> | |
| void | SetInjectionFilter (View *view, F &&filter) |
| Set a View's DOM triggers filter with a C++ callable (the typed form of View::SetDOMTriggersInjectionFilter()). | |
| void | ClearInjectionFilter (View *view) |
| Remove a View's DOM triggers filter, so the origin rules alone decide which pages get each set. | |
Variables | |
| template<detail::FixedString Name> | |
| constexpr CSSPropertyName< Name > | css {} |
| A CSS property name checked when your code compiles. | |
| constexpr Checked_t | Checked {} |
| Pass to a DOM call to get a dom::Result holding the reason for a failure instead of an empty value. | |
| constexpr EventListenerFlags | Capture = EventListenerFlags::Capture |
| Receive the event in the capture phase (shorthand for EventListenerFlags::Capture). | |
| constexpr EventListenerFlags | Once = EventListenerFlags::Once |
| Remove the listener after the first event it receives (shorthand for EventListenerFlags::Once). | |
| constexpr EventListenerFlags | Passive = EventListenerFlags::Passive |
| Make the listener passive (shorthand for EventListenerFlags::Passive). | |
| constexpr EventListenerFlags | NotPassive = EventListenerFlags::NotPassive |
| Make the listener not passive (shorthand for EventListenerFlags::NotPassive). | |
| constexpr AttachFlags | AllFrames = AttachFlags::AllFrames |
| Add the listeners to subframes too (shorthand for AttachFlags::AllFrames). | |
| using Expected = ultralight::detail::Expected<T, E> |
A value of type T or an error of type E (like std::expected).
The result of a DOM operation that can fail (either a T or a dom::Error).
DOM calls return one when you pass dom::Checked.
Result is dom::Expected<T, dom::Error>, the library's own type with the members of std::expected (has_value(), value(), error(), value_or(), and_then(), transform(), and the rest). It's the same type in every file of your program, whichever C++ standard each file is compiled with.
|
strong |
The flags for Triggers::AttachTo() (the typed form of ULDOMTriggersAttachFlags).
| Enumerator | |
|---|---|
| None | Add the listeners to the main frame only. |
| AllFrames | Add the listeners to subframes too. |
|
strong |
The kinds of DOM exception a dom::Error can hold (the web's DOMException names, plus TypeError).
The values match ULDOMErrorCode in the C API. They stay the same across releases. Later versions may add types, so handle a value you don't know (eg, with a default case).
| Enumerator | |
|---|---|
| None | Not a DOM exception (eg, a page-gone error). |
| Unknown | A DOM exception with no code of its own here. |
| SyntaxError | A malformed selector, or an invalid position for Element::insertAdjacentHTML() and its siblings. |
| InvalidCharacterError | An invalid tag, attribute, or class name (only the C API reports it, since the typed calls that take these names have no dom::Checked form). |
| HierarchyRequestError | The tree would contain a cycle, or the node can't go there (eg, text directly under the document). |
| NotFoundError | A node isn't where the call needs it (eg, a reference child that isn't a child). |
| NoModificationAllowedError | The target can't be changed (eg, it has no parent). |
| InvalidStateError | The call isn't valid in the target's current state. |
| IndexSizeError | An index or offset is out of range. |
| NotSupportedError | The operation isn't supported. |
| TypeError | An argument has the wrong type (eg, a submitter passed to HTMLFormElement::requestSubmit() that isn't a submit button). |
| InvalidNodeTypeError | The node is the wrong kind for the call (eg, selecting a node that has no parent). |
| WrongDocumentError | A node or range belongs to a different document. |
|
strong |
Options for adding an event listener, as flags (addEventListener() and On()).
Combine them with |:
Passiveness takes two flags because the web has three choices. dom::Passive makes the listener passive (its preventDefault() calls are ignored), dom::NotPassive makes it not passive, and with neither flag the page's default applies.
To add a listener with an AbortSignal, use AddEventListenerOptions instead.
|
strong |
The kinds of DOM node (nodeType), with the web's numeric values.
| Enumerator | |
|---|---|
| None | No node (an empty handle or one whose page is gone). |
| Element | An element (eg, <div>). |
| Attribute | An attribute node. |
| Text | A text node. |
| CDATASection | A CDATA section (XML documents). |
| ProcessingInstruction | A processing instruction (XML documents). |
| Comment | A comment (<!-- -->). |
| Document | A document (never returned as a Node). |
| DocumentType | A doctype (eg, <!DOCTYPE html>). |
| DocumentFragment | A document fragment. |
|
strong |
|
strong |
Where the selection goes after HTMLInputElement::setRangeText() replaces text (selectMode).
| Enumerator | |
|---|---|
| Preserve | Keep the selection, adjusted for the edit. |
| Select | Select the inserted text. |
| Start | Put the caret before the inserted text. |
| End | Put the caret after the inserted text. |
|
strong |
The unit of a StyleValue (the same values as the C API's ULDOMStyleUnit).
| Enumerator | |
|---|---|
| Number | A unitless number (CSS <number>). |
| Percent | % |
| Px | px |
| Em | em |
| Rem | rem |
| Vw | vw |
| Vh | vh |
| Deg | deg |
| Ms | ms |
| S | s |
| Empty | No number (an Empty StyleValue; writing it removes the property). |
|
inline |
Get the element a JavaScript value refers to (the inverse of ToJS()).
The element can be in any frame of the value's View, subframes included. The returned Element belongs to the element's own page (not the value's), so it stops working when that page goes away.
| value | A JavaScript value that refers to a DOM element. |
|
inline |
Get an element's computed style (getComputedStyle).
This is JavaScript's global getComputedStyle(), and the same as Window::getComputedStyle().
| element | The element to read. |
|
inline |
Get the JavaScript context of a document (the context its page's scripts run in).
This works for the document of any frame. View::GetJSContext() only gives the main frame's context. Like any js::Context, the result stops working when the document's page goes away.
| document | The document. |
|
constexpr |
|
constexpr |
|
constexpr |
|
constexpr |
|
nodiscard |
Get a Result's value, or a default-constructed T if it holds an error.
Use this when you've handled the error and want to carry on with an empty value (every operation on an empty Element does nothing):
| result | The Result to read. |
| void SetInjectionFilter | ( | View * | view, |
| F && | filter ) |
Set a View's DOM triggers filter with a C++ callable (the typed form of View::SetDOMTriggersInjectionFilter()).
The View calls the filter each time it's about to add an attached set of DOM listeners to a page (when a page finishes parsing, when you attach a set, and when a page returns from the back-forward cache). The origin rules run first, and rules_allow holds their result. Return true to add the listeners or false to skip the page, whatever the rules decided. Use it for decisions that origin rules can't express, such as checking a user setting or allowing a page whose origin is opaque.
| view | The View to set the filter on (nothing happens if it's nullptr). |
| filter | A callable invocable as bool(const dom::InjectionRequest&). The new filter replaces (and destroys) the previous one. |
|
inline |
Get an element's JavaScript object (the same object the page's scripts see for it).
Converting one element twice gives the same object (they compare ===).
| context | The JavaScript context of the element's own frame (see GetJSContext()). |
| element | The element to convert. |
|
inlineconstexpr |
Add the listeners to subframes too (shorthand for AttachFlags::AllFrames).
|
inlineconstexpr |
Receive the event in the capture phase (shorthand for EventListenerFlags::Capture).
|
inlineconstexpr |
Pass to a DOM call to get a dom::Result holding the reason for a failure instead of an empty value.
|
inlineconstexpr |
A CSS property name checked when your code compiles.
Use it where style access takes a property name, so a typo is a compile error instead of a write that silently does nothing:
Write a name in lowercase CSS form (background-color) or in the camelCase form JavaScript uses for it (backgroundColor, cssFloat for float). Prefixed legacy names (eg, -webkit-border-radius or webkitBorderRadius) work too, and any custom property name (-- followed by at least one character) is accepted.
|
inlineconstexpr |
Make the listener not passive (shorthand for EventListenerFlags::NotPassive).
|
inlineconstexpr |
Remove the listener after the first event it receives (shorthand for EventListenerFlags::Once).
|
inlineconstexpr |
Make the listener passive (shorthand for EventListenerFlags::Passive).