Skip to main content

ScriptedDom

Struct ScriptedDom 

Source
pub struct ScriptedDom { /* private fields */ }
Expand description

A mutable DOM arena. Nodes live in a prunable [NodeStore] keyed by a monotonic id; a pruned id is “gone” (ids are never reused). Memory is bounded to live nodes by collect (G3), not to every node ever created.

Implementations§

Source§

impl ScriptedDom

Source

pub fn form_values(&self, root: NodeId) -> Vec<(String, String)>

Form-control values under root (inclusive), keyed by the control’s name, falling back to id. Covers <input>/<select> (the value attribute) and <textarea> (text content). Controls with neither a name nor an id are skipped — nothing identifies them in a freshly loaded page.

Source§

impl ScriptedDom

Source

pub fn outer_html(&self, node: NodeId) -> String

Serialize node and its subtree to HTML (outerHTML).

Routes through html5ever’s serializer, so void elements, attribute/text escaping, and raw-text elements are handled by the engine, not here.

Source

pub fn inner_html(&self, node: NodeId) -> String

Serialize only node’s children (innerHTML).

Source§

impl ScriptedDom

Source

pub fn new() -> Self

A fresh document with an empty Document root.

Source

pub fn capture_node_id(&self, id: NodeId) -> u64

Normalize id for capture/replay: strip any debug doc-tag fence and return the bare arena index the recorder writes.

Source

pub fn remint_node_id(&self, raw: u64) -> NodeId

Re-mint a captured bare arena index against this document, restoring this document’s tag on a fenced build.

Source

pub fn remove_child(&mut self, child: NodeId)

DOM removeChild: orphan child from its parent but keep it (and its subtree) alive and re-insertable, recording a DomMutation::Removed. Unlike LayoutDomMut::remove, which also drops the subtree — script may hold a reference to a removed node and re-insert it, so the scripted DOM orphans rather than frees.

Source

pub fn create_document(&mut self) -> NodeId

Create a detached Document node (a second document, for DOMImplementation.createDocument / createHTMLDocument). Lives in the same store as the primary document, so NodeIds stay globally unique. It is pin-kept, not a permanent root (G3): while script holds a reflector to it (or anything in it) the host pins it and collect spares the whole component; once script drops it, it collects like any other orphan.

Source

pub fn create_comment(&mut self, data: &str) -> NodeId

Create a detached Comment node carrying data.

Source

pub fn create_fragment(&mut self) -> NodeId

Create a detached DocumentFragment node (a parentless container).

Source

pub fn from_serialized_document(html: &str) -> Self

Rebuild a fresh document from serialized document children (the same shape inner_html(document()) emits for capture).

Source

pub fn import_serialized_subtree( &mut self, html: &str, ) -> Result<NodeId, String>

Import exactly one serialized subtree as a detached node.

Source

pub fn collect( &mut self, extra_roots: impl IntoIterator<Item = NodeId>, ) -> usize

Mark-sweep collection (G3 dangle contract). Prune every node not reachable — by undirected walk over parent + child edges — from a document root or one of extra_roots. extra_roots are the host’s live-reflector pins (ReflectorPins); passing them keeps a pinned orphan’s whole connected component alive (JS can walk parentNode up and children down and re-insert any of it). The DOM stays pin-agnostic — it takes extra roots, not reflector knowledge. Returns the number of nodes pruned.

Idempotent and cheap to call often: the host drives it at the drain_mutations boundary, on an idle tick, and right after an unpin.

Source

pub fn live_node_count(&self) -> usize

The number of live nodes currently in the store. Bounded by collect (G3), not by total nodes ever created — a diagnostic for the host and the regression signal for the bounded-memory churn test.

Source

pub fn stats(&self) -> DomArenaStats

Cheap live counts plus a rough byte estimate for the DOM arena.

Trait Implementations§

Source§

impl Default for ScriptedDom

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl LayoutDom for ScriptedDom

Source§

fn is_live(&self, id: NodeId) -> bool

The dangle-contract liveness check (see LayoutDom::is_live). Live iff the id belongs to this document and still has an entry in the store (attached or orphaned-but-kept); a dropped or collected node has no entry. Never panics, unlike the read accessors.

Source§

type NodeId = NodeId

Opaque per-backend node identity. Must be Copy for cheap pass-through.
Source§

fn document(&self) -> NodeId

The document root. Read more
Source§

fn parent(&self, id: NodeId) -> Option<NodeId>

Parent node, if any.
Source§

fn prev_sibling(&self, id: NodeId) -> Option<NodeId>

Previous sibling in DOM order. Hot on selector-matching paths (prev_sibling_element in selectors::Element); deriving it from dom_children(parent) would be O(siblings) per call.
Source§

fn next_sibling(&self, id: NodeId) -> Option<NodeId>

Next sibling in DOM order. See Self::prev_sibling.
Source§

