mirror of
https://github.com/typst/typst
synced 2025-06-25 06:42:53 +08:00
Handle pre elements that start with a newline
This commit is contained in:
parent
9050ee1639
commit
f8dc1ad3bd
@ -97,6 +97,11 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
|||||||
|
|
||||||
let pretty = w.pretty;
|
let pretty = w.pretty;
|
||||||
if !element.children.is_empty() {
|
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)
|
let pretty_inside = allows_pretty_inside(element.tag)
|
||||||
&& element.children.iter().any(|node| match node {
|
&& element.children.iter().any(|node| match node {
|
||||||
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
||||||
@ -133,6 +138,18 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
|||||||
Ok(())
|
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
|
/// Whether we are allowed to add an extra newline at the start and end of the
|
||||||
/// element's contents.
|
/// element's contents.
|
||||||
///
|
///
|
||||||
|
17
tests/ref/html/html-pre-starting-with-newline.html
Normal file
17
tests/ref/html/html-pre-starting-with-newline.html
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre>hello</pre>
|
||||||
|
<pre>
|
||||||
|
|
||||||
|
hello</pre>
|
||||||
|
<pre>
|
||||||
|
|
||||||
|
|
||||||
|
hello</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -5,3 +5,8 @@
|
|||||||
--- html-void-element-with-children html ---
|
--- html-void-element-with-children html ---
|
||||||
// Error: 2-27 HTML void elements must not have children
|
// Error: 2-27 HTML void elements must not have children
|
||||||
#html.elem("img", [Hello])
|
#html.elem("img", [Hello])
|
||||||
|
|
||||||
|
--- html-pre-starting-with-newline html ---
|
||||||
|
#html.pre("hello")
|
||||||
|
#html.pre("\nhello")
|
||||||
|
#html.pre("\n\nhello")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user