From a28dd28f4f074674d64721d2b916610b2111c902 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 8 Sep 2023 15:49:15 +0100 Subject: [PATCH] Make `ElementContentHandlers` and `DocumentContentHandlers` fields public. 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(), } } ``` --- src/lib.rs | 5 +++-- src/rewritable_units/tokens/text_chunk.rs | 6 +++++- src/rewriter/settings.rs | 16 ++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e81e5ab4..c83ec740 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/rewritable_units/tokens/text_chunk.rs b/src/rewritable_units/tokens/text_chunk.rs index fd01228e..7d24f332 100644 --- a/src/rewritable_units/tokens/text_chunk.rs +++ b/src/rewritable_units/tokens/text_chunk.rs @@ -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()); diff --git a/src/rewriter/settings.rs b/src/rewriter/settings.rs index 090aaabb..30cc6692 100644 --- a/src/rewriter/settings.rs +++ b/src/rewriter/settings.rs @@ -5,7 +5,7 @@ use super::AsciiCompatibleEncoding; use std::borrow::Cow; use std::error::Error; -pub(crate) type HandlerResult = Result<(), Box>; +pub type HandlerResult = Result<(), Box>; pub type DoctypeHandler<'h> = Box HandlerResult + 'h>; pub type CommentHandler<'h> = Box HandlerResult + 'h>; pub type TextHandler<'h> = Box HandlerResult + 'h>; @@ -16,9 +16,9 @@ pub type EndHandler<'h> = Box HandlerResult + 'h /// Specifies element content handlers associated with a selector. #[derive(Default)] pub struct ElementContentHandlers<'h> { - pub(super) element: Option>, - pub(super) comments: Option>, - pub(super) text: Option>, + pub element: Option>, + pub comments: Option>, + pub text: Option>, } impl<'h> ElementContentHandlers<'h> { @@ -64,10 +64,10 @@ impl<'h> ElementContentHandlers<'h> { /// ``` #[derive(Default)] pub struct DocumentContentHandlers<'h> { - pub(super) doctype: Option>, - pub(super) comments: Option>, - pub(super) text: Option>, - pub(super) end: Option>, + pub doctype: Option>, + pub comments: Option>, + pub text: Option>, + pub end: Option>, } impl<'h> DocumentContentHandlers<'h> {