pub struct Element { /* private fields */ }
Implementations§
Source§impl Element
impl Element
pub fn new(tag: impl ToString) -> Self
pub fn class(self, cls: impl ToString) -> Self
pub fn class_r(&mut self, cls: impl ToString) -> &mut Self
pub fn flag(self, key: impl ToString) -> Self
pub fn flag_r(&mut self, key: impl ToString) -> &mut Self
pub fn attr(self, key: impl ToString, value: impl AsRef<str>) -> Self
pub fn attr_r( &mut self, key: impl ToString, value: impl AsRef<str>, ) -> &mut Self
pub fn data_attr(self, key: impl ToString, value: impl AsRef<str>) -> Self
pub fn data_attr_r( &mut self, key: impl ToString, value: impl AsRef<str>, ) -> &mut Self
pub fn data_flag(self, key: impl ToString) -> Self
pub fn data_flag_r(&mut self, key: impl ToString) -> &mut Self
Source§impl Element
impl Element
Sourcepub fn map_if<F>(self, flag: bool, f: F) -> Selfwhere
F: FnOnce(Self) -> Self,
pub fn map_if<F>(self, flag: bool, f: F) -> Selfwhere
F: FnOnce(Self) -> Self,
Conditionally call a closure to map self
if a predicate is true.
use sidoc_html5::owned::Element;
let someval = 42;
Element::new("body")
.map_if(someval == 42, |obj| obj.flag("selected"));
pub fn map_opt<T, F>(self, opt: Option<T>, f: F) -> Selfwhere
F: FnOnce(Self, T) -> Self,
Sourcepub fn mod_if<F>(&mut self, flag: bool, f: F) -> &mut Selfwhere
F: FnOnce(&mut Self),
pub fn mod_if<F>(&mut self, flag: bool, f: F) -> &mut Selfwhere
F: FnOnce(&mut Self),
Conditionally call a closure to modify self
, in-place, if a predicate
is true.
use sidoc_html5::owned::Element;
let someval = 42;
let mut e = Element::new("body");
e.mod_if(someval == 42, |obj| {
obj.flag_r("selected");
});
pub fn mod_opt<T, F>(&mut self, opt: Option<T>, f: F) -> &mut Selfwhere
F: FnOnce(&mut Self, T),
Source§impl Element
impl Element
Sourcepub fn add_empty(self, bldr: &mut Builder)
pub fn add_empty(self, bldr: &mut Builder)
Consume self
and add a empty tag representation of element to a sidoc
builder.
An empty/void tag comes is one which does not have a closing tag:
<tagname foo="bar">
.
Sourcepub fn add_content(self, text: &str, bldr: &mut Builder)
pub fn add_content(self, text: &str, bldr: &mut Builder)
Consume self
and add a tag containing text content between the opening
and closing tag to the supplied sidoc builder.
The supplied text is escaped as needed.
use std::sync::Arc;
use sidoc_html5::owned::Element;
let mut bldr = sidoc::Builder::new();
let elem = Element::new("textarea")
.raw_attr("rows", 8)
.raw_attr("cols", 32)
.add_content("This is the text content", &mut bldr);
let mut r = sidoc::RenderContext::new();
let doc = bldr.build().unwrap();
r.doc("root", Arc::new(doc));
let buf = r.render("root").unwrap();
assert_eq!(buf, "<textarea rows=\"8\" cols=\"32\">This is the text content</textarea>\n");
§Panics
If the extra-validation
feature is enabled, panic if the tag name is
not a known “void” element.
Sourcepub fn add_raw_content(self, text: &str, bldr: &mut Builder)
pub fn add_raw_content(self, text: &str, bldr: &mut Builder)
Consume self
and add a tag containing text content between the opening
and closing tag to the supplied sidoc builder.
The supplied text is not escaped.
use std::sync::Arc;
use sidoc_html5::Element;
let mut bldr = sidoc::Builder::new();
let elem = Element::new("button")
.add_raw_content("Do Stuff", &mut bldr);
let mut r = sidoc::RenderContext::new();
let doc = bldr.build().unwrap();
r.doc("root", Arc::new(doc));
let buf = r.render("root").unwrap();
assert_eq!(buf, "<button>Do Stuff</button>\n");
pub fn add_scope(self, bldr: &mut Builder)
Sourcepub fn scope<F>(self, bldr: &mut Builder, f: F)
pub fn scope<F>(self, bldr: &mut Builder, f: F)
use std::sync::Arc;
use sidoc_html5::owned::Element;
let mut bldr = sidoc::Builder::new();
let elem = Element::new("div")
.scope(&mut bldr, |bldr| {
let elem = Element::new("button")
.add_raw_content("Do Stuff", bldr);
});
let mut r = sidoc::RenderContext::new();
let doc = bldr.build().unwrap();
r.doc("root", Arc::new(doc));
let buf = r.render("root").unwrap();
// Output should be:
// <div>
// <button>Do Stuff</button>
// </div>
assert_eq!(buf, "<div>\n <button>Do Stuff</button>\n</div>\n");
Auto Trait Implementations§
impl Freeze for Element
impl RefUnwindSafe for Element
impl Send for Element
impl Sync for Element
impl Unpin 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
Mutably borrows from an owned value. Read more