use super::mutations::content_to_bytes;
use super::ContentType;
use encoding_rs::Encoding;
use crate::transform_stream::OutputSink;
pub struct DocumentEnd<'a> {
output_sink: &'a mut dyn OutputSink,
encoding: &'static Encoding,
}
impl<'a> DocumentEnd<'a> {
pub(crate) fn new(output_sink: &'a mut dyn OutputSink, encoding: &'static Encoding) -> Self {
DocumentEnd {
output_sink,
encoding,
}
}
#[inline]
pub fn append(&mut self, content: &str, content_type: ContentType) {
content_to_bytes(content, content_type, self.encoding, &mut |c: &[u8]| {
self.output_sink.handle_chunk(c)
});
}
}
#[cfg(test)]
mod tests {
use crate::html_content::*;
use crate::rewritable_units::test_utils::*;
use crate::*;
use encoding_rs::{Encoding, UTF_8};
fn rewrite_on_end(
html: &[u8],
encoding: &'static Encoding,
mut handler: impl FnMut(&mut DocumentEnd),
) -> String {
let mut handler_called = false;
let output = rewrite_html(
html,
encoding,
vec![],
vec![end!(|end| {
handler_called = true;
handler(end);
Ok(())
})],
);
assert!(handler_called, "Handler not called.");
output
}
#[test]
fn append_to_empty_document() {
let output = rewrite_on_end(b"", UTF_8, |end| {
end.append("<div></div>", ContentType::Html);
});
assert_eq!(output, "<div></div>")
}
#[test]
fn append_content() {
for (html, enc) in encoded("<div><h1>Hεllo</h1></div>") {
let output = rewrite_on_end(&html, enc, |end| {
end.append("<span>", ContentType::Html);
end.append("world", ContentType::Text);
end.append("<foo>", ContentType::Text);
end.append("</span>", ContentType::Html);
});
assert_eq!(
output,
"<div><h1>Hεllo</h1></div><span>world<foo></span>"
);
}
}
#[test]
fn append_content_regression() {
for (html, enc) in encoded("") {
let output = rewrite_on_end(&html, enc, |end| {
end.append("<foo>", ContentType::Text);
});
assert_eq!(output, "<foo>");
}
}
}