The root of a page's DOM tree in a View or frame.
dom::Document provides native C++ access to a page's DOM tree. It's the starting point for querying elements and updating the user interface rendered by a View.
You typically obtain the document during page loads and keep handles to the elements you want to modify later.
The following listener stores a score element once the main frame's document is parsed and ready:
public:
if (!is_main_frame)
return;
score_ = document.getElementById("score");
}
void SetScore(int score) { score_.textContent = std::to_string(score); }
private:
};
User-defined interface to handle load-related events for a View.
Definition Listener.h:218
virtual void OnDOMReady(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const String &url)
Called when a frame's document has been parsed and the DOM is ready.
Definition Listener.h:316
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Web-page container rendered to an offscreen surface.
Definition View.h:483
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
Getting a Document
You obtain a document from its owning View or frame:
- Main frame documents come from the View. Passing a View pointer to the Document constructor returns the active document of the top-level frame.
- Subframe documents come from their elements. Calling dom::HTMLIFrameElement::contentDocument() on a frame element returns the document loaded inside that frame.
You should construct the main frame's document inside LoadListener::OnDOMReady(), where the new document is in place. Before the first page loads and while a new page is loading, the constructor returns the previous document, which goes away when the new page replaces it.
LoadListener::OnDOMReady() runs for every frame on the page. Check the is_main_frame parameter to ensure top-level setup logic runs only for the main frame.
Navigating to a new page creates a new document– handles from the previous page belong to a page that is gone (dom::Element covers handle states).
Relationship to Nodes
dom::Document is a distinct type that doesn't inherit from dom::Node. Because the document isn't a node, the root element has no parent in the DOM tree.
On the root element, parentNode() is empty and ownerDocument() gets back to the document:
Document ownerDocument() const
Get the document this element belongs to (ownerDocument).
Definition Document.h:1026
Node parentNode() const
Get the node's parent (parentNode).
Definition Node.h:266
Document Events
You can listen for events on the document with addEventListener() or handle matching descendants using On(). Events dispatched on elements throughout the page bubble up to the document unless stopped along the way.
Page load and viewport resize events target the window rather than the document, so document listeners never receive them. Call defaultView() to obtain the page's dom::Window when you need to handle window-level events.
- Note
- Don't add a DOMContentLoaded listener in LoadListener::OnDOMReady(). The event has already fired by the time the callback runs, so a listener added there never fires.
- See also
- dom::Element, dom::Node, dom::Window, dom::Triggers, LoadListener::OnDOMReady()
|
| | Document ()=default |
| | Create an empty Document.
|
| | Document (View *view) |
| | Get the document of a View's main frame.
|
| | Document (const Document &other) |
| | Copy constructor (both handles refer to the same document).
|
| | Document (Document &&other) noexcept |
| | Move constructor (other becomes empty).
|
| Document & | operator= (Document other) noexcept |
| | Assignment (copies or moves).
|
| | ~Document () |
| | Destroy this handle (the document itself isn't affected).
|
| | operator bool () const |
| | Whether or not this Document is valid (see IsAlive()).
|
| bool | IsEmpty () const |
| | Whether or not this Document is empty (it holds no handle).
|
| bool | IsAlive () const |
| | Whether or not this Document is valid (it isn't empty and its page is still alive).
|
| Element | getElementById (std::string_view id) const |
| | Find the element with a certain id (getElementById).
|
| Element | querySelector (std::string_view selectors) const |
| | Find the first element matching 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 matching 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).
|
| Element | elementFromPoint (double x, double y) const |
| | Find the topmost element at a point in the viewport (elementFromPoint).
|
| ElementList | elementsFromPoint (double x, double y) const |
| | Find every element at a point in the viewport, topmost first (elementsFromPoint).
|
| Element | documentElement () const |
| | Get the document's root element (documentElement).
|
| Element | body () const |
| | Get the document's body element (body).
|
| Element | head () const |
| | Get the document's head element (head).
|
| Element | activeElement () const |
| | Get the element that has keyboard focus (activeElement).
|
| bool | hasFocus () const |
| | Whether or not the document has keyboard focus (hasFocus).
|
| Node | firstChild () const |
| | Get the document's first child of any kind (firstChild).
|
| Node | lastChild () const |
| | Get the document's last child of any kind (lastChild).
|
| Element | firstElementChild () const |
| | Get the document's first child element (firstElementChild).
|
| Element | lastElementChild () const |
| | Get the document's last child element (lastElementChild).
|
| size_t | childElementCount () const |
| | Get the number of the document's child elements (childElementCount).
|
| ElementList | children () const |
| | Get the document's child elements in order (children).
|
| NodeList | childNodes () const |
| | Get the document's children of every kind in order (childNodes).
|
| bool | hasChildNodes () const |
| | Whether or not the document has any children (hasChildNodes).
|
| Window | defaultView () const |
| | Get the document's window (defaultView).
|
| Selection | getSelection () const |
| | Get the document's selection (getSelection).
|
| Element | createElement (std::string_view tag_name) const |
| | Create an element (createElement).
|
| Node | createTextNode (std::string_view data) const |
| | Create a text node (createTextNode).
|
| Node | createComment (std::string_view data) const |
| | Create a comment node (createComment).
|
| DocumentFragment | createDocumentFragment () const |
| | Create an empty document fragment (createDocumentFragment).
|
| Range | createRange () const |
| | Create a range (createRange).
|
| bool | dispatchEvent (std::string_view type, const EventInit &init={}) const |
| | Dispatch a synthetic event to the document (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 the document.
|
| template<typename F> |
| EventListener | addEventListener (std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const |
| | Listen for an event on the document (addEventListener).
|
| template<typename F> |
| EventListener | addEventListener (std::string_view type, F &&callback, EventListenerFlags flags) const |
| | Listen for an event on the document, 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 for an event on the document by calling a member function of an object.
|
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 for an event on the document by calling a member function of an object, 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 for an event on the document by calling a member function through a smart pointer.
|
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 for an event on the document 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 every element that matches a CSS selector, including elements added later (event delegation from the document).
|
| template<typename F> |
| EventListener | On (std::string_view selector, std::string_view type, F &&callback, EventListenerFlags flags) const |
| | Listen for an event on every element that matches a CSS selector, with options as flags.
|
| 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 every element that matches a CSS selector by calling a member function of an object.
|
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 every element that matches a CSS selector by calling a member function of an object, 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 every element that matches a CSS selector by calling a member function through a smart pointer.
|
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 every element that matches a CSS selector by calling a member function through a smart pointer, with options as flags.
|
| ULDOMDocument | raw () const |
| | Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMDocument.h> functions.
|
| ULDOMDocument | LeakRef () |
| | Give up ownership of the C handle and return it.
|