diff options
Diffstat (limited to 'crates/typst-html')
| -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. /// |