fn dom_children(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_

DOM-tree children (parse-order, ignores shadow trees).
Source§

fn kind(&self, id: NodeId) -> NodeKind

What kind of node id is. Plain enum; details via the typed accessors below.
Source§

fn opaque_id(&self, id: NodeId) -> u64

Stable per-node identity as a u64. Used by foreign trait adapters (Stylo’s OpaqueNode, selectors::OpaqueElement) that need a pointer-shaped value for identity comparisons in the cascade. Read more
Source§

fn element_name(&self, id: NodeId) -> Option<&QualName>

Element name when id is an element, else None. Hot on selector/style match paths.
Source§

fn attribute( &self, id: NodeId, ns: &Namespace, local: &LocalName, ) -> Option<&str>

Attribute value lookup by namespace + local name. Hot on selector/style match paths. Backends with column-stored attrs can implement this as a keyed lookup without materializing a full slice.
Source§

fn attributes(&self, id: NodeId) -> impl Iterator<Item = AttributeView<'_>> + '_

Iterate this element’s attributes (cold path: serialization, introspection). Yields AttributeViews borrowed from the backing store.
Source§

fn text(&self, id: NodeId) -> Option<&str>

Text content for text or comment nodes, else None.
Source§

fn quirks_mode(&self) -> QuirksMode

The document’s quirks mode, as selected by the parser (presence/absence of a <!DOCTYPE>). Drives quirk-gated cascade behaviour (Stylo’s QuirksMode-conditional UA rules, e.g. the table font-size quirk). Defaults to standards mode; a backend that parses a real document overrides it.
Source§

fn flat_children(&self, id: Self::NodeId) -> impl Iterator<Item = Self::NodeId>

Flat-tree children (slot-assigned for shadow hosts, otherwise DOM order). Backends without shadow DOM should leave this defaulted.
Source§

fn walk<V>( &self, visitor: &mut V, ) -> ControlFlow<<V as NodeVisitor<Self>>::Stop>
where V: NodeVisitor<Self> + ?Sized,

Walk the whole document from document(), descending via dom_children. Backends override when they want backend-driven traversal (parallel layout pass, prefetching, flat-tree descent).
Source§

fn has_class(&self, id: Self::NodeId, class: &str) -> bool

Whether element id carries CSS class class (whitespace-split class attr).
Source§

fn first_with_class( &self, id: Self::NodeId, class: &str, ) -> Option<Self::NodeId>

The first element carrying CSS class class in pre-order under id (inclusive).
Source§

fn all_with_class(&self, id: Self::NodeId, class: &str) -> Vec<Self::NodeId>

Every element carrying CSS class class in pre-order under id (inclusive).
Source§

fn first_tag(&self, id: Self::NodeId, local: &str) -> Option<Self::NodeId>

The first element with local tag name local in pre-order under id (inclusive).
Source§

impl LayoutDomMut for ScriptedDom

Source§

fn create_element(&mut self, name: QualName) -> NodeId

Create a detached element node (no parent until appended).
Source§

fn create_text(&mut self, data: &str) -> NodeId

Create a detached text node.
Source§

fn append_child(&mut self, parent: NodeId, child: NodeId)

Append child as the last child of parent, detaching it from any previous parent first.
Source§

fn insert_before( &mut self, parent: NodeId, child: NodeId, reference: Option<NodeId>, )

Insert child immediately before reference among parent’s children, detaching child from any previous parent first. Appends if reference is None, or if reference is not a child of parent (defensive: the DOM insertBefore throws in that case, but the layout-side contract stays total). The ordered-insertion primitive a reactive differ needs; append_child is the reference == None tail case.
Source§

fn move_before( &mut self, parent: NodeId, child: NodeId, reference: Option<NodeId>, )

Atomically move an in-tree child under parent before reference (append when None), preserving subtree state — the Node.moveBefore() contract (WHATWG DOM; docs/2026-07-05_movebefore_dom_standard_plan.md). Records one DomMutation::Moved instead of the Removed + Inserted pair insert_before produces for an in-tree node, so consumers may keep the subtree’s retained state. A move resolving to the current position records nothing; a disconnected child degrades to a plain insert (the DOM-level moveBefore throws there; this layout-side contract stays total, like insert_before’s bad-reference fallback).
Source§

fn remove(&mut self, node: NodeId)

Detach node from its parent and drop its subtree.
Source§

fn set_attribute(&mut self, node: NodeId, name: QualName, value: &str)

Set (or replace) an attribute on an element.
Source§

fn remove_attribute(&mut self, node: NodeId, name: QualName)

Remove the attribute named name from node (no-op if absent). Records an DomMutation::AttributeChanged carrying the removed value as old_value (the live DOM then reads as absent), so serval-layout builds the Stylo snapshot the same way it does for a value change.
Source§

fn set_text(&mut self, node: NodeId, data: &str)

Replace a text/comment node’s character data.
Source§

fn set_inner_html(&mut self, node: NodeId, html: &str)

Replace node’s children with the subtree parsed from an HTML fragment (the innerHTML setter). Records a single DomMutation::SubtreeReplaced.
Source§

fn drain_mutations(&mut self, out: &mut Vec<DomMutation<NodeId>>)

Drain the structural mutations recorded since the last call into out. The provider records WHAT changed; serval-layout decides what to invalidate.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.