Skip to content

The headless browser for AI agents and web scraping

fix(dom): neutralize "-->" when serializing comment contents

SGavrl4603c05ab296
diff --git a/crates/obscura-dom/src/serialize.rs b/crates/obscura-dom/src/serialize.rs
index 7a489a51..608b5b41 100644
--- a/crates/obscura-dom/src/serialize.rs
+++ b/crates/obscura-dom/src/serialize.rs
@@ -72,7 +72,17 @@ impl DomTree {
}
NodeData::Comment { contents } => {
buf.push_str("<!--");
- buf.push_str(contents);
+ // The HTML parser can never produce a comment whose data contains
+ // "-->" (that ends the comment), but script can via
+ // document.createComment("a-->b"). Emitting it verbatim would close
+ // the comment early and let the trailing text escape into markup.
+ // Neutralize the terminator so the serialized output round-trips as
+ // a single comment.
+ if contents.contains("-->") {
+ buf.push_str(&contents.replace("-->", "--&gt;"));
+ } else {
+ buf.push_str(contents);
+ }
buf.push_str("-->");
}
NodeData::ProcessingInstruction { target, data } => {