Files
moli/moli-html2md/src/dom.rs
T

61 lines
2.7 KiB
Rust

/// The information needed to render a node. Text and tag names are borrowed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NodeKind<'a> {
/// A document or fragment whose children should be visited.
Document,
/// An element's normalized, lowercase HTML local name.
Element(&'a str),
/// Already decoded text, including CDATA when supported by the DOM.
Text(&'a str),
/// Comments, doctypes, processing instructions, and invalid node IDs.
Other,
}
/// A read-only view of an existing DOM.
///
/// IDs must describe a stable, acyclic tree for the duration of conversion.
/// Sibling chains must terminate. Attributes and text must already be decoded;
/// this crate neither parses HTML nor decodes entities a second time. No parent
/// lookup, allocation, interior mutation, or particular node representation is
/// required. Template contents and shadow roots are visited only if the adapter
/// exposes them as children.
pub trait Dom {
type NodeId: Copy;
fn node_kind(&self, node: Self::NodeId) -> NodeKind<'_>;
fn first_child(&self, node: Self::NodeId) -> Option<Self::NodeId>;
fn next_sibling(&self, node: Self::NodeId) -> Option<Self::NodeId>;
/// Iterate children in conversion order. Projected DOMs can override this
/// to resolve slot assignment once per parent, without a sibling index.
fn children(&self, node: Self::NodeId) -> impl Iterator<Item = Self::NodeId> {
std::iter::successors(self.first_child(node), move |node| self.next_sibling(*node))
}
fn attribute(&self, node: Self::NodeId, name: &str) -> Option<&str>;
/// Current input/textarea value supplied by an adapter. None uses the
/// source default; Some("") suppresses that default after a clear. Empty
/// text-entry controls do not emit their name as a field value.
fn control_value(&self, _node: Self::NodeId) -> Option<&str> {
None
}
/// A caller with computed CSS can retain block boundaries introduced by
/// stylesheets (for example, links rendered as separate action buttons).
/// Structural-only adapters can leave this at its default.
fn has_block_layout(&self, _node: Self::NodeId) -> bool {
false
}
/// A caller with computed CSS can retain boundaries between independent
/// inline boxes such as tabs, badges, and compact action controls.
/// Request a collapsible space on each side of an inline layout box.
fn has_text_boundary(&self, _node: Self::NodeId) -> bool {
false
}
/// Adapters that already inventory elements can skip fragment-link
/// collection when none exist. Table header references are still collected.
fn may_have_fragment_links(&self) -> bool {
true
}
}