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>
impl<'h, O: OutputSink> HtmlRewriter<'h, O>
sourcepub fn new<'s>(settings: Settings<'h, 's>, output_sink: O) -> Self
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.
sourcepub fn write(&mut self, data: &[u8]) -> Result<(), RewritingError>
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).
sourcepub fn end(self) -> Result<(), RewritingError>
pub fn end(self) -> Result<(), RewritingError>
Finalizes the rewriting process.
Should be called once the last chunk of the input is written.
§Panics
- If previous invocation of
write
returned aRewritingError
(these errors are unrecovarable).
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more