rdom-core 0.4.0

Arena-backed DOM for Rust — pure tree + events, no renderer. Can be used headlessly or paired with rdom-tui for terminal rendering.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! `NodeRef<'a, Ext>` / `NodeMut<'a, Ext>` — ergonomic wrappers around
//! `(&Dom, NodeId)` / `(&mut Dom, NodeId)` pairs so call sites read DOM-ish:
//!
//! ```ignore
//! dom.node(row).get_attribute("data-sel");
//! dom.node_mut(hero).set_attribute("role", "banner");
//! ```

use crate::dom::Dom;
use crate::dom_string_map::{DomStringMap, DomStringMapMut};
use crate::error::{DomError, Result};
use crate::node::{NodeData, NodeType};
use crate::node_id::NodeId;
use crate::node_list::NodeList;
use crate::node_or_string::NodeOrString;
use crate::token_list::{DomTokenList, DomTokenListMut};
use crate::tree::AdjacentPosition;

// ─────────────────────────────────────────────────────────────────────
//  NodeRef
// ─────────────────────────────────────────────────────────────────────

/// Read-only handle to a node in the arena.
#[derive(Clone, Copy)]
pub struct NodeRef<'a, Ext: 'static = ()> {
    pub(crate) dom: &'a Dom<Ext>,
    pub(crate) id: NodeId,
}

