Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store a reference to the OutputSink in DocumentEnd.
- Loading branch information
Showing
5 changed files
with
78 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,56 @@ | ||
use super::mutations::content_to_bytes; | ||
use super::ContentType; | ||
|
||
use encoding_rs::Encoding; | ||
|
||
use crate::transform_stream::OutputSink; | ||
|
||
/// A rewritable unit that represents the end of the document. | ||
/// | ||
/// This exposes the [append](#method.append) function that can be used to append content at the | ||
/// end of the document. The content will only be appended after the rewriter has finished processing | ||
/// the final chunk. | ||
pub struct DocumentEnd<'a> { | ||
output_handler: &'a mut dyn FnMut(&[u8]), | ||
output_sink: &'a mut dyn OutputSink, | ||
encoding: &'static Encoding, | ||
} | ||
|
||
impl<'a> DocumentEnd<'a> { | ||
pub(crate) fn new( | ||
output_handler: &'a mut dyn FnMut(&[u8]), | ||
encoding: &'static Encoding, | ||
) -> Self { | ||
pub(crate) fn new(output_sink: &'a mut dyn OutputSink, encoding: &'static Encoding) -> Self { | ||
DocumentEnd { | ||
output_handler, | ||
output_sink, | ||
encoding, | ||
} | ||
} | ||
|
||
/// Appends `content` at the end of the document. | ||
/// | ||
/// Subsequent calls to this method append `content` to the previously inserted content. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use cool_thing::{end, rewrite_str, RewriteStrSettings}; | ||
/// use cool_thing::html_content::{ContentType, DocumentEnd}; | ||
/// | ||
/// let html = rewrite_str( | ||
/// r#"<div id="foo"><!-- content --></div><img>"#, | ||
/// RewriteStrSettings { | ||
/// document_content_handlers: vec![end!(|end| { | ||
/// end.append("<bar>", ContentType::Html); | ||
/// end.append("<baz>", ContentType::Text); | ||
/// Ok(()) | ||
/// })], | ||
/// ..RewriteStrSettings::default() | ||
/// } | ||
/// ).unwrap(); | ||
/// | ||
/// assert_eq!(html, r#"<div id="foo"><!-- content --></div><img><bar><baz>"#); | ||
/// ``` | ||
#[inline] | ||
pub fn append(&mut self, content: &str, content_type: ContentType) { | ||
content_to_bytes(content, content_type, self.encoding, self.output_handler); | ||
content_to_bytes(content, content_type, self.encoding, &mut |c: &[u8]| { | ||
self.output_sink.handle_chunk(c) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters