mirror of
https://github.com/typst/typst
synced 2025-07-17 01:22:53 +08:00
Extract write_children
function
This commit is contained in:
parent
f8dc1ad3bd
commit
c2e2fd99f6
@ -28,7 +28,7 @@ struct Writer {
|
||||
pretty: bool,
|
||||
}
|
||||
|
||||
/// Write a newline and indent, if pretty printing is enabled.
|
||||
/// Writes a newline and indent, if pretty printing is enabled.
|
||||
fn write_indent(w: &mut Writer) {
|
||||
if w.pretty {
|
||||
w.buf.push('\n');
|
||||
@ -38,7 +38,7 @@ fn write_indent(w: &mut Writer) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode an HTML node into the writer.
|
||||
/// Encodes an HTML node into the writer.
|
||||
fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
||||
match node {
|
||||
HtmlNode::Tag(_) => {}
|
||||
@ -49,7 +49,7 @@ fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode plain text into the writer.
|
||||
/// Encodes plain text into the writer.
|
||||
fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
||||
for c in text.chars() {
|
||||
if charsets::is_valid_in_normal_element_text(c) {
|
||||
@ -61,7 +61,7 @@ fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode one element into the write.
|
||||
/// Encodes one element into the writer.
|
||||
fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
w.buf.push('<');
|
||||
w.buf.push_str(&element.tag.resolve());
|
||||
@ -95,13 +95,25 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pretty = w.pretty;
|
||||
if !element.children.is_empty() {
|
||||
write_children(w, element)?;
|
||||
}
|
||||
|
||||
w.buf.push_str("</");
|
||||
w.buf.push_str(&element.tag.resolve());
|
||||
w.buf.push('>');
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encodes the children of an element.
|
||||
fn write_children(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
// See HTML spec § 13.1.2.5.
|
||||
if element.tag == tag::pre && starts_with_newline(element) {
|
||||
w.buf.push('\n');
|
||||
}
|
||||
|
||||
let pretty = w.pretty;
|
||||
let pretty_inside = allows_pretty_inside(element.tag)
|
||||
&& element.children.iter().any(|node| match node {
|
||||
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
||||
@ -128,13 +140,8 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
w.level -= 1;
|
||||
|
||||
write_indent(w);
|
||||
}
|
||||
w.pretty = pretty;
|
||||
|
||||
w.buf.push_str("</");
|
||||
w.buf.push_str(&element.tag.resolve());
|
||||
w.buf.push('>');
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user