Struct ammonia::Document

source ·
pub struct Document(/* private fields */);
Expand description

A sanitized HTML document.

The Document type is an opaque struct representing an HTML fragment that was sanitized by ammonia. It can be converted to a String or written to a Write instance. This allows users to avoid buffering the serialized representation to a String when desired.

This type is opaque to insulate the caller from breaking changes in the html5ever interface.

Note that this type wraps an html5ever DOM tree. ammonia does not support streaming, so the complete fragment needs to be stored in memory during processing.

§Examples

use ammonia::Builder;

let input = "<!-- comments will be stripped -->This is an Ammonia example.";
let output = "This is an Ammonia example.";

let document = Builder::new()
    .clean(input);
assert_eq!(document.to_string(), output);

Implementations§

source§

impl Document

source

pub fn write_to<W>(&self, writer: W) -> Result<()>
where W: Write,

Serializes a Document instance to a writer.

This method writes the sanitized HTML to a Write instance, avoiding a buffering step.

To avoid consuming the writer, a mutable reference can be passed, like in the example below.

Note that the in-memory representation of Document is larger than the serialized String.

§Examples
use ammonia::Builder;

let input = "Some <style></style>HTML here";
let expected = b"Some HTML here";

let document = Builder::new()
    .clean(input);

let mut sanitized = Vec::new();
document.write_to(&mut sanitized)
    .expect("Writing to a string should not fail (except on OOM)");
assert_eq!(sanitized, expected);

Trait Implementations§

source§

impl Clone for Document

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Document

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Document

Convert a Document to stringified HTML.

Since Document implements Display, it can be converted to a String using the standard ToString::to_string method. This is the simplest way to use ammonia.

§Examples

use ammonia::Builder;

let input = "Some <style></style>HTML here";
let output = "Some HTML here";

let document = Builder::new()
    .clean(input);
assert_eq!(document.to_string(), output);
source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Document> for String

source§

fn from(document: Document) -> Self

Converts to this type from the input type.

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.