impl<'a, Ext: 'static> NodeRef<'a, Ext> {
    // ── Identity ──────────────────────────────────────────────────

    pub fn id(&self) -> NodeId {
        self.id
    }

    /// Borrow the owning `Dom<Ext>`. Lifetime `'a` matches the
    /// underlying borrow that produced this `NodeRef`, so callers can
    /// hold the returned reference for the same scope.
    ///
    /// Exposed for extension traits (e.g. `rdom-tui::TuiAccessors`)
    /// that need to reach back to dom-level helpers operating on
    /// `(dom, id)` pairs (runtime focus, builtin form helpers, etc.).
    pub fn dom(&self) -> &'a Dom<Ext> {
        self.dom
    }

    /// # Panics
    ///
    /// Panics if the id behind this `NodeRef` is not live (freed, or a
    /// stale handle to a recycled slot). `Dom::node` does not validate
    /// up front; check `Dom::contains` first when the id may be stale.
    pub fn node_type(&self) -> NodeType {
        self.dom
            .get_node(self.id)
            .map(|n| n.node_type())
            .unwrap_or_else(|| panic!("NodeRef::node_type on a node that is not live: {}", self.id))
    }

    /// Canonical `nodeName`: element tag, or `#text` / `#comment` /
    /// `#document-fragment` for non-elements.
    ///
    /// # Panics
    ///
    /// Panics if the id is not live; see [`NodeRef::node_type`].
    pub fn node_name(&self) -> &'a str {
        let n = self.dom.get_node(self.id).unwrap_or_else(|| {
            panic!("NodeRef::node_name on a node that is not live: {}", self.id)
        });
        match &n.data {
            NodeData::Element { tag, .. } => tag,
            NodeData::Text { .. } => "#text",
            NodeData::Comment { .. } => "#comment",
            NodeData::Fragment => "#document-fragment",
        }
    }

    pub fn tag_name(&self) -> Option<&'a str> {
        self.dom.get_node(self.id).and_then(|n| n.tag_name())
    }

    /// Borrow the per-element extension data.
    ///
    /// This is the hook by which rdom-tui (or any downstream crate
    /// parameterizing `Dom<Ext>`) reads presentation / layout / styling
    /// state attached to each Element. Returns `None` for Text / Comment /
    /// Fragment nodes — those don't carry `Ext`.
    pub fn ext(&self) -> Option<&'a Ext> {
        match &self.dom.get_node(self.id)?.data {
            NodeData::Element { ext, .. } => Some(ext),
            _ => None,
        }
    }

    pub fn node_value(&self) -> Option<&'a str> {
        match &self.dom.get_node(self.id)?.data {
            NodeData::Text { data } | NodeData::Comment { data } => Some(data),
            _ => None,
        }
    }

    /// `CharacterData.data` — MDN alias for Text/Comment `nodeValue`. Same
    /// behaviour, different name; exists because the spec treats these as
    /// two different interface members.
    pub fn data(&self) -> Option<&'a str> {
        self.node_value()
    }

    /// `textContent` — concatenate the string content of this node and all
    /// its descendants. Comments are excluded per spec.
    pub fn text_content(&self) -> String {
        self.dom.text_content(self.id)
    }

    // ── Tree: core navigation ─────────────────────────────────────

    pub fn parent_node(&self) -> Option<NodeRef<'a, Ext>> {
        let p = self.dom.get_node(self.id)?.parent?;
        Some(NodeRef {
            dom: self.dom,
            id: p,
        })
    }

    /// Parent that is an element (skips fragment parents).
    pub fn parent_element(&self) -> Option<NodeRef<'a, Ext>> {
        let mut current = self.parent_node();
        while let Some(p) = current {
            if p.node_type() == NodeType::Element {
                return Some(p);
            }
            current = p.parent_node();
        }
        None
    }

    pub fn first_child(&self) -> Option<NodeRef<'a, Ext>> {
        let f = self.dom.get_node(self.id)?.first_child?;
        Some(NodeRef {
            dom: self.dom,
            id: f,
        })
    }

    pub fn last_child(&self) -> Option<NodeRef<'a, Ext>> {
        let l = self.dom.get_node(self.id)?.last_child?;
        Some(NodeRef {
            dom: self.dom,
            id: l,
        })
    }

    pub fn previous_sibling(&self) -> Option<NodeRef<'a, Ext>> {
        let p = self.dom.get_node(self.id)?.prev_sibling?;
        Some(NodeRef {
            dom: self.dom,
            id: p,
        })
    }

    pub fn next_sibling(&self) -> Option<NodeRef<'a, Ext>> {
        let n = self.dom.get_node(self.id)?.next_sibling?;
        Some(NodeRef {
            dom: self.dom,
            id: n,
        })
    }

    pub fn has_child_nodes(&self) -> bool {
        self.dom
            .get_node(self.id)
            .and_then(|n| n.first_child)
            .is_some()
    }

    pub fn child_nodes(&self) -> ChildIter<'a, Ext> {
        ChildIter {
            dom: self.dom,
            next: self.dom.get_node(self.id).and_then(|n| n.first_child),
        }
    }

    // ── Element-only navigation ────────────────────────────────────

    pub fn first_element_child(&self) -> Option<NodeRef<'a, Ext>> {
        let mut c = self.first_child();
        while let Some(n) = c {
            if n.node_type() == NodeType::Element {
                return Some(n);
            }
            c = n.next_sibling();
        }
        None
    }

    pub fn last_element_child(&self) -> Option<NodeRef<'a, Ext>> {
        let mut c = self.last_child();
        while let Some(n) = c {
            if n.node_type() == NodeType::Element {
                return Some(n);
            }
            c = n.previous_sibling();
        }
        None
    }

    pub fn previous_element_sibling(&self) -> Option<NodeRef<'a, Ext>> {
        let mut s = self.previous_sibling();
        while let Some(n) = s {
            if n.node_type() == NodeType::Element {
                return Some(n);
            }
            s = n.previous_sibling();
        }
        None
    }

    pub fn next_element_sibling(&self) -> Option<NodeRef<'a, Ext>> {
        let mut s = self.next_sibling();
        while let Some(n) = s {
            if n.node_type() == NodeType::Element {
                return Some(n);
            }
            s = n.next_sibling();
        }
        None
    }

    pub fn children(&self) -> ElementChildIter<'a, Ext> {
        ElementChildIter {
            inner: self.child_nodes(),
        }
    }

    pub fn child_element_count(&self) -> usize {
        self.children().count()
    }

    // ── Attributes / classes ──────────────────────────────────────

    pub fn id_attr(&self) -> Option<&'a str> {
        self.get_attribute("id")
    }

    pub fn get_attribute(&self, key: &str) -> Option<&'a str> {
        self.dom.get_attribute(self.id, key)
    }

    pub fn has_attribute(&self, key: &str) -> bool {
        self.dom.has_attribute(self.id, key)
    }

    pub fn has_class(&self, class: &str) -> bool {
        self.dom.has_class(self.id, class)
    }

    /// Iterate `(name, value)` pairs in deterministic order.
    pub fn attributes(&self) -> impl Iterator<Item = (&'a str, &'a str)> {
        self.dom.attributes(self.id)
    }

    /// Raw `class` attribute value, or `""` if absent. DOM
    /// `Element.className`.
    ///
    /// This is the unparsed string. For token-level access use
    /// [`Self::class_list`]; for hot-path membership tests use
    /// [`Self::has_class`].
    pub fn class_name(&self) -> &'a str {
        self.get_attribute("class").unwrap_or("")
    }

    /// Snapshot of the element's class tokens. DOM
    /// `Element.classList`.
    ///
    /// **Hot-path footgun.** Each call allocates a fresh `Vec`
    /// snapshot. For per-paint or per-event membership checks,
    /// prefer [`Self::has_class`].
    pub fn class_list(&self) -> DomTokenList {
        DomTokenList::from_tokens(self.dom.class_list(self.id).map(str::to_owned))
    }

    // ── Predicates ────────────────────────────────────────────────

    pub fn contains(&self, other: NodeId) -> bool {
        self.dom.is_ancestor(self.id, other)
    }

    pub fn is_same_node(&self, other: NodeId) -> bool {
        self.id == other
    }

    /// `true` iff this node is reachable from the document root by
    /// walking parent pointers. DOM `Node.isConnected`.
    pub fn is_connected(&self) -> bool {
        self.dom.is_ancestor(self.dom.root(), self.id)
    }

    /// Walk parent pointers until none and return the topmost node.
    /// DOM `Node.getRootNode()` (options ignored — no shadow DOM in
    /// rdom).
    pub fn get_root_node(&self) -> NodeRef<'a, Ext> {
        let mut cur = self.id;
        loop {
            match self.dom.get_node(cur).and_then(|n| n.parent) {
                Some(p) => cur = p,
                None => {
                    return NodeRef {
                        dom: self.dom,
                        id: cur,
                    };
                }
            }
        }
    }

    // ── Selector queries (element-rooted) ─────────────────────────

    /// Does this element match `selector`? DOM `Element.matches`.
    ///
    /// **Divergence from browser:** browser throws `SyntaxError` on
    /// malformed selectors; rdom returns `false`. Authors who need
    /// to surface parser errors can call
    /// [`Dom::matches`](crate::Dom::matches) directly.
    pub fn matches(&self, selector: &str) -> bool {
        self.dom.matches(self.id, selector).unwrap_or(false)
    }

    /// Walk from this node (inclusive) up the ancestor chain and
    /// return the first match. DOM `Element.closest`.
    ///
    /// Same parser-error policy as [`Self::matches`].
    pub fn closest(&self, selector: &str) -> Option<NodeRef<'a, Ext>> {
        self.dom
            .closest(self.id, selector)
            .ok()
            .flatten()
            .map(|id| NodeRef { dom: self.dom, id })
    }

    /// First descendant matching `selector`, in document order. DOM
    /// `Element.querySelector`. The subject element itself is **not**
    /// a candidate (spec).
    ///
    /// Same parser-error policy as [`Self::matches`].
    pub fn query_selector(&self, selector: &str) -> Option<NodeRef<'a, Ext>> {
        self.dom
            .query_selector_in(self.id, selector)
            .ok()
            .flatten()
            .filter(|&id| id != self.id)
            .map(|id| NodeRef { dom: self.dom, id })
    }

    /// All descendants matching `selector`, in document order, as a
    /// snapshot `NodeList`. DOM `Element.querySelectorAll`. The
    /// subject element itself is **not** a candidate.
    ///
    /// Same parser-error policy as [`Self::matches`]; an unparseable
    /// selector yields an empty list.
    pub fn query_selector_all(&self, selector: &str) -> NodeList<'a, Ext> {
        let ids = self
            .dom
            .query_selector_all_in(self.id, selector)
            .unwrap_or_default()
            .into_iter()
            .filter(|&id| id != self.id);
        NodeList::from_ids(self.dom, ids)
    }

    // ── HTMLElement IDL accessors (rdom-core, raw shapes) ─────────

    /// DOM `HTMLElement.dataset` — snapshot view of `data-*`
    /// attributes keyed by their camelCase form.
    pub fn dataset(&self) -> DomStringMap<'a, Ext> {
        DomStringMap::new(NodeRef {
            dom: self.dom,
            id: self.id,
        })
    }

    /// DOM `HTMLElement.tabIndex` — raw `tabindex` attribute
    /// parsed as `i32`. `None` when the attribute is absent or
    /// unparseable.
    ///
    /// **Note:** this is the *raw* value. The TUI-aware effective
    /// tab index (honoring implicit focusability per
    /// `runtime::focus::tabindex`) lives on
    /// `rdom_tui::TuiAccessors::effective_tab_index` per §13 and
    /// ships in M4b step 21.
    pub fn tab_index(&self) -> Option<i32> {
        self.get_attribute("tabindex")?.parse().ok()
    }

    /// DOM `HTMLElement.hidden` — `true` iff the `hidden` boolean
    /// attribute is present.
    pub fn hidden(&self) -> bool {
        self.has_attribute("hidden")
    }

    /// DOM `HTMLElement.contentEditable` — IDL string. Returns the
    /// raw attribute when set, else `"inherit"` per spec.
    pub fn content_editable(&self) -> &'a str {
        self.get_attribute("contenteditable").unwrap_or("inherit")
    }

    /// DOM `Element.innerHTML` getter — markup of this element's
    /// children. Delegates to `Dom::inner_markup`.
    ///
    /// **Hot-path footgun.** Each call re-serializes the entire
    /// subtree; for tight loops, prefer attribute / child walks
    /// against the live tree.
    pub fn inner_html(&self) -> String {
        self.dom.inner_markup(self.id)
    }

    /// DOM `Element.outerHTML` getter — markup of this element
    /// including itself. Delegates to `Dom::outer_markup`.
    pub fn outer_html(&self) -> String {
        self.dom.outer_markup(self.id)
    }
}

