docs
Loading...
Searching...
No Matches
Selection.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#pragma once
6#include <Ultralight/CAPI/CAPI_DOMSelection.h>
12
13#include <cstddef>
14#include <string>
15#include <string_view>
16#include <utility>
17
18namespace ultralight {
19namespace dom {
20
21///
22/// A handle to the active user selection or caret in a document.
23///
24/// A dom::Selection represents either a caret or highlighted text on a page. You obtain one by
25/// calling Document::getSelection() or Window::getSelection().
26///
27/// The handle is live-- reading its properties reflects the user's current selection.
28///
29/// This example checks whether the user highlighted text and shows a quote button:
30///
31/// ```
32/// dom::Selection selection = document.getSelection();
33/// if (!selection.isCollapsed())
34/// ShowQuoteButton(selection.toString());
35/// ```
36///
37/// ## Anchor and Focus
38///
39/// A selection spans between two endpoints in the document tree:
40///
41/// - **The anchor marks where the selection starts.** Calling anchorNode() returns the containing
42/// dom::Node, and anchorOffset() returns the offset within that node.
43/// - **The focus marks where the selection ends.** Calling focusNode() and focusOffset() returns
44/// its position, which can sit before the anchor when the user selects backward.
45///
46/// Each document has its own Selection (including each subframe).
47///
48/// ## Reacting to Selection Changes
49///
50/// Every change to the active selection dispatches a `selectionchange` event on the document. The
51/// library delivers these events during a later Renderer::Update(), dispatching one event for each
52/// change.
53///
54/// This example updates UI state whenever the selection changes:
55///
56/// ```
57/// document.addEventListener("selectionchange", [selection] {
58/// UpdateQuoteButton(selection.toString());
59/// });
60/// ```
61///
62/// ## Selecting a Range
63///
64/// To select a dom::Range, call removeAllRanges() before calling addRange(). On its own, addRange()
65/// only expands an existing selection if the new range overlaps it, and it ignores any range that
66/// doesn't overlap.
67///
68/// This example selects the contents of an element:
69///
70/// ```
71/// dom::Range range = document.createRange();
72/// range.selectNodeContents(document.getElementById("quote"));
73/// selection.removeAllRanges();
74/// selection.addRange(range);
75/// ```
76///
77/// ## Selections in Text Fields
78///
79/// The selection doesn't enter editable form controls. When an `<input>` or `<textarea>` has focus,
80/// anchorNode() and focusNode() sit just outside the element, and isCollapsed() returns true. To
81/// read or change selected text inside an editable field, use the element's `selectionStart` and
82/// `selectionEnd` properties instead (see dom::HTMLInputElement).
83///
84/// ## Differences from Web Browsers
85///
86/// Several methods handle node validation and range ownership differently from JavaScript:
87///
88/// - **Cross-document nodes are ignored.** Passing a node from another document (such as a
89/// subframe) leaves the selection unchanged instead of throwing an exception.
90/// - **Offsets past the end of a node fail only in extend().** Other placement methods like
91/// collapse(), setBaseAndExtent(), and selectAllChildren() don't check whether an offset exceeds
92/// the node's length.
93/// - **getRangeAt() returns an independent copy.** Changes made to the returned dom::Range don't
94/// alter the active selection.
95///
96/// After its page goes away, a Selection stops working like a dom::Element does (see that class).
97///
98/// @see dom::Document::getSelection(), dom::Window::getSelection(), dom::Range,
99/// dom::HTMLInputElement
100///
102 public:
103 ///
104 /// Create an empty Selection.
105 ///
107
108 ///
109 /// Copy constructor (both handles refer to the same selection).
110 ///
111 /// @param other The Selection to copy.
112 ///
113 Selection(const Selection& other)
114 : handle_(other.handle_ ? ulCreateDOMSelectionRef(other.handle_) : nullptr) {}
115
116 ///
117 /// Move constructor (`other` becomes empty).
118 ///
119 /// @param other The Selection to move from.
120 ///
121 Selection(Selection&& other) noexcept : handle_(other.handle_) {
122 other.handle_ = nullptr;
123 }
124
125 ///
126 /// Assignment (copies or moves).
127 ///
128 /// @param other The Selection to assign from.
129 ///
130 /// @return Returns this Selection.
131 ///
132 Selection& operator=(Selection other) noexcept {
133 std::swap(handle_, other.handle_);
134 return *this;
135 }
136
137 ///
138 /// Destroy this handle (the selection itself isn't affected).
139 ///
140 ~Selection() { ulDestroyDOMSelection(handle_); }
141
142 ///
143 /// Whether or not this Selection is valid (see IsAlive()).
144 ///
145 explicit operator bool() const { return IsAlive(); }
146
147 ///
148 /// Whether or not this Selection is empty (it holds no handle).
149 ///
150 bool IsEmpty() const { return handle_ == nullptr; }
151
152 ///
153 /// Whether or not this Selection is valid (it isn't empty and its page is still alive).
154 ///
155 /// @note Safe to call from any thread.
156 ///
157 bool IsAlive() const { return handle_ && ulDOMSelectionIsAlive(handle_); }
158
159 // --- Reads ------------------------------------------------------------------------------
160
161 ///
162 /// Get the node the selection starts in (anchorNode).
163 ///
164 /// @return Returns the node (empty if nothing is selected).
165 ///
166 Node anchorNode() const { return Node::Adopt(ulDOMSelectionGetAnchorNode(handle_)); }
167
168 ///
169 /// Get the offset of the anchor in anchorNode() (anchorOffset).
170 ///
171 size_t anchorOffset() const { return ulDOMSelectionGetAnchorOffset(handle_); }
172
173 ///
174 /// Get the node the selection ends in (focusNode).
175 ///
176 /// @return Returns the node (empty if nothing is selected).
177 ///
178 Node focusNode() const { return Node::Adopt(ulDOMSelectionGetFocusNode(handle_)); }
179
180 ///
181 /// Get the offset of the focus in focusNode() (focusOffset).
182 ///
183 size_t focusOffset() const { return ulDOMSelectionGetFocusOffset(handle_); }
184
185 ///
186 /// Whether or not the selection is a caret (isCollapsed).
187 ///
188 /// @return Returns true for a caret, and also when nothing is selected (or the Selection is
189 /// empty or its page is gone).
190 ///
191 bool isCollapsed() const { return ulDOMSelectionIsCollapsed(handle_); }
192
193 ///
194 /// Get the number of ranges in the selection (rangeCount).
195 ///
196 /// @return Returns 0 if nothing is selected, otherwise 1.
197 ///
198 size_t rangeCount() const { return ulDOMSelectionGetRangeCount(handle_); }
199
200 ///
201 /// Get the kind of selection (type).
202 ///
203 /// @return Returns `None` (nothing is selected), `Caret`, or `Range`.
204 ///
205 std::string type() const { return detail::TakeString(ulDOMSelectionGetType(handle_)); }
206
207 ///
208 /// Get the selected text (toString).
209 ///
210 std::string toString() const {
211 return detail::TakeString(ulDOMSelectionToString(handle_));
212 }
213
214 // --- Placing the selection --------------------------------------------------------------
215
216 ///
217 /// Place a caret (collapse or setPosition).
218 ///
219 /// @param node The node to put the caret in. Pass an empty Node to clear the selection.
220 ///
221 /// @param offset The offset in `node` (like a Range boundary point).
222 ///
223 /// @note Unlike the web, an `offset` past the end of `node` doesn't fail, so check it yourself.
224 ///
225 void collapse(const Node& node, size_t offset = 0) const {
226 ulDOMSelectionCollapse(handle_, node.raw(), offset, nullptr);
227 }
228
229 ///
230 /// Same as collapse(), but returns a Result with the reason for a failure.
231 ///
232 /// @return Returns success (it fails only when this Selection is empty, or its page or
233 /// `node`'s page is gone).
234 ///
235 [[nodiscard]] Result<void> collapse(const Node& node, Checked_t) const {
236 return collapse(node, 0, Checked);
237 }
238
239 ///
240 /// Same as collapse() with an `offset`, but returns a Result with the reason for a failure.
241 ///
242 /// @return Returns success (it fails only when this Selection is empty, or its page or
243 /// `node`'s page is gone).
244 ///
245 [[nodiscard]] Result<void> collapse(const Node& node, size_t offset, Checked_t) const {
246 if (IsEmpty())
247 return detail::EmptyHandleError();
248 detail::ErrorScope error;
249 if (ulDOMSelectionCollapse(handle_, node.raw(), offset, error.out()))
250 return {};
251 return Unexpected<Error>(error.TakeError());
252 }
253
254 ///
255 /// Collapse the selection to its start (collapseToStart). Does nothing if nothing is selected.
256 ///
257 void collapseToStart() const { ulDOMSelectionCollapseToStart(handle_, nullptr); }
258
259 ///
260 /// Same as collapseToStart(), but returns a Result with the reason for a failure (eg, an
261 /// InvalidStateError if nothing is selected).
262 ///
263 /// @return Returns success. Fails with an InvalidStateError if nothing is selected.
264 ///
266 if (IsEmpty())
267 return detail::EmptyHandleError();
268 detail::ErrorScope error;
269 if (ulDOMSelectionCollapseToStart(handle_, error.out()))
270 return {};
271 return Unexpected<Error>(error.TakeError());
272 }
273
274 ///
275 /// Collapse the selection to its end (collapseToEnd). Does nothing if nothing is selected.
276 ///
277 void collapseToEnd() const { ulDOMSelectionCollapseToEnd(handle_, nullptr); }
278
279 ///
280 /// Same as collapseToEnd(), but returns a Result with the reason for a failure (eg, an
281 /// InvalidStateError if nothing is selected).
282 ///
283 /// @return Returns success. Fails with an InvalidStateError if nothing is selected.
284 ///
285 [[nodiscard]] Result<void> collapseToEnd(Checked_t) const {
286 if (IsEmpty())
287 return detail::EmptyHandleError();
288 detail::ErrorScope error;
289 if (ulDOMSelectionCollapseToEnd(handle_, error.out()))
290 return {};
291 return Unexpected<Error>(error.TakeError());
292 }
293
294 ///
295 /// Move the focus and keep the anchor where it is (extend).
296 ///
297 /// Use this to grow or shrink the selection from one end.
298 ///
299 /// @param node The node to move the focus into.
300 ///
301 /// @param offset The offset in `node` (like a Range boundary point).
302 ///
303 /// @note Nothing changes if nothing is selected or `offset` is past the end of `node`.
304 ///
305 void extend(const Node& node, size_t offset = 0) const {
306 ulDOMSelectionExtend(handle_, node.raw(), offset, nullptr);
307 }
308
309 ///
310 /// Same as extend(), but returns a Result with the reason for a failure (eg, an
311 /// InvalidStateError if nothing is selected).
312 ///
313 /// @return Returns success. Fails with an InvalidStateError if nothing is selected, or an
314 /// IndexSizeError if `offset` is past the end of `node`.
315 ///
316 [[nodiscard]] Result<void> extend(const Node& node, Checked_t) const {
317 return extend(node, 0, Checked);
318 }
319
320 ///
321 /// Same as extend() with an `offset`, but returns a Result with the reason for a failure (eg,
322 /// an IndexSizeError if `offset` is past the end of `node`).
323 ///
324 /// @return Returns success. Fails with an InvalidStateError if nothing is selected, or an
325 /// IndexSizeError if `offset` is past the end of `node`.
326 ///
327 [[nodiscard]] Result<void> extend(const Node& node, size_t offset, Checked_t) const {
328 if (IsEmpty() || node.IsEmpty())
329 return detail::EmptyHandleError();
330 detail::ErrorScope error;
331 if (ulDOMSelectionExtend(handle_, node.raw(), offset, error.out()))
332 return {};
333 return Unexpected<Error>(error.TakeError());
334 }
335
336 ///
337 /// Set the anchor and the focus in one call (setBaseAndExtent).
338 ///
339 /// The focus can come before the anchor in the document.
340 ///
341 /// @param anchor_node The node for the anchor.
342 ///
343 /// @param anchor_offset The offset in `anchor_node`.
344 ///
345 /// @param focus_node The node for the focus.
346 ///
347 /// @param focus_offset The offset in `focus_node`.
348 ///
349 /// \parblock
350 /// @note If one node is empty, the selection becomes a caret at the other point. If both are
351 /// empty, the selection is cleared.
352 /// \endparblock
353 ///
354 /// \parblock
355 /// @note Unlike the web, an offset past the end of its node doesn't fail, so check offsets
356 /// yourself.
357 /// \endparblock
358 ///
359 void setBaseAndExtent(const Node& anchor_node, size_t anchor_offset, const Node& focus_node,
360 size_t focus_offset) const {
361 ulDOMSelectionSetBaseAndExtent(handle_, anchor_node.raw(), anchor_offset, focus_node.raw(),
362 focus_offset, nullptr);
363 }
364
365 ///
366 /// Same as setBaseAndExtent(), but returns a Result with the reason for a failure.
367 ///
368 /// @return Returns success (it fails only when this Selection is empty, or its page or a node's
369 /// page is gone).
370 ///
371 [[nodiscard]] Result<void> setBaseAndExtent(const Node& anchor_node,
372 size_t anchor_offset,
373 const Node& focus_node,
374 size_t focus_offset,
375 Checked_t) const {
376 if (IsEmpty())
377 return detail::EmptyHandleError();
378 detail::ErrorScope error;
379 if (ulDOMSelectionSetBaseAndExtent(handle_, anchor_node.raw(), anchor_offset,
380 focus_node.raw(), focus_offset, error.out()))
381 return {};
382 return Unexpected<Error>(error.TakeError());
383 }
384
385 ///
386 /// Select everything inside a node (selectAllChildren).
387 ///
388 /// @param node The node whose children to select. A text node has no children, so this places
389 /// a caret at its start.
390 ///
391 void selectAllChildren(const Node& node) const {
392 ulDOMSelectionSelectAllChildren(handle_, node.raw(), nullptr);
393 }
394
395 ///
396 /// Same as selectAllChildren(), but returns a Result with the reason for a failure.
397 ///
398 /// @return Returns success (it fails only when a handle is empty or its page is gone).
399 ///
400 [[nodiscard]] Result<void> selectAllChildren(const Node& node, Checked_t) const {
401 if (IsEmpty() || node.IsEmpty())
402 return detail::EmptyHandleError();
403 detail::ErrorScope error;
404 if (ulDOMSelectionSelectAllChildren(handle_, node.raw(), error.out()))
405 return {};
406 return Unexpected<Error>(error.TakeError());
407 }
408
409 ///
410 /// Clear the selection (removeAllRanges or empty).
411 ///
412 void removeAllRanges() const { ulDOMSelectionRemoveAllRanges(handle_); }
413
414 ///
415 /// Select a range (addRange).
416 ///
417 /// If nothing is selected, the selection moves to the range. If something is selected (even just
418 /// a caret), a range that overlaps it grows the selection to cover both, and any other range is
419 /// ignored. To replace the selection, call removeAllRanges() first.
420 ///
421 /// @param range The range to select.
422 ///
423 /// @note The selection takes a copy of the range's boundary points, so changing the range
424 /// later doesn't change the selection.
425 ///
426 void addRange(const Range& range) const { ulDOMSelectionAddRange(handle_, range.raw()); }
427
428 // --- Working with the selection ---------------------------------------------------------
429
430 ///
431 /// Get a range with the selection's start and end (getRangeAt).
432 ///
433 /// @param index The index of the range (always 0, since a selection has at most one range).
434 ///
435 /// @return Returns a new Range (empty if `index` isn't less than rangeCount()).
436 ///
437 /// @note Each call returns a new copy. Changing the Range doesn't change the selection.
438 ///
439 Range getRangeAt(size_t index) const {
440 return Range::Adopt(ulDOMSelectionGetRangeAt(handle_, index, nullptr));
441 }
442
443 ///
444 /// Same as getRangeAt(), but returns a Result with the reason for a failure (eg, an
445 /// IndexSizeError if `index` isn't less than rangeCount()).
446 ///
447 /// @return Returns a new Range. Fails with an IndexSizeError if `index` isn't less than
448 /// rangeCount().
449 ///
450 [[nodiscard]] Result<Range> getRangeAt(size_t index, Checked_t) const {
451 if (IsEmpty())
452 return detail::EmptyHandleError();
453 detail::ErrorScope error;
454 if (ULDOMRange range = ulDOMSelectionGetRangeAt(handle_, index, error.out()))
455 return Range::Adopt(range);
456 return Unexpected<Error>(error.TakeError());
457 }
458
459 ///
460 /// Remove the selected content from the page (deleteFromDocument).
461 ///
462 /// The selection collapses to where the content was.
463 ///
464 void deleteFromDocument() const { ulDOMSelectionDeleteFromDocument(handle_); }
465
466 ///
467 /// Whether or not a node is in the selection (containsNode).
468 ///
469 /// @param node The node to check.
470 ///
471 /// @param partial Pass true to count a node that's only partly selected.
472 ///
473 /// @return Returns true if the node is in the selection.
474 ///
475 /// @note A text node counts if any of its text is selected, even when `partial` is false.
476 ///
477 bool containsNode(const Node& node, bool partial = false) const {
478 return ulDOMSelectionContainsNode(handle_, node.raw(), partial);
479 }
480
481 ///
482 /// Move or extend the selection by a unit of text (modify), like the arrow keys do.
483 ///
484 /// This is the non-standard Selection.modify() that browsers support.
485 ///
486 /// @param alter `move` to move the caret or `extend` to grow the selection.
487 ///
488 /// @param direction `forward`, `backward`, `left`, or `right`.
489 ///
490 /// @param granularity `character`, `word`, `sentence`, `line`, `paragraph`, `lineboundary`,
491 /// `sentenceboundary`, `paragraphboundary`, or `documentboundary`.
492 ///
493 /// @note The values ignore case. A call with any other value does nothing.
494 ///
495 void modify(std::string_view alter, std::string_view direction,
496 std::string_view granularity) const {
497 detail::CString a(alter);
498 detail::CString d(direction);
499 detail::CString g(granularity);
500 ulDOMSelectionModify(handle_, a.c_str(), d.c_str(), g.c_str());
501 }
502
503 // --- Interop with the C API (most embedders never touch raw handles) -----------------------
504
505 ///
506 /// Wrap a C handle you own, taking ownership of it.
507 ///
508 /// @param handle A handle from the C API that you would otherwise destroy with
509 /// ulDestroyDOMSelection() (NULL gives an empty Selection).
510 ///
511 /// @return Returns a Selection that destroys `handle` when it's done.
512 ///
513 static Selection Adopt(ULDOMSelection handle) { return Selection(handle); }
514
515 ///
516 /// Wrap a C handle you don't own, adding a reference.
517 ///
518 /// @param handle The borrowed handle (NULL gives an empty Selection).
519 ///
520 /// @return Returns a Selection with its own reference, so you can keep it as long as you like.
521 ///
522 static Selection FromBorrowed(ULDOMSelection handle) {
523 return Selection(handle ? ulCreateDOMSelectionRef(handle) : nullptr);
524 }
525
526 ///
527 /// Get the C handle, for passing to the `<Ultralight/CAPI/CAPI_DOMSelection.h>` functions.
528 ///
529 /// @return Returns the handle (NULL for an empty Selection). This Selection still owns it, so
530 /// don't destroy it.
531 ///
532 ULDOMSelection raw() const { return handle_; }
533
534 ///
535 /// Give up ownership of the C handle and return it. This Selection becomes empty.
536 ///
537 /// @return Returns the handle. You must call ulDestroyDOMSelection() when finished.
538 ///
539 ULDOMSelection LeakRef() {
540 ULDOMSelection handle = handle_;
541 handle_ = nullptr;
542 return handle;
543 }
544
545 protected:
546 explicit Selection(ULDOMSelection handle) : handle_(handle) {}
547
548 ULDOMSelection handle_ = nullptr;
549};
550
552 return Selection::Adopt(ulDOMDocumentGetSelection(handle_));
553}
554
556 ULDOMDocument doc = ulDOMWindowGetDocument(handle_);
557 Selection selection = Selection::Adopt(ulDOMDocumentGetSelection(doc));
558 ulDestroyDOMDocument(doc);
559 return selection;
560}
561
562} // namespace dom
563} // namespace ultralight
struct C_DOMDocument * ULDOMDocument
Opaque handle to a page's DOM document.
Definition View.h:39
Selection getSelection() const
Get the document's selection (getSelection).
Definition Selection.h:551
A reference to an item in the DOM tree.
Definition Node.h:147
ULDOMNode raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMNode.h> functions.
Definition Node.h:539
static Node Adopt(ULDOMNode handle)
Wrap a C handle you own, taking ownership of it.
Definition Node.h:520
bool IsEmpty() const
Whether or not this Node is empty (it holds no handle).
Definition Node.h:216
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
ULDOMRange raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMRange.h> functions.
Definition Range.h:544
A handle to the active user selection or caret in a document.
Definition Selection.h:101
size_t focusOffset() const
Get the offset of the focus in focusNode() (focusOffset).
Definition Selection.h:183
void addRange(const Range &range) const
Select a range (addRange).
Definition Selection.h:426
void selectAllChildren(const Node &node) const
Select everything inside a node (selectAllChildren).
Definition Selection.h:391
Selection & operator=(Selection other) noexcept
Assignment (copies or moves).
Definition Selection.h:132
Result< void > selectAllChildren(const Node &node, Checked_t) const
Same as selectAllChildren(), but returns a Result with the reason for a failure.
Definition Selection.h:400
Result< void > collapse(const Node &node, Checked_t) const
Same as collapse(), but returns a Result with the reason for a failure.
Definition Selection.h:235
bool isCollapsed() const
Whether or not the selection is a caret (isCollapsed).
Definition Selection.h:191
void collapseToStart() const
Collapse the selection to its start (collapseToStart).
Definition Selection.h:257
std::string toString() const
Get the selected text (toString).
Definition Selection.h:210
Result< void > collapse(const Node &node, size_t offset, Checked_t) const
Same as collapse() with an offset, but returns a Result with the reason for a failure.
Definition Selection.h:245
bool containsNode(const Node &node, bool partial=false) const
Whether or not a node is in the selection (containsNode).
Definition Selection.h:477
size_t rangeCount() const
Get the number of ranges in the selection (rangeCount).
Definition Selection.h:198
void collapseToEnd() const
Collapse the selection to its end (collapseToEnd).
Definition Selection.h:277
Result< void > collapseToStart(Checked_t) const
Same as collapseToStart(), but returns a Result with the reason for a failure (eg,...
Definition Selection.h:265
void removeAllRanges() const
Clear the selection (removeAllRanges or empty).
Definition Selection.h:412
~Selection()
Destroy this handle (the selection itself isn't affected).
Definition Selection.h:140
Result< void > extend(const Node &node, Checked_t) const
Same as extend(), but returns a Result with the reason for a failure (eg, an InvalidStateError if not...
Definition Selection.h:316
Selection(const Selection &other)
Copy constructor (both handles refer to the same selection).
Definition Selection.h:113
Result< void > collapseToEnd(Checked_t) const
Same as collapseToEnd(), but returns a Result with the reason for a failure (eg, an InvalidStateError...
Definition Selection.h:285
ULDOMSelection raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMSelection.h> functions.
Definition Selection.h:532
Selection(Selection &&other) noexcept
Move constructor (other becomes empty).
Definition Selection.h:121
Range getRangeAt(size_t index) const
Get a range with the selection's start and end (getRangeAt).
Definition Selection.h:439
void modify(std::string_view alter, std::string_view direction, std::string_view granularity) const
Move or extend the selection by a unit of text (modify), like the arrow keys do.
Definition Selection.h:495
bool IsEmpty() const
Whether or not this Selection is empty (it holds no handle).
Definition Selection.h:150
bool IsAlive() const
Whether or not this Selection is valid (it isn't empty and its page is still alive).
Definition Selection.h:157
std::string type() const
Get the kind of selection (type).
Definition Selection.h:205
void collapse(const Node &node, size_t offset=0) const
Place a caret (collapse or setPosition).
Definition Selection.h:225
Result< void > extend(const Node &node, size_t offset, Checked_t) const
Same as extend() with an offset, but returns a Result with the reason for a failure (eg,...
Definition Selection.h:327
static Selection FromBorrowed(ULDOMSelection handle)
Wrap a C handle you don't own, adding a reference.
Definition Selection.h:522
static Selection Adopt(ULDOMSelection handle)
Wrap a C handle you own, taking ownership of it.
Definition Selection.h:513
void setBaseAndExtent(const Node &anchor_node, size_t anchor_offset, const Node &focus_node, size_t focus_offset) const
Set the anchor and the focus in one call (setBaseAndExtent).
Definition Selection.h:359
Node anchorNode() const
Get the node the selection starts in (anchorNode).
Definition Selection.h:166
Selection()
Create an empty Selection.
Definition Selection.h:106
Selection(ULDOMSelection handle)
Definition Selection.h:546
void deleteFromDocument() const
Remove the selected content from the page (deleteFromDocument).
Definition Selection.h:464
Result< void > setBaseAndExtent(const Node &anchor_node, size_t anchor_offset, const Node &focus_node, size_t focus_offset, Checked_t) const
Same as setBaseAndExtent(), but returns a Result with the reason for a failure.
Definition Selection.h:371
size_t anchorOffset() const
Get the offset of the anchor in anchorNode() (anchorOffset).
Definition Selection.h:171
void extend(const Node &node, size_t offset=0) const
Move the focus and keep the anchor where it is (extend).
Definition Selection.h:305
Node focusNode() const
Get the node the selection ends in (focusNode).
Definition Selection.h:178
ULDOMSelection handle_
Definition Selection.h:548
Result< Range > getRangeAt(size_t index, Checked_t) const
Same as getRangeAt(), but returns a Result with the reason for a failure (eg, an IndexSizeError if in...
Definition Selection.h:450
ULDOMSelection LeakRef()
Give up ownership of the C handle and return it.
Definition Selection.h:539
Selection getSelection() const
Get the page's selection (getSelection), the caret or the highlighted text.
Definition Selection.h:555
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
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.
The type of dom::Checked.
Definition Error.h:282