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 winged_rust::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 attributes(&self) -> &[Attribute]
pub fn attributes(&self) -> &[Attribute]
The attributes, in insertion order.
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.
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.
Ports setAttribute(key:value:).
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 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. Winged-Swift’s dataAttributes(_:)
takes a Swift Dictionary, so its rendered attribute order is nondeterministic
between runs; this signature makes the output stable. See PORTING.md.
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. Tests/WingedSwiftTests/CSSHelpersTests.swift
has a regression test for 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 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
impl StructuralPartialEq for Element
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