mod node_mut;
pub use node_mut::NodeMut;

// ─────────────────────────────────────────────────────────────────────
//  Iterators
// ─────────────────────────────────────────────────────────────────────

/// Iterator over all direct children (any node type), in document order.
pub struct ChildIter<'a, Ext: 'static> {
    dom: &'a Dom<Ext>,
    next: Option<NodeId>,
}

impl<'a, Ext: 'static> Iterator for ChildIter<'a, Ext> {
    type Item = NodeRef<'a, Ext>;
    fn next(&mut self) -> Option<Self::Item> {
        let current = self.next?;
        self.next = self.dom.get_node(current).and_then(|n| n.next_sibling);
        Some(NodeRef {
            dom: self.dom,
            id: current,
        })
    }
}

/// Iterator over element children only.
pub struct ElementChildIter<'a, Ext: 'static> {
    inner: ChildIter<'a, Ext>,
}

impl<'a, Ext: 'static> Iterator for ElementChildIter<'a, Ext> {
    type Item = NodeRef<'a, Ext>;
    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .by_ref()
            .find(|n| n.node_type() == NodeType::Element)
    }
}

// ─────────────────────────────────────────────────────────────────────
//  Dom -> accessor helpers
// ─────────────────────────────────────────────────────────────────────

impl<Ext> Dom<Ext> {
    /// Read accessor for `id`. Construction does not validate the id:
    /// most `NodeRef` methods return `None` / empty for a dead id, but
    /// the ones that have no empty value (`node_type`, `node_name`)
    /// panic. Check [`Dom::contains`] first when the id may be stale.
    pub fn node(&self, id: NodeId) -> NodeRef<'_, Ext> {
        NodeRef { dom: self, id }
    }

    /// Mutable accessor for `id`. Same validation contract as
    /// [`Dom::node`]: mutation methods return `Err(InvalidNode)` for a
    /// dead id.
    pub fn node_mut(&mut self, id: NodeId) -> NodeMut<'_, Ext> {
        NodeMut { dom: self, id }
    }

    /// Convenience: `NodeRef` for the root.
    pub fn root_ref(&self) -> NodeRef<'_, Ext> {
        self.node(self.root())
    }
}

#[cfg(test)]
mod tests;