-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format/html): attribute formatting
- Loading branch information
Showing
10 changed files
with
125 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
crates/biome_html_formatter/src/html/lists/attribute_list.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
})))] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
crates/biome_html_formatter/tests/specs/attributes-break.html.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>``` |
1 change: 1 addition & 0 deletions
1
crates/biome_html_formatter/tests/specs/attributes-no-break.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div foo="bar" id="foo"></div> |
28 changes: 28 additions & 0 deletions
28
crates/biome_html_formatter/tests/specs/attributes-no-break.html.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>``` |