Skip to content

Commit

Permalink
fix(parse/html): handle unclosed elements more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Feb 8, 2025
1 parent de27f6f commit f1e5b20
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
38 changes: 28 additions & 10 deletions crates/biome_html_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 19 additions & 12 deletions crates/biome_html_formatter/src/html/auxiliary/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
closing_element,
} = node.as_fields();

let closing_element = closing_element?;
let opening_element = opening_element?;
let tag_name = opening_element.name()?;
let tag_name = tag_name
Expand Down Expand Up @@ -62,9 +61,13 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
.last_token()
.is_some_and(|tok| tok.has_trailing_whitespace())
|| closing_element
.l_angle_token()
.ok()
.is_some_and(|tok| tok.has_leading_whitespace_or_newline());
.as_ref()
.map(|e| {
e.l_angle_token()
.ok()
.is_some_and(|tok| tok.has_leading_whitespace_or_newline())
})
.unwrap_or_default();

// "Borrowing" in this context refers to tokens in nodes that would normally be
// formatted by that node's formatter, but are instead formatted by a sibling
Expand Down Expand Up @@ -98,7 +101,7 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
None
};
let borrowed_closing_tag = if should_borrow_closing_tag {
Some(closing_element.clone())
closing_element.clone()
} else {
None
};
Expand Down Expand Up @@ -147,13 +150,17 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
}
}
}
FormatNodeRule::fmt(
&FormatHtmlClosingElement::default().with_options(FormatHtmlClosingElementOptions {
tag_borrowed: should_borrow_closing_tag,
}),
&closing_element,
f,
)?;
if let Some(closing_element) = closing_element {
FormatNodeRule::fmt(
&FormatHtmlClosingElement::default().with_options(
FormatHtmlClosingElementOptions {
tag_borrowed: should_borrow_closing_tag,
},
),
&closing_element,
f,
)?;
}

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions crates/biome_html_syntax/src/generated/nodes.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions crates/biome_html_syntax/src/generated/nodes_mut.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion xtask/codegen/html.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ HtmlSelfClosingElement =
HtmlElement =
opening_element: HtmlOpeningElement
children: HtmlElementList
closing_element: HtmlClosingElement
closing_element: HtmlClosingElement?


// <a href="">
Expand Down

0 comments on commit f1e5b20

Please sign in to comment.