#[non_exhaustive]pub enum Node {
Element(Element),
Text(Markup),
Raw(Markup),
Comment(Markup),
Fragment(Vec<Node>),
}Expand description
A node in the HTML tree.
§Examples
use molt::prelude::*;
let node = Node::from(div().text("hi"));
assert_eq!(node.render(), "<div>hi</div>");Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Element(Element)
An element with a tag name, attributes and children.
Text(Markup)
Escaped text. The escaping already happened — see crate::core::escape.
Raw(Markup)
Verbatim markup, never escaped. For imported snippets: an SVG, an embed code.
Unlike Node::Fragment, this is an opaque string, so pretty printing can only
indent the whole blob. Reach for a fragment when you want the children indented.
Comment(Markup)
An HTML comment.
The content has -- neutralised so the comment cannot be closed early.
Fragment(Vec<Node>)
A transparent group that renders its children with no wrapper element.
Use it to return several nodes from one expression — a map of cards, the body of
an if, a group of <meta> tags — without introducing an extra <div>.
Implementations§
Source§impl Node
impl Node
Sourcepub fn raw(content: impl Into<String>) -> Self
pub fn raw(content: impl Into<String>) -> Self
Creates a raw markup node. The content is not escaped.
One of the three escape hatches, with Element::raw_text
and Attribute::raw; grep for the three to audit a
codebase. Use it for markup you wrote or already sanitised: an SVG icon, an embed
snippet. Never pass text a user typed. < in it becomes markup, </script> ends a
script, and nothing downstream will stop it.
For JSON in a <script> use seo::json_ld, which escapes it.
To show untrusted text, use Node::text.
§Examples
use molt::prelude::*;
let icon = Node::raw(r#"<svg viewBox="0 0 1 1"><path d="M0 0h1v1z"/></svg>"#);
assert!(icon.render().starts_with("<svg"));
// The same text through `Node::text` is shown, not interpreted.
assert_eq!(Node::text("<b>").render(), "<b>");Sourcepub fn comment(content: impl AsRef<str>) -> Self
pub fn comment(content: impl AsRef<str>) -> Self
Creates a comment node.
A space is put between every two adjacent - and after a trailing -, so no run of
two hyphens survives and the comment cannot terminate early and inject markup.
§Examples
use molt::prelude::*;
assert_eq!(Node::comment("a -- b").render(), "<!-- a - - b -->");
assert_eq!(Node::comment("x--->").render(), "<!-- x- - -> -->");Sourcepub fn fragment(children: impl IntoIterator<Item = Node>) -> Self
pub fn fragment(children: impl IntoIterator<Item = Node>) -> Self
Creates a transparent group of nodes.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether this node renders to nothing.
The pretty-print writer arrives at the same answer without calling this: it lets a
child write straight into the output and rolls the separator back if the child wrote
nothing, which is what stops an empty fragment — the body of a false if — from
leaving a blank line behind.
Trait Implementations§
impl Eq for Node
Source§impl<T: Into<Node>> From<Option<T>> for Node
None is an empty fragment, which renders to nothing and leaves no blank line.
impl<T: Into<Node>> From<Option<T>> for Node
None is an empty fragment, which renders to nothing and leaves no blank line.
Source§impl<N: Into<Node>> FromIterator<N> for Node
Collects nodes into a Node::Fragment, so .collect::<Node>() replaces
Node::fragment(iter).
impl<N: Into<Node>> FromIterator<N> for Node
Collects nodes into a Node::Fragment, so .collect::<Node>() replaces
Node::fragment(iter).
Source§fn from_iter<I: IntoIterator<Item = N>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = N>>(iter: I) -> Self
Source§impl Render for Node
impl Render for Node
Source§fn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize)
fn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize)
Source§fn render_pretty(&self) -> String
fn render_pretty(&self) -> String
Source§fn render_with(&self, options: &RenderOptions) -> String
fn render_with(&self, options: &RenderOptions) -> String
Auto Trait Implementations§
impl Freeze for Node
impl RefUnwindSafe for Node
impl Send for Node
impl Sync for Node
impl Unpin for Node
impl UnsafeUnpin for Node
impl UnwindSafe for Node
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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