From fee6844045e6a898bc65491a8e18cd520b24d08b Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 19 Jun 2025 17:11:13 +0200 Subject: [PATCH] Encode empty attributes with shorthand syntax --- crates/typst-html/src/encode.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/typst-html/src/encode.rs b/crates/typst-html/src/encode.rs index 612f923fc..c6a6a7bce 100644 --- a/crates/typst-html/src/encode.rs +++ b/crates/typst-html/src/encode.rs @@ -69,16 +69,21 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> { for (attr, value) in &element.attrs.0 { w.buf.push(' '); w.buf.push_str(&attr.resolve()); - w.buf.push('='); - w.buf.push('"'); - for c in value.chars() { - if charsets::is_valid_in_attribute_value(c) { - w.buf.push(c); - } else { - write_escape(w, c).at(element.span)?; + + // If the string is empty, we can use shorthand syntax. + // `....` + if !value.is_empty() { + w.buf.push('='); + w.buf.push('"'); + for c in value.chars() { + if charsets::is_valid_in_attribute_value(c) { + w.buf.push(c); + } else { + write_escape(w, c).at(element.span)?; + } } + w.buf.push('"'); } - w.buf.push('"'); } w.buf.push('>');