Fix html comment rendering

Apparently I just completely forgot to actually include <!-- and -->,
and instead just rendered the comment contents.
This commit is contained in:
Joscha 2024-12-20 16:42:48 +01:00
parent ec7bc571b1
commit 0167d3cea3
3 changed files with 30 additions and 1 deletions

View file

@ -84,7 +84,7 @@ pub use self::{element::*, render::*};
#[cfg(test)]
mod tests {
use crate::{html::*, Attr, Element, Render};
use crate::{html::*, Attr, Content, Element, Render};
#[test]
fn simple_website() {
@ -189,4 +189,26 @@ mod tests {
r#"<html lang="EN"></html>"#,
);
}
#[test]
fn comments() {
assert_eq!(
html(("<!--abc--> ", Content::comment("abc")))
.render_to_string()
.unwrap(),
r#"<html>&lt;!--abc--&gt; <!--abc--></html>"#,
);
assert_eq!(
html(Content::comment("Hello <!-- world -->!"))
.render_to_string()
.unwrap(),
r#"<html><!--Hello <!== world ==>!--></html>"#,
);
assert_eq!(
html(Content::comment("-><!-")).render_to_string().unwrap(),
r#"<html><!-- -><!- --></html>"#,
);
}
}

View file

@ -246,6 +246,8 @@ fn render_text<W: fmt::Write>(w: &mut W, text: &str) -> Result<()> {
}
fn render_comment<W: fmt::Write>(w: &mut W, text: &str) -> Result<()> {
write!(w, "<!--")?;
// A comment...
// - must not start with the string ">"
// - must not start with the string "->"
@ -269,6 +271,7 @@ fn render_comment<W: fmt::Write>(w: &mut W, text: &str) -> Result<()> {
write!(w, " ")?;
}
write!(w, "-->")?;
Ok(())
}