Skip to content

Commit

Permalink
feat(format/html): normalize casing for directive (#5266)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 authored Mar 5, 2025
1 parent c3ceca4 commit e26c1c3
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 166 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/biome_html_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ biome_diagnostics_categories = { workspace = true }
biome_formatter = { workspace = true }
biome_html_syntax = { workspace = true }
biome_rowan = { workspace = true }
biome_string_case = { workspace = true }
biome_suppression = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
Expand Down
5 changes: 2 additions & 3 deletions crates/biome_html_formatter/src/html/auxiliary/directive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{prelude::*, utils::formatters::FormatTokenAsLowercase};
use biome_formatter::write;
use biome_html_syntax::{HtmlDirective, HtmlDirectiveFields};
#[derive(Debug, Clone, Default)]
Expand All @@ -25,8 +25,7 @@ impl FormatNodeRule<HtmlDirective> for FormatHtmlDirective {
]
)?;
if let Some(html) = html_token {
write!(f, [space()])?;
html.format().fmt(f)?;
write!(f, [space(), FormatTokenAsLowercase::from(html)])?;
}
if let Some(quirk) = quirk_token {
write!(f, [space()])?;
Expand Down
37 changes: 37 additions & 0 deletions crates/biome_html_formatter/src/utils/formatters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::borrow::Cow;

use crate::prelude::*;
use biome_formatter::write;
use biome_formatter::{Format, FormatResult};
use biome_html_syntax::HtmlLanguage;
use biome_rowan::{Language, SyntaxToken};
use biome_string_case::StrLikeExtension;

use crate::{HtmlFormatter, context::HtmlFormatContext};

// TODO: deduplicate with CSS formatter's version of this, move to `biome_formatter`.
pub(crate) struct FormatTokenAsLowercase<L: Language> {
token: SyntaxToken<L>,
}

impl<L: Language> From<SyntaxToken<L>> for FormatTokenAsLowercase<L> {
fn from(value: SyntaxToken<L>) -> Self {
Self { token: value }
}
}

impl Format<HtmlFormatContext> for FormatTokenAsLowercase<HtmlLanguage> {
fn fmt(&self, f: &mut HtmlFormatter) -> FormatResult<()> {
let original = self.token.text_trimmed();
match original.to_ascii_lowercase_cow() {
Cow::Borrowed(_) => write!(f, [self.token.format()]),
Cow::Owned(lowercase) => write!(
f,
[format_replaced(
&self.token,
&dynamic_text(&lowercase, self.token.text_trimmed_range().start()),
)]
),
}
}
}
1 change: 1 addition & 0 deletions crates/biome_html_formatter/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod children;
pub mod formatters;
pub mod metadata;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: directive/lowercase.html
---
# Input

```html
<!doctype html>
```


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

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: css
Indent script and style: false
-----

```html
<!doctype html>
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: example.html
info: directive/uppercase.html
---
# Input

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE HTML>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: directive/uppercase2.html
---
# Input

```html
<!DOCTYPE HTML>
```


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

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
Bracket same line: false
Whitespace sensitivity: css
Indent script and style: false
-----

```html
<!DOCTYPE html>
```

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit e26c1c3

Please sign in to comment.