pub struct HtmlRewriter<'h, O: OutputSink, H: HandlerTypes = LocalHandlerTypes> { /* 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::new()
},
|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, H: HandlerTypes> HtmlRewriter<'h, O, H>
impl<'h, O: OutputSink, H: HandlerTypes> HtmlRewriter<'h, O, H>
Sourcepub fn new<'s>(settings: Settings<'h, 's, H>, output_sink: O) -> Self
pub fn new<'s>(settings: Settings<'h, 's, H>, 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§
Source§impl<O: OutputSink, H: HandlerTypes> Debug for HtmlRewriter<'_, O, H>
impl<O: OutputSink, H: HandlerTypes> Debug for HtmlRewriter<'_, O, H>
Auto Trait Implementations§
impl<'h, O, H> Freeze for HtmlRewriter<'h, O, H>where
O: Freeze,
impl<'h, O, H = LocalHandlerTypes> !RefUnwindSafe for HtmlRewriter<'h, O, H>
impl<'h, O, H> Send for HtmlRewriter<'h, O, H>where
O: Send,
<H as HandlerTypes>::DoctypeHandler<'h>: Send,
<H as HandlerTypes>::CommentHandler<'h>: Send,
<H as HandlerTypes>::TextHandler<'h>: Send,
<H as HandlerTypes>::EndTagHandler<'static>: Send,
<H as HandlerTypes>::ElementHandler<'h>: Send,
<H as HandlerTypes>::EndHandler<'h>: Send,
impl<'h, O, H = LocalHandlerTypes> !Sync for HtmlRewriter<'h, O, H>
impl<'h, O, H> Unpin for HtmlRewriter<'h, O, H>where
O: Unpin,
<H as HandlerTypes>::DoctypeHandler<'h>: Unpin,
<H as HandlerTypes>::CommentHandler<'h>: Unpin,
<H as HandlerTypes>::TextHandler<'h>: Unpin,
<H as HandlerTypes>::EndTagHandler<'static>: Unpin,
<H as HandlerTypes>::ElementHandler<'h>: Unpin,
<H as HandlerTypes>::EndHandler<'h>: Unpin,
impl<'h, O, H = LocalHandlerTypes> !UnwindSafe for HtmlRewriter<'h, O, H>
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