Struct lol_html::HtmlRewriter

source ·
pub struct HtmlRewriter<'h, O: OutputSink> { /* private fields */ }
Expand description

A streaming HTML rewriter.

§Example

use lol_html::{element, HtmlRewriter, Settings};

let mut output = vec![];

{
    let mut rewriter = HtmlRewriter::new(
        Settings {
            element_content_handlers: vec![
                // Rewrite insecure hyperlinks
                element!("a[href]", |el| {
                    let href = el
                        .get_attribute("href")
                        .unwrap()
                        .replace("http:", "https:");

                    el.set_attribute("href", &href).unwrap();

                    Ok(())
                })
            ],
            ..Settings::default()
        },
        |c: &[u8]| output.extend_from_slice(c)
    );

    rewriter.write(b"<div><a href=").unwrap();
    rewriter.write(b"http://example.com>").unwrap();
    rewriter.write(b"</a></div>").unwrap();
    rewriter.end().unwrap();
}

assert_eq!(
    String::from_utf8(output).unwrap(),
    r#"<div><a href="https://example.com"></a></div>"#
);

Implementations§

source§

impl<'h, O: OutputSink> HtmlRewriter<'h, O>

source

pub fn new<'s>(settings: Settings<'h, 's>, output_sink: O) -> Self

Constructs a new rewriter with the provided settings that writes the output to the output_sink.

§Note

For the convenience the OutputSink trait is implemented for closures.

source

pub fn write(&mut self, data: &[u8]) -> Result<(), RewritingError>

Writes a chunk of input data to the rewriter.

§Panics
  • If previous invocation of the method returned a RewritingError (these errors are unrecovarable).
source

pub fn end(self) -> Result<(), RewritingError>

Finalizes the rewriting process.

Should be called once the last chunk of the input is written.

§Panics

Trait Implementations§

source§

impl<O: OutputSink> Debug for HtmlRewriter<'_, O>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'h, O> !RefUnwindSafe for HtmlRewriter<'h, O>

§

impl<'h, O> !Send for HtmlRewriter<'h, O>

§

impl<'h, O> !Sync for HtmlRewriter<'h, O>

§

impl<'h, O> Unpin for HtmlRewriter<'h, O>

§

impl<'h, O> !UnwindSafe for HtmlRewriter<'h, O>

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

§

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.