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,16 +69,21 @@ 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());
w.buf.push('=');
w.buf.push('"'); // If the string is empty, we can use shorthand syntax.
for c in value.chars() { // `<elem attr="">..</div` is equivalent to `<elem attr>..</div>`
if charsets::is_valid_in_attribute_value(c) { if !value.is_empty() {
w.buf.push(c); w.buf.push('=');
} else { w.buf.push('"');
write_escape(w, c).at(element.span)?; 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('>'); w.buf.push('>');