Encode empty attributes with shorthand syntax

This commit is contained in:
Laurenz 2025-06-19 17:11:13 +02:00
parent f364b3c323
commit fee6844045

View File

@ -69,6 +69,10 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
for (attr, value) in &element.attrs.0 { for (attr, value) in &element.attrs.0 {
w.buf.push(' '); w.buf.push(' ');
w.buf.push_str(&attr.resolve()); w.buf.push_str(&attr.resolve());
// 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('=');
w.buf.push('"'); w.buf.push('"');
for c in value.chars() { for c in value.chars() {
@ -80,6 +84,7 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
} }
w.buf.push('"'); w.buf.push('"');
} }
}
w.buf.push('>'); w.buf.push('>');