Skip to content

Commit c20cefb

Browse files
committed
Remove invalid /> when content is added to an element
#215
1 parent 3275280 commit c20cefb

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/rewritable_units/element.rs

+3
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
370370

371371
fn prepend_chunk(&mut self, chunk: StringChunk) {
372372
if self.can_have_content {
373+
self.start_tag.set_self_closing_syntax(false);
373374
self.start_tag
374375
.mutations
375376
.mutate()
@@ -434,6 +435,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
434435

435436
fn append_chunk(&mut self, chunk: StringChunk) {
436437
if self.can_have_content {
438+
self.start_tag.set_self_closing_syntax(false);
437439
self.end_tag_mutations_mut().content_before.push_back(chunk);
438440
}
439441
}
@@ -492,6 +494,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
492494

493495
fn set_inner_content_chunk(&mut self, chunk: StringChunk) {
494496
if self.can_have_content {
497+
self.start_tag.set_self_closing_syntax(false);
495498
self.remove_content();
496499
self.start_tag
497500
.mutations

src/rewritable_units/tokens/start_tag.rs

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ impl<'i> StartTag<'i> {
117117
self.self_closing
118118
}
119119

120+
pub(crate) fn set_self_closing_syntax(&mut self, has_slash: bool) {
121+
self.self_closing = has_slash;
122+
}
123+
120124
/// Inserts `content` before the start tag.
121125
///
122126
/// Consequent calls to the method append `content` to the previously inserted content.

src/rewriter/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,30 @@ mod tests {
446446
assert_eq!(res, "<!-- 42 --><span><!--hello--></span>");
447447
}
448448

449+
#[test]
450+
fn rewrite_incorrect_self_closing() {
451+
let res = rewrite_str::<LocalHandlerTypes>(
452+
"<title /></title><div/></div><style /></style><script /></script>
453+
<br/><br><embed/><embed> <svg><a/><path/><path></path></svg>",
454+
RewriteStrSettings {
455+
element_content_handlers: vec![element!("*:not(svg)", |el| {
456+
el.set_attribute("s", if el.is_self_closing() { "y" } else { "n" })?;
457+
el.set_attribute("c", if el.can_have_content() { "y" } else { "n" })?;
458+
el.append("…", ContentType::Text);
459+
Ok(())
460+
})],
461+
..RewriteStrSettings::new()
462+
},
463+
)
464+
.unwrap();
465+
466+
assert_eq!(
467+
res,
468+
r#"<title s="y" c="y">…</title><div s="y" c="y">…</div><style s="y" c="y">…</style><script s="y" c="y">…</script>
469+
<br s="y" c="n" /><br s="n" c="n"><embed s="y" c="n" /><embed s="n" c="n"> <svg><a s="y" c="n" /><path s="y" c="n" /><path s="n" c="y">…</path></svg>"#
470+
);
471+
}
472+
449473
#[test]
450474
fn rewrite_arbitrary_settings() {
451475
let res = rewrite_str("<span>Some text</span>", Settings::new()).unwrap();

0 commit comments

Comments
 (0)