Skip to main content

HtmlFormatter

Struct HtmlFormatter 

Source
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\">&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;</div>");

Implementations§

Source§

impl<'a> HtmlFormatter<'a>

Source

pub fn new(html: &'a mut Html) -> Self

Create a new HtmlFormatter for the given Html string.

Source

pub fn open_tag(&mut self, tag_name: &str)

Opens a tag with the given name.

Source

pub fn close_start_tag(&mut self)

Closes start tag (should be called after writing attributes).

Source

pub fn close_tag(&mut self, tag_name: &str)

Closes a tag with the given name.

Source

pub fn write_raw(&mut self, raw: impl Display)

Write HTML content to the formatter without escaping any special HTML characters.

Source

pub fn write_html_escaped(&mut self, html: impl Display)

Write HTML content to the formatter, escaping any special HTML characters.

Source

pub fn write_attribute_raw(&mut self, name: &str, value: impl Display)

Write an attribute to the formatter without escaping any special HTML characters.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn write_boolean_attribute(&mut self, name: &str, value: bool)

Write a boolean attribute to the formatter.

Auto Trait Implementations§

§

impl<'a> Freeze for HtmlFormatter<'a>

§

impl<'a> RefUnwindSafe for HtmlFormatter<'a>

§

impl<'a> Send for HtmlFormatter<'a>

§

impl<'a> Sync for HtmlFormatter<'a>

§

impl<'a> Unpin for HtmlFormatter<'a>

§

impl<'a> !UnwindSafe for HtmlFormatter<'a>

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.