Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(format/html): add css option for whitespace sensitivity #5016

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/biome_html_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ impl FormatOptions for HtmlFormatOptions {
)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum WhitespaceSensitivity {
/// Leading and trailing whitespace in content is considered significant for inline elements.
/// The formatter considers whitespace significant for elements that have an "inline" display style by default in
/// browser's user agent style sheets.
#[default]
Css,
/// Leading and trailing whitespace in content is considered significant for all elements.
///
/// The formatter should leave at least one whitespace character if whitespace is present.
/// Otherwise, if there is no whitespace, it should not add any after `>` or before `<`. In other words, if there's no whitespace, the text content should hug the tags.
Expand All @@ -256,7 +260,6 @@ pub enum WhitespaceSensitivity {
/// >content</b
/// >
/// ```
#[default]
Strict,
/// Whitespace is considered insignificant. The formatter is free to remove or add whitespace as it sees fit.
Ignore,
Expand All @@ -265,6 +268,7 @@ pub enum WhitespaceSensitivity {
impl fmt::Display for WhitespaceSensitivity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Css => std::write!(f, "css"),
Self::Strict => std::write!(f, "strict"),
Self::Ignore => std::write!(f, "ignore"),
}
Expand All @@ -276,14 +280,19 @@ impl FromStr for WhitespaceSensitivity {

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"css" => Ok(Self::Css),
"strict" => Ok(Self::Strict),
"ignore" => Ok(Self::Ignore),
_ => Err("Value not supported for WhitespaceSensitivity. Supported values are 'strict' and 'ignore'."),
_ => Err("Value not supported for WhitespaceSensitivity. Supported values are 'css', 'strict' and 'ignore'."),
}
}
}

impl WhitespaceSensitivity {
pub const fn is_css(&self) -> bool {
matches!(self, Self::Css)
}

pub const fn is_strict(&self) -> bool {
matches!(self, Self::Strict)
}
Expand Down
14 changes: 6 additions & 8 deletions crates/biome_html_formatter/src/html/auxiliary/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
.as_ref()
.is_some_and(|tag_name| tag_name.text().eq_ignore_ascii_case(tag))
});
let is_whitespace_sensitive = whitespace_sensitivity.is_strict()
|| (whitespace_sensitivity.is_css() && is_inline_tag);

let content_has_leading_whitespace = children
.syntax()
Expand Down Expand Up @@ -85,14 +87,10 @@ impl FormatNodeRule<HtmlElement> for FormatHtmlElement {
// to borrow, while the child formatters are responsible for actually printing
// the tokens. `HtmlElementList` prints them if they are borrowed, otherwise
// they are printed by their original formatter.
let should_borrow_opening_r_angle = whitespace_sensitivity.is_strict()
&& is_inline_tag
&& !children.is_empty()
&& !content_has_leading_whitespace;
let should_borrow_closing_tag = whitespace_sensitivity.is_strict()
&& is_inline_tag
&& !children.is_empty()
&& !content_has_trailing_whitespace;
let should_borrow_opening_r_angle =
is_whitespace_sensitive && !children.is_empty() && !content_has_leading_whitespace;
let should_borrow_closing_tag =
is_whitespace_sensitive && !children.is_empty() && !content_has_trailing_whitespace;

let borrowed_r_angle = if should_borrow_opening_r_angle {
opening_element.r_angle_token().ok()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
assertion_line: 211
info: attributes/multiline/break.html
---
# Input
Expand All @@ -24,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand All @@ -41,7 +40,7 @@ Line ending: LF
Line width: 80
Attribute Position: Multiline
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
assertion_line: 211
info: attributes/multiline/no-break.html
---
# Input
Expand All @@ -24,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand All @@ -41,7 +40,7 @@ Line ending: LF
Line width: 80
Attribute Position: Multiline
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand All @@ -54,7 +54,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: true
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand All @@ -52,7 +52,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: true
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: elements/inline/tags-hug-content-longer.html
info: elements/inline/tags-hug-content-longer-w-attr.html
---
# Input

Expand All @@ -22,7 +22,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: elements/inline/tags-hug-content.html
info: elements/inline/tags-hug-content-w-attr.html
---
# Input

Expand All @@ -22,7 +22,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: strict
Whitespace sensitivity: css
Indent script and style: false
-----

Expand Down
2 changes: 1 addition & 1 deletion packages/@biomejs/backend-jsonrpc/src/workspace.ts

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

Loading