6#include <Ultralight/CAPI/CAPI_DOMDocument.h>
8#include <Ultralight/dom/detail/Receivers.h>
9#include <Ultralight/CAPI/CAPI_DOMEvent.h>
114 detail::DocumentStringProp<detail::DocumentTitleTag>
title;
130 explicit Document(
View* view) : handle_(view ? view->GetDOMDocument() : nullptr) {}
138 : handle_(other.handle_ ? ulCreateDOMDocumentRef(other.handle_) : nullptr) {}
155 std::swap(handle_, other.handle_);
167 explicit operator bool()
const {
return IsAlive(); }
172 bool IsEmpty()
const {
return handle_ ==
nullptr; }
179 bool IsAlive()
const {
return handle_ && ulDOMDocumentIsAlive(handle_); }
191 detail::CString i(
id);
192 return Element::Adopt(ulDOMDocumentGetElementById(handle_, i.c_str()));
204 detail::CString s(selectors);
205 return Element::Adopt(ulDOMDocumentQuerySelector(handle_, s.c_str(),
nullptr));
217 return detail::EmptyHandleError();
218 detail::CString s(selectors);
219 detail::ErrorScope error;
220 ULDOMElement result = ulDOMDocumentQuerySelector(handle_, s.c_str(), error.out());
223 if (error.has_exception())
224 return Unexpected<Error>(error.TakeError());
241 detail::CString s(selectors);
242 return ElementList::Adopt(ulDOMDocumentQuerySelectorAll(handle_, s.c_str(),
nullptr));
255 return detail::EmptyHandleError();
256 detail::CString s(selectors);
257 detail::ErrorScope error;
258 ULDOMElementList result = ulDOMDocumentQuerySelectorAll(handle_, s.c_str(), error.out());
261 return Unexpected<Error>(error.TakeError());
280 return Element::Adopt(ulDOMDocumentElementFromPoint(handle_, x, y));
345 bool hasFocus()
const {
return ulDOMDocumentHasFocus(handle_); }
370 return Element::Adopt(ulDOMDocumentGetFirstElementChild(handle_));
456 detail::CString t(tag_name);
457 return Element::Adopt(ulDOMDocumentCreateElement(handle_, t.c_str(),
nullptr));
472 ulDOMDocumentCreateTextNode(handle_,
data.data() ?
data.data() :
"",
data.size()));
487 ulDOMDocumentCreateComment(handle_,
data.data() ?
data.data() :
"",
data.size()));
527 detail::CString t(type);
528 return ulDOMDocumentDispatchEvent(handle_, t.c_str(), detail::EventInitFlags(init),
550 detail::CString t(type);
551 detail::CString d(event_detail);
552 return ulDOMDocumentDispatchCustomEvent(handle_, t.c_str(),
553 detail::EventInitFlags(init), d.c_str(),
572 template <
typename F>
575 using Fn = std::decay_t<F>;
576 static_assert(detail::InvocableWithEvent<Fn> || std::is_invocable_v<Fn&>,
577 "addEventListener takes a callable invocable with (dom::Event) or ()");
578 detail::CString t(type);
579 ULDOMEventCallback thunk = &detail::EventThunk<Fn>;
580 ULUserDataDestroyCallback destroy = &detail::DeleteCallable<Fn>;
581 return detail::AddListener<Fn>(std::forward<F>(callback), options,
582 [&](
unsigned flags,
void* fn) {
583 return ulDOMDocumentAddEventListener(handle_, t.c_str(),
601 template <
typename F>
603 return addEventListener(type, std::forward<F>(callback), detail::ToOptions(flags));
625 template <
typename C,
typename M>
626 requires(std::is_class_v<C> && std::is_member_function_pointer_v<M>
630 return addEventListener(type, detail::WrapBorrowedMember(receiver, method), options);
652 template <
typename C,
typename M>
653 requires(std::is_class_v<C> && std::is_member_function_pointer_v<M>
654 && !LockableHolder<C>)
657 return addEventListener(type, detail::WrapBorrowedMember(receiver, method), flags);
678 template <
typename H,
typename M>
682 return addEventListener(type, detail::WrapHolderMember(std::move(holder), method), options);
701 template <
typename H,
typename M>
702 requires(LockableHolder<H> && std::is_member_function_pointer_v<M>)
705 return addEventListener(type, detail::WrapHolderMember(std::move(holder), method), flags);
728 template <
typename F>
731 using Fn = std::decay_t<F>;
732 static_assert(detail::InvocableWithEvent<Fn, Element> || std::is_invocable_v<Fn&, Element>
733 || std::is_invocable_v<Fn&>,
734 "On takes a callable invocable with (dom::Event, dom::Element), "
735 "(dom::Element), or ()");
736 detail::CString sel(selector);
737 detail::CString t(type);
738 ULDOMDelegatedEventCallback thunk = &detail::DelegatedThunk<Fn>;
739 ULUserDataDestroyCallback destroy = &detail::DeleteCallable<Fn>;
740 return detail::AddListener<Fn>(std::forward<F>(callback), options,
741 [&](
unsigned flags,
void* fn) {
742 return ulDOMDocumentAddDelegatedEventListener(
743 handle_, sel.c_str(), t.c_str(), flags, thunk, fn,
763 template <
typename F>
766 return On(selector, type, std::forward<F>(callback), detail::ToOptions(flags));
785 template <
typename F>
790 return detail::EmptyHandleError();
791 return detail::CheckedDelegation(
On(selector, type, std::forward<F>(callback), options),
808 template <
typename F>
837 template <
typename C,
typename M>
838 requires(std::is_class_v<C> && std::is_member_function_pointer_v<M>
840 EventListener On(std::string_view selector, std::string_view type, C& receiver, M method,
842 return On(selector, type, detail::WrapBorrowedMember(receiver, method), options);
867 template <
typename C,
typename M>
868 requires(std::is_class_v<C> && std::is_member_function_pointer_v<M>
869 && !LockableHolder<C>)
870 EventListener On(std::string_view selector, std::string_view type, C& receiver, M method,
872 return On(selector, type, detail::WrapBorrowedMember(receiver, method), flags);
897 template <
typename H,
typename M>
899 EventListener On(std::string_view selector, std::string_view type, H holder, M method,
901 return On(selector, type, detail::WrapHolderMember(std::move(holder), method), options);
923 template <
typename H,
typename M>
924 requires(LockableHolder<H> && std::is_member_function_pointer_v<M>)
925 EventListener On(std::string_view selector, std::string_view type, H holder, M method,
927 return On(selector, type, detail::WrapHolderMember(std::move(holder), method), flags);
950 return Document(handle ? ulCreateDOMDocumentRef(handle) :
nullptr);
986#pragma GCC diagnostic push
987#pragma GCC diagnostic ignored "-Winvalid-offsetof"
990inline ULDOMDocument OwnerDocumentHandle(
const void* member,
size_t member_offset) {
991 const Document* document =
reinterpret_cast<const Document*
>(
992 reinterpret_cast<const char*
>(member) - member_offset);
993 return document->raw();
997inline size_t DocumentPropOffset<DocumentTitleTag>() {
998 return offsetof(Document, title);
1001#if defined(__GNUC__)
1002#pragma GCC diagnostic pop
1006void DocumentStringProp<Tag>::operator=(std::string_view value)
const {
1007 Tag::Set(OwnerDocumentHandle(
this, DocumentPropOffset<Tag>()), value);
1011void DocumentStringProp<Tag>::operator+=(std::string_view suffix)
const {
1012 ULDOMDocument h = OwnerDocumentHandle(
this, DocumentPropOffset<Tag>());
1013 std::string combined = Tag::Get(h);
1014 combined.append(suffix);
1015 Tag::Set(h, combined);
1019DocumentStringProp<Tag>::operator std::string()
const {
1020 return Tag::Get(OwnerDocumentHandle(
this, DocumentPropOffset<Tag>()));
struct C_DOMDocument * ULDOMDocument
Opaque handle to a page's DOM document.
Definition View.h:39
Web-page container rendered to an offscreen surface.
Definition View.h:483
A container for assembling DOM nodes off the page.
Definition DocumentFragment.h:43
static DocumentFragment Adopt(ULDOMFragment handle)
Wrap a C handle you own, taking ownership of it.
Definition DocumentFragment.h:229
The root of a page's DOM tree in a View or frame.
Definition Document.h:106
~Document()
Destroy this handle (the document itself isn't affected).
Definition Document.h:162
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).
Definition Document.h:602
Element body() const
Get the document's body element (body).
Definition Document.h:320
Element elementFromPoint(double x, double y) const
Find the topmost element at a point in the viewport (elementFromPoint).
Definition Document.h:279
Element head() const
Get the document's head element (head).
Definition Document.h:327
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.
Definition Document.h:655
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.
Definition Document.h:680
Element firstElementChild() const
Get the document's first child element (firstElementChild).
Definition Document.h:369
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.
Definition Document.h:809
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 o...
Definition Document.h:870
Node lastChild() const
Get the document's last child of any kind (lastChild).
Definition Document.h:362
static Document Adopt(ULDOMDocument handle)
Wrap a C handle you own, taking ownership of it.
Definition Document.h:940
static Document FromBorrowed(ULDOMDocument handle)
Wrap a C handle the library owns (eg, a callback argument), adding a reference.
Definition Document.h:949
ULDOMDocument LeakRef()
Give up ownership of the C handle and return it.
Definition Document.h:966
EventListener addEventListener(std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const
Listen for an event on the document (addEventListener).
Definition Document.h:573
NodeList childNodes() const
Get the document's children of every kind in order (childNodes).
Definition Document.h:410
Element querySelector(std::string_view selectors) const
Find the first element matching a CSS selector (querySelector).
Definition Document.h:203
Document(Document &&other) noexcept
Move constructor (other becomes empty).
Definition Document.h:145
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...
Definition Document.h:925
Window defaultView() const
Get the document's window (defaultView).
Definition Window.h:480
Selection getSelection() const
Get the document's selection (getSelection).
Definition Selection.h:551
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...
Definition Document.h:899
bool hasFocus() const
Whether or not the document has keyboard focus (hasFocus).
Definition Document.h:345
DocumentFragment createDocumentFragment() const
Create an empty document fragment (createDocumentFragment).
Definition Document.h:498
Document()=default
Create an empty Document.
Element documentElement() const
Get the document's root element (documentElement).
Definition Document.h:311
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 s...
Definition Document.h:786
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.
Definition Document.h:764
detail::DocumentStringProp< detail::DocumentTitleTag > title
The document's title (title), the text of its <title> element with whitespace collapsed.
Definition Document.h:114
Node createComment(std::string_view data) const
Create a comment node (createComment).
Definition Document.h:485
size_t childElementCount() const
Get the number of the document's child elements (childElementCount).
Definition Document.h:387
Document(View *view)
Get the document of a View's main frame.
Definition Document.h:130
bool IsEmpty() const
Whether or not this Document is empty (it holds no handle).
Definition Document.h:172
bool IsAlive() const
Whether or not this Document is valid (it isn't empty and its page is still alive).
Definition Document.h:179
Range createRange() const
Create a range (createRange).
Definition Document.h:510
bool dispatchEvent(std::string_view type, const EventInit &init={}) const
Dispatch a synthetic event to the document (dispatchEvent).
Definition Document.h:526
ElementList querySelectorAll(std::string_view selectors) const
Find every element matching a CSS selector (querySelectorAll).
Definition Document.h:240
Element lastElementChild() const
Get the document's last child element (lastElementChild).
Definition Document.h:378
Element activeElement() const
Get the element that has keyboard focus (activeElement).
Definition Document.h:335
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,...
Definition Document.h:703
Document(ULDOMDocument handle)
Definition Document.h:973
ElementList elementsFromPoint(double x, double y) const
Find every element at a point in the viewport, topmost first (elementsFromPoint).
Definition Document.h:299
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 o...
Definition Document.h:840
Node createTextNode(std::string_view data) const
Create a text node (createTextNode).
Definition Document.h:470
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.
Definition Document.h:548
Node firstChild() const
Get the document's first child of any kind (firstChild).
Definition Document.h:355
Document & operator=(Document other) noexcept
Assignment (copies or moves).
Definition Document.h:154
ULDOMDocument raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMDocument.h> functions.
Definition Document.h:959
bool hasChildNodes() const
Whether or not the document has any children (hasChildNodes).
Definition Document.h:417
ElementList children() const
Get the document's child elements in order (children).
Definition Document.h:397
Element getElementById(std::string_view id) const
Find the element with a certain id (getElementById).
Definition Document.h:190
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.
Definition Document.h:628
Result< ElementList > querySelectorAll(std::string_view selectors, Checked_t) const
Same as querySelectorAll(), but returns a Result with the reason for a failure (eg,...
Definition Document.h:252
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 (eve...
Definition Document.h:729
Document(const Document &other)
Copy constructor (both handles refer to the same document).
Definition Document.h:137
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 ...
Definition Document.h:215
Element createElement(std::string_view tag_name) const
Create an element (createElement).
Definition Document.h:455
A handle to an element on a page.
Definition Element.h:142
Document ownerDocument() const
Get the document this element belongs to (ownerDocument).
Definition Document.h:1026
static Element Adopt(ULDOMElement handle)
Wrap a C handle you own, taking ownership of it.
Definition Element.h:1987
Element()
Create an empty Element.
Definition Element.h:368
ULDOMElement raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMElement.h> functions.
Definition Element.h:2006
A fixed list of DOM elements returned by a query or an Element::children() call.
Definition ElementList.h:55
static ElementList Adopt(ULDOMElementList handle)
Wrap a C handle you own, taking ownership of it.
Definition ElementList.h:168
static Error PageGone()
Create a page-gone error (is_page_gone() returns true).
Definition Error.h:123
A handle to a registered DOM event listener.
Definition EventListener.h:151
Document contentDocument() const
Get the document inside the frame (contentDocument).
Definition Document.h:1034
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
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
A fixed list of nodes of any kind (eg, the results of Node::childNodes()).
Definition NodeList.h:34
static NodeList Adopt(ULDOMNodeList handle)
Wrap a C handle you own, taking ownership of it.
Definition NodeList.h:143
A handle to a live range (a span of a page between two boundary points).
Definition Range.h:58
static Range Adopt(ULDOMRange handle)
Wrap a C handle you own, taking ownership of it.
Definition Range.h:525
A handle to the active user selection or caret in a document.
Definition Selection.h:101
The viewport of a DOM document.
Definition Window.h:73
Whether or not the DOM API can hold an object through H (ignoring const and references),...
Definition Holders.h:41
Data-binding API that connects native C++ data to HTML and CSS markup.
Definition ActionInfo.h:13
Direct C++ access to modify page elements and handle events.
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.
Definition Error.h:299
EventListenerFlags
Options for adding an event listener, as flags (addEventListener() and On()).
Definition EventListener.h:41
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.
Options for adding an event listener, like the web's options object for addEventListener().
Definition EventListener.h:526
The type of dom::Checked.
Definition Error.h:282
Options for an event you dispatch yourself (the web's EventInit).
Definition Event.h:30