pub struct Document(_);
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. Currently, Document
is backed by an html5ever::rcdom::Node
object.
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
sourceimpl Document
impl Document
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Serializes a Document
instance to a String
.
This method returns a String
with the sanitized HTML. 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);
sourcepub fn write_to<W>(&self, writer: W) -> Result<()> where
W: Write,
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
Auto Trait Implementations
impl !RefUnwindSafe for Document
impl !Send for Document
impl !Sync for Document
impl Unpin for Document
impl !UnwindSafe for Document
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more