sidoc_html5::owned

Struct Element

Source
pub struct Element { /* private fields */ }

Implementations§

Source§

impl Element

Source

pub fn new(tag: impl ToString) -> Self

Source

pub fn class(self, cls: impl ToString) -> Self

Source

pub fn class_r(&mut self, cls: impl ToString) -> &mut Self

Source

pub fn flag(self, key: impl ToString) -> Self

Source

pub fn flag_r(&mut self, key: impl ToString) -> &mut Self

Source

pub fn attr(self, key: impl ToString, value: impl AsRef<str>) -> Self

Source

pub fn attr_r( &mut self, key: impl ToString, value: impl AsRef<str>, ) -> &mut Self

Source

pub fn data_attr(self, key: impl ToString, value: impl AsRef<str>) -> Self

Source

pub fn data_attr_r( &mut self, key: impl ToString, value: impl AsRef<str>, ) -> &mut Self

Source

pub fn data_flag(self, key: impl ToString) -> Self

Source

pub fn data_flag_r(&mut self, key: impl ToString) -> &mut Self

Source§

impl Element

Source

pub fn raw_attr(self, key: impl ToString, value: impl ToString) -> Self

Source

pub fn raw_attr_r( &mut self, key: impl ToString, value: impl ToString, ) -> &mut Self

Source§

impl Element

Source

pub fn map_if<F>(self, flag: bool, f: F) -> Self
where 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"));
Source

pub fn map_opt<T, F>(self, opt: Option<T>, f: F) -> Self
where F: FnOnce(Self, T) -> Self,

Source

pub fn mod_if<F>(&mut self, flag: bool, f: F) -> &mut Self
where 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");
});
Source

pub fn mod_opt<T, F>(&mut self, opt: Option<T>, f: F) -> &mut Self
where F: FnOnce(&mut Self, T),

Source§

impl Element

Source

pub fn sub<F>(self, bldr: &mut Builder, f: F)
where F: FnOnce(&mut Builder),

Call a closure for adding child nodes.

use sidoc_html5::owned::Element;
let mut bldr = sidoc::Builder::new();
Element::new("div")
  .sub(&mut bldr, |bldr| {
    Element::new("br")
      .add_empty(bldr);
});
Source§

impl Element

Source

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">.

Source

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.

Source

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");
Source

pub fn add_scope(self, bldr: &mut Builder)

Source

pub fn scope<F>(self, bldr: &mut Builder, f: F)
where F: FnOnce(&mut Builder),

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.