Skip to content

Commit

Permalink
feat(format/html): opt out of formatting for pre, script and `sty…
Browse files Browse the repository at this point in the history
…le` tags (#4729)
  • Loading branch information
dyc3 authored Dec 12, 2024
1 parent c3c405f commit c2dda66
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 10 deletions.
2 changes: 2 additions & 0 deletions crates/biome_formatter/src/format_element/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ pub trait Label {
pub enum VerbatimKind {
Bogus,
Suppressed,
/// This was intentionally skipped, not as a result of suppression.
Skipped,
Verbatim {
/// the length of the formatted node
length: TextSize,
Expand Down
1 change: 1 addition & 0 deletions crates/biome_formatter/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use crate::format_element::document::Document;
pub use crate::format_element::tag::{LabelId, Tag, TagKind};
pub use crate::verbatim::{
format_bogus_node, format_or_verbatim, format_suppressed_node, format_verbatim_node,
format_verbatim_skipped,
};

pub use crate::{
Expand Down
10 changes: 10 additions & 0 deletions crates/biome_formatter/src/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ pub fn format_verbatim_node<L: Language>(node: &SyntaxNode<L>) -> FormatVerbatim
}
}

/// "Formats" a node according to its original formatting in the source text. It's functionally equal to
/// [`format_verbatim_node`], but it doesn't track the node as [VerbatimKind::Verbatim].
pub fn format_verbatim_skipped<L: Language>(node: &SyntaxNode<L>) -> FormatVerbatimNode<L> {
FormatVerbatimNode {
node,
kind: VerbatimKind::Skipped,
format_comments: true,
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct FormatVerbatimNode<'node, L: Language> {
node: &'node SyntaxNode<L>,
Expand Down
27 changes: 19 additions & 8 deletions crates/biome_html_formatter/src/html/auxiliary/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
closing_element,
} = node.as_fields();

write!(
f,
[
opening_element.format(),
children.format(),
closing_element.format(),
]
)?;
let tag_name = opening_element
.clone()
.and_then(|e| e.name())
.map(|e| e.text())
.unwrap_or_default();
// `pre` tags are "preformatted", so we should not format the content inside them. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
// We ignore the `script` and `style` tags as well, since embedded language parsing/formatting is not yet implemented.
let should_be_verbatim = ["script", "style", "pre"]
.iter()
.any(|tag| tag_name.eq_ignore_ascii_case(tag));

write!(f, [opening_element.format()])?;
if should_be_verbatim {
format_verbatim_skipped(children.syntax()).fmt(f)?;
write!(f, [hard_line_break()])?;
} else {
write!(f, [children.format()])?;
}
write!(f, [closing_element.format()])?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_html_formatter/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[allow(unused_imports)]
pub(crate) use crate::{
format_verbatim_node, AsFormat, FormatNodeRule, FormatResult, FormatRule, FormattedIterExt,
HtmlFormatContext, HtmlFormatter,
format_verbatim_node, format_verbatim_skipped, AsFormat, FormatNodeRule, FormatResult,
FormatRule, FormattedIterExt, HtmlFormatContext, HtmlFormatter,
};
pub(crate) use biome_formatter::prelude::*;
#[allow(unused_imports)]
Expand Down
13 changes: 13 additions & 0 deletions crates/biome_html_formatter/tests/specs/html/elements/pre.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<pre>
___ ___ ___ ___
/\ \ ___ /\ \ /\__\ /\ \
/::\ \ /\ \ /::\ \ /::| | /::\ \
/:/\:\ \ \:\ \ /:/\:\ \ /:|:| | /:/\:\ \
/::\~\:\__\ /::\__\ /:/ \:\ \ /:/|:|__|__ /::\~\:\ \
/:/\:\ \:|__| __/:/\/__/ /:/__/ \:\__\ /:/ |::::\__\ /:/\:\ \:\__\
\:\~\:\/:/ / /\/:/ / \:\ \ /:/ / \/__/~~/:/ / \:\~\:\ \/__/
\:\ \::/ / \::/__/ \:\ /:/ / /:/ / \:\ \:\__\
\:\/:/ / \:\__\ \:\/:/ / /:/ / \:\ \/__/
\::/__/ \/__/ \::/ / /:/ / \:\__\
~~ \/__/ \/__/ \/__/
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: elements/pre.html
---
# Input

```html
<pre>
___ ___ ___ ___
/\ \ ___ /\ \ /\__\ /\ \
/::\ \ /\ \ /::\ \ /::| | /::\ \
/:/\:\ \ \:\ \ /:/\:\ \ /:|:| | /:/\:\ \
/::\~\:\__\ /::\__\ /:/ \:\ \ /:/|:|__|__ /::\~\:\ \
/:/\:\ \:|__| __/:/\/__/ /:/__/ \:\__\ /:/ |::::\__\ /:/\:\ \:\__\
\:\~\:\/:/ / /\/:/ / \:\ \ /:/ / \/__/~~/:/ / \:\~\:\ \/__/
\:\ \::/ / \::/__/ \:\ /:/ / /:/ / \:\ \:\__\
\:\/:/ / \:\__\ \:\/:/ / /:/ / \:\ \/__/
\::/__/ \/__/ \::/ / /:/ / \:\__\
~~ \/__/ \/__/ \/__/
</pre>
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
-----

```html
<pre>___ ___ ___ ___
/\ \ ___ /\ \ /\__\ /\ \
/::\ \ /\ \ /::\ \ /::| | /::\ \
/:/\:\ \ \:\ \ /:/\:\ \ /:|:| | /:/\:\ \
/::\~\:\__\ /::\__\ /:/ \:\ \ /:/|:|__|__ /::\~\:\ \
/:/\:\ \:|__| __/:/\/__/ /:/__/ \:\__\ /:/ |::::\__\ /:/\:\ \:\__\
\:\~\:\/:/ / /\/:/ / \:\ \ /:/ / \/__/~~/:/ / \:\~\:\ \/__/
\:\ \::/ / \::/__/ \:\ /:/ / /:/ / \:\ \:\__\
\:\/:/ / \:\__\ \:\/:/ / /:/ / \:\ \/__/
\::/__/ \/__/ \::/ / /:/ / \:\__\
~~ \/__/ \/__/ \/__/
</pre>
```

0 comments on commit c2dda66

Please sign in to comment.