diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-06-23 14:30:22 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2025-06-23 15:56:01 +0200 |
| commit | f8dc1ad3bdbe20ec25379427e6afba36c75ec08c (patch) | |
| tree | 84dca4a33f24d8ea065eff30d142263be9cfdcb6 /crates/typst-html/src | |
| parent | 9050ee1639a20463e3cafce58964c9ef0fa38205 (diff) | |
Handle pre elements that start with a newline
Diffstat (limited to 'crates/typst-html/src')
| -rw-r--r-- | crates/typst-html/src/encode.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/crates/typst-html/src/encode.rs b/crates/typst-html/src/encode.rs index 2bfa78a7..82a5df47 100644 --- a/crates/typst-html/src/encode.rs +++ b/crates/typst-html/src/encode.rs @@ -97,6 +97,11 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> { let pretty = w.pretty; if !element.children.is_empty() { + // See HTML spec ยง 13.1.2.5. + if element.tag == tag::pre && starts_with_newline(element) { + w.buf.push('\n'); + } + let pretty_inside = allows_pretty_inside(element.tag) && element.children.iter().any(|node| match node { HtmlNode::Element(child) => wants_pretty_around(child.tag), @@ -133,6 +138,18 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> { Ok(()) } +/// Whether the first character in the element is a newline. +fn starts_with_newline(element: &HtmlElement) -> bool { + for child in &element.children { + match child { + HtmlNode::Tag(_) => {} + HtmlNode::Text(text, _) => return text.starts_with(['\n', '\r']), + _ => return false, + } + } + false +} + /// Whether we are allowed to add an extra newline at the start and end of the /// element's contents. /// |
