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): attribute formatting #3783

Merged
merged 1 commit into from
Sep 14, 2024
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
8 changes: 5 additions & 3 deletions crates/biome_html_formatter/src/html/auxiliary/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::prelude::*;
use biome_html_syntax::HtmlAttribute;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_html_syntax::{HtmlAttribute, HtmlAttributeFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatHtmlAttribute;
impl FormatNodeRule<HtmlAttribute> for FormatHtmlAttribute {
fn fmt_fields(&self, node: &HtmlAttribute, f: &mut HtmlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let HtmlAttributeFields { name, initializer } = node.as_fields();

write![f, [name.format(), initializer.format()]]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use biome_html_syntax::HtmlAttributeInitializerClause;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_html_syntax::{HtmlAttributeInitializerClause, HtmlAttributeInitializerClauseFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatHtmlAttributeInitializerClause;
impl FormatNodeRule<HtmlAttributeInitializerClause> for FormatHtmlAttributeInitializerClause {
Expand All @@ -9,6 +9,8 @@ impl FormatNodeRule<HtmlAttributeInitializerClause> for FormatHtmlAttributeIniti
node: &HtmlAttributeInitializerClause,
f: &mut HtmlFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let HtmlAttributeInitializerClauseFields { eq_token, value } = node.as_fields();

write![f, [eq_token.format(), value.format()]]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ impl FormatNodeRule<HtmlOpeningElement> for FormatHtmlOpeningElement {
r_angle_token,
} = node.as_fields();

write!(
f,
[
l_angle_token.format(),
name.format(),
attributes.format(),
r_angle_token.format(),
]
)?;
write!(f, [l_angle_token.format(), name.format(),])?;
if attributes.len() > 0 {
write!(f, [space(), attributes.format()])?
}
write!(f, [r_angle_token.format()])?;

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ impl FormatNodeRule<HtmlSelfClosingElement> for FormatHtmlSelfClosingElement {
[
l_angle_token.format(),
name.format(),
space(),
attributes.format(),
soft_line_break_or_space(),
space(),
slash_token.format(),
r_angle_token.format()
]
Expand Down
33 changes: 30 additions & 3 deletions crates/biome_html_formatter/src/html/auxiliary/string.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
use crate::prelude::*;
use biome_html_syntax::HtmlString;
use biome_rowan::AstNode;
use biome_formatter::{format_args, write};
use biome_html_syntax::{HtmlString, HtmlStringFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatHtmlString;
impl FormatNodeRule<HtmlString> for FormatHtmlString {
fn fmt_fields(&self, node: &HtmlString, f: &mut HtmlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let HtmlStringFields { value_token } = node.as_fields();

// Prettier always uses double quotes for HTML strings, regardless of configuration.
if let Ok(value) = value_token.as_ref() {
let value_text = value.text().trim();

if !(value_text.starts_with('"') && value_text.ends_with('"')) {
let range = if value_text.starts_with('\'') && value_text.ends_with('\'') {
value.text_range().add_start(1.into()).sub_end(1.into())
} else {
value.text_range()
};
write!(
f,
[format_replaced(
value,
&group(&format_args![
text("\""),
located_token_text(value, range),
text("\""),
])
)]
)?;
return Ok(());
}
}

write!(f, [value_token.format()])
}
}
16 changes: 15 additions & 1 deletion crates/biome_html_formatter/src/html/lists/attribute_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use crate::prelude::*;
use biome_formatter::{write, AttributePosition};
use biome_html_syntax::HtmlAttributeList;
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatHtmlAttributeList;
impl FormatRule<HtmlAttributeList> for FormatHtmlAttributeList {
type Context = HtmlFormatContext;
fn fmt(&self, node: &HtmlAttributeList, f: &mut HtmlFormatter) -> FormatResult<()> {
f.join().entries(node.iter().formatted()).finish()
let line_break = if f.options().attribute_position() == AttributePosition::Multiline {
hard_line_break()
} else {
soft_line_break_or_space()
};

write!(
f,
[&group(&soft_block_indent(&format_with(|f| {
f.join_with(&line_break)
.entries(node.iter().formatted())
.finish()
})))]
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div foo="bar" disabled class="w-full h-full bg-green text-white" id="foo"></div>
33 changes: 33 additions & 0 deletions crates/biome_html_formatter/tests/specs/attributes-break.html.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: attributes-break.html
---
# Input

```html
<div foo="bar" disabled class="w-full h-full bg-green text-white" id="foo"></div>

```


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

# Outputs

## Output 1

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

```html
<div
foo="bar"
disabled
class="w-full h-full bg-green text-white"
id="foo"
></div>```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div foo="bar" id="foo"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: attributes-no-break.html
---
# Input

```html
<div foo="bar" id="foo"></div>

```


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

# Outputs

## Output 1

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

```html
<div foo="bar" id="foo"></div>```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta charset="utf-8" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: attributes-self-closing.html
---
# Input

```html
<meta charset="utf-8" />

```


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

# Outputs

## Output 1

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

```html
<meta charset="utf-8" />```
1 change: 1 addition & 0 deletions crates/biome_html_formatter/tests/specs/self-closing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<br />
28 changes: 28 additions & 0 deletions crates/biome_html_formatter/tests/specs/self-closing.html.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: self-closing.html
---
# Input

```html
<br />

```


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

# Outputs

## Output 1

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

```html
<br />```
Loading