[−][src]Struct marked::Document
A DOM-like container for a tree of markup elements and text.
Unlike RcDom
, this uses a simple vector of Node
s and indexes for
parent/child and sibling ordering. Attributes are stored as separately
allocated vectors for each element. For memory efficiency, a single
document is limited to 4 billion (2^32 - 1) total nodes.
All Document
instances, even logically "empty" ones as freshly
constructed, contain a synthetic document node at the fixed
DOCUMENT_NODE_ID
that serves as a container for N top level nodes,
including the root_element
if present.
Methods
impl Document
[src]
NodeRef
convenence accessor methods.
pub fn document_node_ref(&self) -> NodeRef
[src]
Return the (single, always present) document node as a NodeRef
.
pub fn root_element_ref(&self) -> Option<NodeRef>
[src]
Return the root element NodeRef
for this Document
, or None
if
there is no such qualified element.
A node with NodeData::Element
is a root element, if it is a direct
child of the document node, with no other element nor text sibling.
impl Document
[src]
Serialize convenience method.
pub fn serialize<W>(&self, writer: &mut W) -> Result<()> where
W: Write,
[src]
W: Write,
Serialize the contents of the document node and descendants in HTML syntax to the given stream.
impl Document
[src]
Mutating filter methods.
pub fn filter<F>(&mut self, f: F) where
F: Fn(NodeRef, &mut NodeData) -> Action,
[src]
F: Fn(NodeRef, &mut NodeData) -> Action,
Perform a depth-first (children before parent nodes) walk of the
entire Document
, including synthetic document node, applying the
provided function.
See Document::filter_at
for additional details.
pub fn filter_breadth<F>(&mut self, f: F) where
F: Fn(NodeRef, &mut NodeData) -> Action,
[src]
F: Fn(NodeRef, &mut NodeData) -> Action,
Perform a breadth-first (children after parent nodes) walk of the
entire Document
, including synthetic document node, applying the
provided function.
See Document::filter_at
for additional details.
pub fn filter_at<F>(&mut self, id: NodeId, f: F) where
F: Fn(NodeRef, &mut NodeData) -> Action,
[src]
F: Fn(NodeRef, &mut NodeData) -> Action,
Perform a depth-first (children before parent nodes) walk from the specified node ID, applying the provided function.
Traversal order
This variant performs a depth-first (children before parent nodes) tree
walk, but there is also Document::filter_at_breadth
, a
breadth-first (parent before children) variant. Filter functions such
as detach_banned_elements
may perform better breadth-first (but are
compatible with both traversal orders). Other functions such as
fold_empty_inline
and text_normalize
will only yield complete
results when run depth-first. See individual functions for
compatibility with traversal orders.
Filter functions
The f
parameter can be a closure or free-function in the form:
fn a_filter_fn(pos: NodeRef<'_>, data: &mut NodeData) -> Action;
Where data
provides read-write access to the the NodeData
of the
current node being visited, and pos
gives a read-only view to the
remainder of the Document
, e.g. parent, children, and siblings of the
current node. Note that to avoid aliasing issues, the NodeData
is
actually moved out of the Document
and replaced with a
NodeData::Hole
value which could be observed via pos
. The
potentially modified NodeData
is moved back to the Document
if the
function returns Action::Continue
. The function may also modify the
Document
by returning other Action
values.
For convenience and efficiency, multiple filter functions can be
combined via the chain_filters
macro and run in one pass.
Note that to free up all memory associated with filtered Node
s that
have been detached, use Document::deep_clone
and drop the original
Document.
.
pub fn filter_at_breadth<F>(&mut self, id: NodeId, f: F) where
F: Fn(NodeRef, &mut NodeData) -> Action,
[src]
F: Fn(NodeRef, &mut NodeData) -> Action,
Perform a breadth-first (children after parent nodes) walk from the specified node ID, applying the provided function.
See Document::filter_at
for additional details.
impl Document
[src]
Core implementation.
pub const DOCUMENT_NODE_ID: NodeId
[src]
The constant NodeId
for the document node of all Document
s.
pub fn new() -> Self
[src]
Construct a new Document
with the single empty document node.
pub fn root_element(&self) -> Option<NodeId>
[src]
Return the root element NodeId
for this Document, or None if there is
no such qualified element.
A node with NodeData::Element
is a root element, if it is a direct
child of the document node, with no other element or text sibling.
pub fn detach(&mut self, id: NodeId)
[src]
Detach the specified node ID.
Panics if called with the synthetic DOCUMENT_NODE_ID. Detaching the root element results in an empty document with no root element.
Detach just removes references from other nodes. To free up the memory
associated with the node and its children, use Document::deep_clone
and drop the original Document
.
pub fn append_child(&mut self, parent: NodeId, node: Node) -> NodeId
[src]
Append node as new last child of parent, and return its new ID.
pub fn insert_before_sibling(&mut self, sibling: NodeId, node: Node) -> NodeId
[src]
Insert node before the given sibling and return its new ID.
pub fn text(&self, id: NodeId) -> Option<StrTendril>
[src]
Return all decendent text content (character data) of the given node ID.
If node is a text node, return that text. If this is an element node
or the document node, return the concatentation of all text
descendants, in tree order. Return None
for all other node types.
pub fn children<'a>(&'a self, id: NodeId) -> impl Iterator<Item = NodeId> + 'a
[src]
Return an iterator over this node's direct children.
Will be empty if the node can not or does not have children.
pub fn node_and_following_siblings<'a>(
&'a self,
id: NodeId
) -> impl Iterator<Item = NodeId> + 'a
[src]
&'a self,
id: NodeId
) -> impl Iterator<Item = NodeId> + 'a
Return an iterator over the specified node and all its following, direct siblings, within the same parent.
pub fn node_and_ancestors<'a>(
&'a self,
id: NodeId
) -> impl Iterator<Item = NodeId> + 'a
[src]
&'a self,
id: NodeId
) -> impl Iterator<Item = NodeId> + 'a
Return an iterator over the specified node and all its ancestors, terminating at the document node.
pub fn nodes<'a>(&'a self) -> impl Iterator<Item = NodeId> + 'a
[src]
Return an iterator over all nodes, starting with the document node, and including all descendants in tree order.
pub fn deep_clone(&self, id: NodeId) -> Document
[src]
Create a new Document
from the ordered sub-tree rooted in the node
referenced by ID.
pub fn fold(&mut self, id: NodeId)
[src]
Replace the specified node ID with its children.
Panics if called with the synthetic DOCUMENT_NODE_ID. Folding the root
element may result in a Document
with no single root element, or
which is otherwise invalid based on its doctype, e.g. the HTML or XML
specifications.
After repositioning children the specified node is detached, which only
removes references. To free up the memory associated with the node, use
Document::deep_clone
and drop the original Document
. For a
node with no children, fold is equivalent to Document::detach
.
Trait Implementations
impl Debug for Document
[src]
impl Default for Document
[src]
impl Index<NodeId> for Document
[src]
impl IndexMut<NodeId> for Document
[src]
impl ToString for Document
[src]
Implemented via Document::serialize
.
Auto Trait Implementations
impl !RefUnwindSafe for Document
impl !Send for Document
impl !Sync for Document
impl Unpin for Document
impl UnwindSafe for Document
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,