docs
Loading...
Searching...
No Matches
DOM.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5
6///
7/// @namespace ultralight::dom
8///
9/// Direct C++ access to modify page elements and handle events.
10///
11/// `#include <Ultralight/DOM.h>`
12///
13/// @note This API is a preview and may still change after 2.0.
14///
15/// The DOM API connects your application to the HTML pages a View loads, letting native code update
16/// the user interface and respond to user input.
17///
18/// Names and behavior follow the web's DOM, so page script ports over nearly line for line.
19///
20/// This example configures a DOM-ready hook and a click listener on a View before loading a page:
21///
22/// ```
23/// dom::Triggers page;
24///
25/// page.OnDOMReady([](dom::Document document) {
26/// auto status = document.getElementById("status");
27/// status.textContent = "Connected";
28/// status.classList.add("online");
29/// });
30///
31/// page.On("#save", "click", [](dom::Element button) {
32/// button.textContent = "Saved";
33/// });
34///
35/// if (page.AttachTo(view.get()))
36/// view->LoadURL("file:///app.html");
37/// ```
38///
39/// ## Accessing Page Content
40///
41/// You can interact with a View through persistent listeners or a direct document handle:
42///
43/// - **Attach a dom::Triggers set to handle every page a View loads.** Listeners and hooks persist
44/// across navigations until you detach the set or destroy the object.
45/// - **Obtain a dom::Document from the View in LoadListener::OnDOMReady().** The handle applies to
46/// the current page only.
47///
48/// ## Where to Start
49///
50/// Recommended types and headers for common tasks:
51///
52/// | Task | Starting Point |
53/// |--------------------------------|----------------------------------|
54/// | Finding and changing elements | dom::Document, dom::Element |
55/// | Listening for events | dom::Element::addEventListener() |
56/// | Viewport and window events | dom::Window |
57/// | Inspecting call failures | dom::Error |
58/// | Passing elements to JavaScript | `<Ultralight/dom/JSInterop.h>` |
59///
60/// ## Rules for All DOM Types
61///
62/// Every DOM class follows these common conventions:
63///
64/// - **Call DOM operations on the Renderer's thread.** To work with the DOM from another thread,
65/// post tasks using Renderer::PostTask(). Copying, moving, and destroying handles is safe on any
66/// thread.
67/// - **A handle never keeps its page alive.** Once a page navigates away, calls on its handles fail
68/// safely (for handle states, see dom::Element).
69/// - **The API never throws C++ exceptions.** A call that fails returns an empty value, or a
70/// dom::Result holding a dom::Error if you pass dom::Checked.
71/// - **Callbacks that throw are caught and logged.**
72/// - **A const handle can still modify its node.** Constness applies to the handle itself rather
73/// than the page element, so you can still assign to properties and insert child nodes.
74/// - **Counts and indices use `size_t`.** Where web standards return `-1` or `null` for a missing
75/// position, methods return `std::optional<size_t>` holding `std::nullopt`.
76///
77/// @note The DOM API works even with JavaScript disabled
78/// (`ViewConfig::enable_javascript = false`).
79///
80/// @see dom::Document, dom::Element, dom::Triggers, LoadListener::OnDOMReady()
81///
82#pragma once
84#include <Ultralight/Color.h>
95#include <Ultralight/dom/Node.h>