pub struct Element { /* private fields */ }Expand description
An HTML element: a tag name, attributes, optional text content, and children.
Every builder method takes self and returns Self, so they chain. Content and
attribute values are escaped as they go in, not when the element is rendered.
§Examples
use molt::prelude::*;
let card = div()
.add_class("card")
.set_id("hero")
.child(h1().text("Welcome"))
.child(p().text("Fuel, tyres & chain."));
assert_eq!(
card.render(),
r#"<div class="card" id="hero"><h1>Welcome</h1><p>Fuel, tyres & chain.</p></div>"#
);Implementations§
Source§impl Element
impl Element
Sourcepub fn from_static(tag: &'static str) -> Self
pub fn from_static(tag: &'static str) -> Self
Creates an element whose tag is a 'static string, without allocating it.
What the built-in constructors (div(), p(), …) use. Reach for it when you define
your own element helpers with a literal tag, such as a custom element.
§Examples
use molt::prelude::*;
assert_eq!(Element::from_static("my-card").text("hi").render(), "<my-card>hi</my-card>");Sourcepub fn attributes(&self) -> &[Attribute]
pub fn attributes(&self) -> &[Attribute]
The attributes, in insertion order.
Sourcepub fn is_self_closing(&self) -> bool
pub fn is_self_closing(&self) -> bool
Whether this element closes without an end tag, dropping content and children.
Void tags do this on their own. Self::self_closing opts any other tag into the
same shape.
§Examples
use molt::prelude::*;
assert!(!div().is_self_closing());
assert!(div().self_closing(true).is_self_closing());Sourcepub fn text(self, content: impl AsRef<str>) -> Self
pub fn text(self, content: impl AsRef<str>) -> Self
Sets the text content, escaping it.
Replaces any content already set.
Sourcepub fn raw_text(self, content: impl Into<String>) -> Self
pub fn raw_text(self, content: impl Into<String>) -> Self
Sets the text content without escaping it.
The documented escape hatch for markup you already trust. <script> and <style>
use this by default.
Do not put untrusted data in a <script> or <style> through this. The element
ends at the first </script (or </style) wherever it appears, including inside a
JSON string. For JSON data use seo::json_ld or
seo::script_json, which escape it; there is no safe way to
embed untrusted text in script code.
Sourcepub fn children_from<N: Into<Node>>(
self,
children: impl IntoIterator<Item = N>,
) -> Self
pub fn children_from<N: Into<Node>>( self, children: impl IntoIterator<Item = N>, ) -> Self
Appends several child nodes.
Sourcepub fn add_attribute(self, attribute: Attribute) -> Self
pub fn add_attribute(self, attribute: Attribute) -> Self
Appends an attribute. Does not deduplicate.
Sourcepub fn attr(self, key: impl Into<String>, value: impl AsRef<str>) -> Self
pub fn attr(self, key: impl Into<String>, value: impl AsRef<str>) -> Self
Appends key="value", escaping the value. Does not deduplicate.
Sourcepub fn bool_attr(self, key: impl Into<String>) -> Self
pub fn bool_attr(self, key: impl Into<String>) -> Self
Appends a boolean attribute, which renders as a bare key.
Sourcepub fn trusted_url(self, key: impl Into<String>, value: impl AsRef<str>) -> Self
pub fn trusted_url(self, key: impl Into<String>, value: impl AsRef<str>) -> Self
Replaces key with a URL that is written as given, even if it is a script URL.
The value is still attribute-escaped. Use this only for a URL you wrote yourself.
§Examples
use molt::prelude::*;
assert_eq!(
a().attr("href", "javascript:x")
.trusted_url("href", "javascript:print()")
.render(),
"<a href=\"javascript:print()\"></a>"
);Sourcepub fn self_closing(self, yes: bool) -> Self
pub fn self_closing(self, yes: bool) -> Self
Closes this element without an end tag, and does not render its content or children.
true emits > or, when RenderOptions::xhtml_self_closing is set, />, even
when the tag is not void. The default is false, so existing markup is unchanged.
Attributes are still written, which is what keeps the URL policy in front of a tag
that would otherwise have been emitted as raw markup.
§Examples
use molt::core::RenderOptions;
use molt::prelude::*;
let field = div().attr("id", "x").self_closing(true).child(span().text("no"));
assert_eq!(field.render(), "<div id=\"x\">");
assert_eq!(
field.render_with(&RenderOptions::compact().with_xhtml_self_closing(true)),
"<div id=\"x\" />"
);Sourcepub fn data_attr(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self
pub fn data_attr(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self
Appends data-{key}="{value}".
Sourcepub fn data_attrs<K: AsRef<str>, V: AsRef<str>>(
self,
data: impl IntoIterator<Item = (K, V)>,
) -> Self
pub fn data_attrs<K: AsRef<str>, V: AsRef<str>>( self, data: impl IntoIterator<Item = (K, V)>, ) -> Self
Appends several data-* attributes.
Takes an ordered iterator rather than a map: a hash map would render its attributes in a different order from one run to the next, and this makes the output stable.
Sourcepub fn aria_attr(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self
pub fn aria_attr(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self
Appends aria-{key}="{value}".
Sourcepub fn aria_attrs<K: AsRef<str>, V: AsRef<str>>(
self,
aria: impl IntoIterator<Item = (K, V)>,
) -> Self
pub fn aria_attrs<K: AsRef<str>, V: AsRef<str>>( self, aria: impl IntoIterator<Item = (K, V)>, ) -> Self
Appends several aria-* attributes, in the order given.
Sourcepub fn add_class(self, class_name: impl AsRef<str>) -> Self
pub fn add_class(self, class_name: impl AsRef<str>) -> Self
Appends a class name to class, space-joined.
Only the new value is escaped — the existing attribute is already escaped, and re-escaping it would double-encode, and a test pins exactly that.
Sourcepub fn add_classes<S: AsRef<str>>(
self,
class_names: impl IntoIterator<Item = S>,
) -> Self
pub fn add_classes<S: AsRef<str>>( self, class_names: impl IntoIterator<Item = S>, ) -> Self
Appends several class names.
Trait Implementations§
Source§impl Drop for Element
Tears the subtree down iteratively.
impl Drop for Element
Tears the subtree down iteratively.
The derived drop glue recurses once per nesting level, so a tree deep enough to need the iterative writer would abort while being freed instead — after rendering fine. Draining into an explicit worklist keeps teardown flat.
Each node has its children moved out before it goes out of scope, so the Drop that
runs for it finds nothing left to recurse into.
impl Eq for Element
Source§impl<N: Into<Node>> Extend<N> for Element
Appends nodes as children, so element.extend(items) works beside children_from.
impl<N: Into<Node>> Extend<N> for Element
Appends nodes as children, so element.extend(items) works beside children_from.
Source§fn extend<I: IntoIterator<Item = N>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = N>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Render for Element
impl Render for Element
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 Element
impl RefUnwindSafe for Element
impl Send for Element
impl Sync for Element
impl Unpin for Element
impl UnsafeUnpin for Element
impl UnwindSafe for Element
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