diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-06-19 17:11:13 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2025-06-20 17:32:37 +0200 |
| commit | fee6844045e6a898bc65491a8e18cd520b24d08b (patch) | |
| tree | 9b9b17efa7f14ff304a8aa5954622a2b9c9d2e72 /crates | |
| parent | f364b3c3239261e98da4c28c53463b751addfee7 (diff) | |
Encode empty attributes with shorthand syntax
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst-html/src/encode.rs | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/crates/typst-html/src/encode.rs b/crates/typst-html/src/encode.rs index 612f923f..c6a6a7bc 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. + // `<elem attr="">..</div` is equivalent to `<elem attr>..</div>` + 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('>'); |
