[][src]Struct lol_html::HtmlRewriter

pub struct HtmlRewriter<'h, O: OutputSink> { /* fields omitted */ }

A streaming HTML rewriter.

Example

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

let mut output = vec![];

{
    let mut rewriter = HtmlRewriter::try_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)
    ).unwrap();

    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>"#
);

Methods

impl<'h, O: OutputSink> HtmlRewriter<'h, O>[src]

pub fn try_new<'s>(
    settings: Settings<'h, 's>,
    output_sink: O
) -> Result<Self, EncodingError>
[src]

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.

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

Writes a chunk of input data to the rewriter.

Panics

  • If previous invocation of the method returned a RewritingError (these errors are unrecovarable).
  • If called after end.

pub fn end(&mut self) -> Result<(), RewritingError>[src]

Finalizes the rewriting process.

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

Panics

  • If previous invocation of write returned a RewritingError (these errors are unrecovarable).
  • If called twice.

Trait Implementations

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.