Skip to content

Commit

Permalink
Make ElementContentHandlers and DocumentContentHandlers fields pu…
Browse files Browse the repository at this point in the history
…blic.

This is helpful if you want to patch already created handlers with extra
functionality. For instance, if you want to measure the time a handler takes
to run you can do something like:

```
fn measure_element_time_handler(handlers: ElementContentHandlers<'_>) -> = ElementContentHandlers<'_> {
   let element_handler = self.element.take().map(|handler| {
       |element| {
           // measure start time here.
           let result = handler(element);
           // measure end time here.
           result
       }
   });

   ElementContentHandlers {
       element: element_handler,
       comments: self.comments.take(),
       text: self.text.take(),
   }
}
```
  • Loading branch information
orium committed Sep 10, 2023
1 parent 8d4c273 commit a28dd28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ mod transform_stream;
use cfg_if::cfg_if;

pub use self::rewriter::{
rewrite_str, AsciiCompatibleEncoding, DocumentContentHandlers, ElementContentHandlers,
HtmlRewriter, MemorySettings, RewriteStrSettings, Settings,
rewrite_str, AsciiCompatibleEncoding, CommentHandler, DoctypeHandler, DocumentContentHandlers,
ElementContentHandlers, ElementHandler, EndHandler, EndTagHandler, HtmlRewriter,
MemorySettings, RewriteStrSettings, Settings, TextHandler, HandlerResult,
};
pub use self::selectors_vm::Selector;
pub use self::transform_stream::OutputSink;
Expand Down
6 changes: 5 additions & 1 deletion src/rewritable_units/tokens/text_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ mod tests {
use super::super::Token;

let encoding = Encoding::for_label_no_replacement("utf-8".as_bytes()).unwrap();
let Token::TextChunk(mut chunk) = TextChunk::new_token("original text", TextType::PlainText, true, encoding) else { unreachable!() };
let Token::TextChunk(mut chunk) =
TextChunk::new_token("original text", TextType::PlainText, true, encoding)
else {
unreachable!()
};

assert_eq!(chunk.as_str(), "original text");
chunk.set_str("hello".to_owned());
Expand Down
16 changes: 8 additions & 8 deletions src/rewriter/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::AsciiCompatibleEncoding;
use std::borrow::Cow;
use std::error::Error;

pub(crate) type HandlerResult = Result<(), Box<dyn Error + Send + Sync>>;
pub type HandlerResult = Result<(), Box<dyn Error + Send + Sync>>;
pub type DoctypeHandler<'h> = Box<dyn FnMut(&mut Doctype) -> HandlerResult + 'h>;
pub type CommentHandler<'h> = Box<dyn FnMut(&mut Comment) -> HandlerResult + 'h>;
pub type TextHandler<'h> = Box<dyn FnMut(&mut TextChunk) -> HandlerResult + 'h>;
Expand All @@ -16,9 +16,9 @@ pub type EndHandler<'h> = Box<dyn FnOnce(&mut DocumentEnd) -> HandlerResult + 'h
/// Specifies element content handlers associated with a selector.
#[derive(Default)]
pub struct ElementContentHandlers<'h> {
pub(super) element: Option<ElementHandler<'h>>,
pub(super) comments: Option<CommentHandler<'h>>,
pub(super) text: Option<TextHandler<'h>>,
pub element: Option<ElementHandler<'h>>,
pub comments: Option<CommentHandler<'h>>,
pub text: Option<TextHandler<'h>>,
}

impl<'h> ElementContentHandlers<'h> {
Expand Down Expand Up @@ -64,10 +64,10 @@ impl<'h> ElementContentHandlers<'h> {
/// ```
#[derive(Default)]
pub struct DocumentContentHandlers<'h> {
pub(super) doctype: Option<DoctypeHandler<'h>>,
pub(super) comments: Option<CommentHandler<'h>>,
pub(super) text: Option<TextHandler<'h>>,
pub(super) end: Option<EndHandler<'h>>,
pub doctype: Option<DoctypeHandler<'h>>,
pub comments: Option<CommentHandler<'h>>,
pub text: Option<TextHandler<'h>>,
pub end: Option<EndHandler<'h>>,
}

impl<'h> DocumentContentHandlers<'h> {
Expand Down

0 comments on commit a28dd28

Please sign in to comment.