pub struct HtmlFormatter<'a>(/* private fields */);Expand description
A formatter for building HTML content safely and efficiently.
HtmlFormatter provides methods for constructing HTML by writing tags, attributes, and content to an underlying
Html buffer. It handles HTML escaping automatically when using the *_escaped methods, helping
prevent XSS vulnerabilities.
§Note
This type should not be used directly. Instead, use the html! macro with
render or render_with_capacity functions.
§Usage
use plait::{Html, HtmlFormatter};
let mut html = Html::new();
let mut f = HtmlFormatter::new(&mut html);
f.open_tag("div");
f.write_attribute_escaped("class", "container");
f.close_start_tag();
f.write_html_escaped("<script>alert('xss')</script>");
f.close_tag("div");
assert_eq!(html, "<div class=\"container\"><script>alert('xss')</script></div>");Implementations§
Source§impl<'a> HtmlFormatter<'a>
impl<'a> HtmlFormatter<'a>
Sourcepub fn close_start_tag(&mut self)
pub fn close_start_tag(&mut self)
Closes start tag (should be called after writing attributes).
Sourcepub fn write_raw(&mut self, raw: impl Display)
pub fn write_raw(&mut self, raw: impl Display)
Write HTML content to the formatter without escaping any special HTML characters.
Sourcepub fn write_html_escaped(&mut self, html: impl Display)
pub fn write_html_escaped(&mut self, html: impl Display)
Write HTML content to the formatter, escaping any special HTML characters.
Sourcepub fn write_attribute_raw(&mut self, name: &str, value: impl Display)
pub fn write_attribute_raw(&mut self, name: &str, value: impl Display)
Write an attribute to the formatter without escaping any special HTML characters.
Sourcepub fn write_attribute_escaped(&mut self, name: &str, value: impl Display)
pub fn write_attribute_escaped(&mut self, name: &str, value: impl Display)
Write an attribute to the formatter, escaping any special HTML characters in value.
Sourcepub fn write_maybe_attribute_raw(&mut self, name: &str, value: impl MaybeAttr)
pub fn write_maybe_attribute_raw(&mut self, name: &str, value: impl MaybeAttr)
Write an optional or boolean attribute to the formatter without escaping any special HTML characters.
Sourcepub fn write_maybe_attribute_escaped(
&mut self,
name: &str,
value: impl MaybeAttr,
)
pub fn write_maybe_attribute_escaped( &mut self, name: &str, value: impl MaybeAttr, )
Write an optional or boolean attribute to the formatter, escaping any special HTML characters in value.
Sourcepub fn write_optional_attribute_raw(
&mut self,
name: &str,
value: Option<impl Display>,
)
pub fn write_optional_attribute_raw( &mut self, name: &str, value: Option<impl Display>, )
Write an optional attribute to the formatter without escaping any special HTML characters.
Sourcepub fn write_optional_attribute_escaped(
&mut self,
name: &str,
value: Option<impl Display>,
)
pub fn write_optional_attribute_escaped( &mut self, name: &str, value: Option<impl Display>, )
Write an optional attribute to the formatter, escaping any special HTML characters in value.
Sourcepub fn write_url_attribute_escaped(&mut self, name: &str, value: &str)
pub fn write_url_attribute_escaped(&mut self, name: &str, value: &str)
Write an URL attribute to the formatter, escaping any special HTML characters in value.
Sourcepub fn write_optional_url_attribute_escaped(
&mut self,
name: &str,
value: Option<&str>,
)
pub fn write_optional_url_attribute_escaped( &mut self, name: &str, value: Option<&str>, )
Write an optional URL attribute to the formatter, escaping any special HTML characters in value.
Sourcepub fn write_boolean_attribute(&mut self, name: &str, value: bool)
pub fn write_boolean_attribute(&mut self, name: &str, value: bool)
Write a boolean attribute to the formatter.