some data",
+ &EXPECTED_USER_DATA,
+ sizeof(EXPECTED_USER_DATA)
+);
+
void element_api_test() {
int user_data = 43;
@@ -718,4 +760,30 @@ void element_api_test() {
lol_html_selector_free(selector);
}
+
+ {
+ note("EndTagChange");
+
+ const char *selector_str = "div";
+ lol_html_selector_t *selector = lol_html_selector_parse(
+ selector_str,
+ strlen(selector_str)
+ );
+
+ lol_html_rewriter_builder_t *builder = lol_html_rewriter_builder_new();
+
+ lol_ok(lol_html_rewriter_builder_add_element_content_handlers(
+ builder,
+ selector,
+ modify_element_end_tag_name_outer,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ ));
+
+ const char *input = "
42
some data
";
+ run_rewriter(builder, input, modify_element_end_tag, &user_data);
+ }
}
diff --git a/src/base/bytes.rs b/src/base/bytes.rs
index 5f232654..828811f2 100644
--- a/src/base/bytes.rs
+++ b/src/base/bytes.rs
@@ -20,6 +20,15 @@ impl<'b> Bytes<'b> {
encoding.encode(string).0.into()
}
+ /// Same as `Bytes::from_str(&string).into_owned()`, but avoids copying in the common case where
+ /// the output and input encodings are the same.
+ pub fn from_string(string: String, encoding: &'static Encoding) -> Bytes<'static> {
+ Bytes(Cow::Owned(match encoding.encode(&string).0 {
+ Cow::Owned(bytes) => bytes,
+ Cow::Borrowed(_) => string.into_bytes(),
+ }))
+ }
+
#[inline]
pub fn from_str_without_replacements(
string: &'b str,
diff --git a/src/rewritable_units/tokens/end_tag.rs b/src/rewritable_units/tokens/end_tag.rs
index c2ac2248..54577de1 100644
--- a/src/rewritable_units/tokens/end_tag.rs
+++ b/src/rewritable_units/tokens/end_tag.rs
@@ -36,6 +36,11 @@ impl<'i> EndTag<'i> {
self.raw = None;
}
+ #[inline]
+ pub fn set_name_str(&mut self, name: String) {
+ self.set_name(Bytes::from_string(name, self.encoding))
+ }
+
#[inline]
pub fn before(&mut self, content: &str, content_type: ContentType) {
self.mutations.before(content, content_type);