pub struct Document { /* private fields */ }Expand description
A document — the top-level owner of a node tree.
Owns all node data through parallel arenas. Internal subsystems
(layout, style, events) access data through DocumentCell.
The owner must keep Document at a stable address. Handles store
raw pointers back to this Document through DocumentCell.
Implementations§
Source§impl Document
impl Document
Sourcepub fn init_body(&mut self)
pub fn init_body(&mut self)
Initialize the <body> element. Called after Document is pinned
in memory (needs stable address for Handle operations).
Creates a real <body> element — UA stylesheet provides:
body { display: block; margin: 8px; }
Sourcepub fn computed_style(&self, index: u32) -> Option<Arc<ComputedValues>>
pub fn computed_style(&self, index: u32) -> Option<Arc<ComputedValues>>
Get the computed style for a node (from Stylo’s ElementData).
Returns None if styles haven’t been computed yet or for non-element nodes.
pub fn div(&self) -> HtmlDivElement
pub fn span(&self) -> HtmlSpanElement
pub fn p(&self) -> HtmlParagraphElement
pub fn img(&self) -> HtmlImageElement
pub fn input(&self) -> HtmlInputElement
pub fn label(&self) -> HtmlLabelElement
pub fn h1(&self) -> HtmlHeadingElement
pub fn h2(&self) -> HtmlHeadingElement
pub fn h3(&self) -> HtmlHeadingElement
pub fn text_node(&self, content: &str) -> Text
pub fn div_in(&self, parent: impl Into<Handle>) -> HtmlDivElement
pub fn span_in(&self, parent: impl Into<Handle>) -> HtmlSpanElement
pub fn text_in(&self, parent: impl Into<Handle>, content: &str) -> Text
pub fn create<T: Element>(&self) -> T
pub fn create_with_tag<T: Element>(&self, tag: &'static str) -> T
pub fn create_heading(&self, level: u8) -> HtmlHeadingElement
pub fn create_text(&self, content: &str) -> Text
pub fn root(&self) -> Handle
Sourcepub fn body(&self) -> Handle
pub fn body(&self) -> Handle
The <body> element — main content container.
Like Chrome’s document.body. Created by init_body() after the
Document is pinned in memory. Styles come from the UA stylesheet:
body { display: block; margin: 8px; }.
All user content goes in body(), not root().
pub fn resolve(&self, raw: RawId) -> Option<Handle>
pub fn node_count(&self) -> u32
pub fn root_index(&self) -> u32
Sourcepub fn needs_visual_update(&self) -> bool
pub fn needs_visual_update(&self) -> bool
Whether any pending changes need a visual update.
Chrome equivalent: Document::NeedsStyleRecalc() + layout dirty checks.
Used by the scheduler to request a frame after spawned tasks mutate the DOM.
Sourcepub fn handle_for_index(&self, index: u32) -> Option<Handle>
pub fn handle_for_index(&self, index: u32) -> Option<Handle>
Get a Handle for a node by its arena index.
Used by hit testing and event dispatch: the fragment tree stores only
the node index (u32), this recovers the full Handle needed for
event dispatch. Returns None if the index is dead or out of bounds.
Chrome equivalent: resolving a LayoutObject’s GetNode() pointer.
pub fn recalc_styles(&mut self)
Sourcepub fn add_stylesheet(&self, css: &str)
pub fn add_stylesheet(&self, css: &str)
Add a CSS stylesheet with full selector support.
Chrome equivalent: <style> element or document.adoptedStyleSheets.
Supports all CSS selectors: .class, #id, tag, [attr],
descendant, child, pseudo-classes — everything Stylo/Chrome supports.
doc.add_stylesheet(".card { background: red; border-radius: 8px; }");
doc.add_stylesheet(include_str!("../assets/dashboard.css"));Source§impl Document
impl Document
Sourcepub fn resolve_layout(
&mut self,
root: u32,
available_width: Option<f32>,
available_height: Option<f32>,
ctx: &LayoutContext<'_>,
) -> LayoutResult
pub fn resolve_layout( &mut self, root: u32, available_width: Option<f32>, available_height: Option<f32>, ctx: &LayoutContext<'_>, ) -> LayoutResult
Full layout resolve: flush styles → build children → compute → fragments.
Single entry point that replaces the old 4-step pipeline:
build_layout_tree_from_doc()→resolve_layout_children()sync_all_styles()→flush_styles_to_layout()compute_layout()→ Taffy runs against Document directlybuild_fragment_recursive()→ reads from Document
Convenience wrapper — always force-clears caches.
Sourcepub fn resolve_layout_dirty(
&mut self,
root: u32,
available_width: Option<f32>,
available_height: Option<f32>,
ctx: &LayoutContext<'_>,
layout_dirty: bool,
) -> LayoutResult
pub fn resolve_layout_dirty( &mut self, root: u32, available_width: Option<f32>, available_height: Option<f32>, ctx: &LayoutContext<'_>, layout_dirty: bool, ) -> LayoutResult
Full layout resolve with dirty flag control.
layout_dirty: true = something changed, clear all Taffy caches.
false = nothing changed, Taffy hits cache → ~0ms layout.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Document
impl !RefUnwindSafe for Document
impl !Send for Document
impl !Sync for Document
impl Unpin for Document
impl UnsafeUnpin for Document
impl !UnwindSafe for Document
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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