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(css_formatter): Always quote attribute matcher values #1321

Merged
merged 1 commit into from
Dec 24, 2023
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::borrow::Cow;

use crate::prelude::*;
use biome_css_syntax::{CssAttributeMatcherValue, CssAttributeMatcherValueFields};
use biome_css_syntax::{
AnyCssAttributeMatcherValue, CssAttributeMatcherValue, CssAttributeMatcherValueFields,
};
use biome_formatter::write;

#[derive(Debug, Clone, Default)]
Expand All @@ -12,6 +16,39 @@ impl FormatNodeRule<CssAttributeMatcherValue> for FormatCssAttributeMatcherValue
) -> FormatResult<()> {
let CssAttributeMatcherValueFields { name } = node.as_fields();

write!(f, [name.format()])
// All attribute values get quoted, no matter what. Strings already
// have the quotes around them, but identifiers need to have quotes
// added.
match name? {
AnyCssAttributeMatcherValue::CssString(string) => {
write!(f, [string.format()])
}
AnyCssAttributeMatcherValue::CssIdentifier(ident) => {
let value = ident.value_token()?;

if f.comments().is_suppressed(ident.syntax()) {
return write!(f, [ident.format()]);
}

let quoted = std::format!("\"{}\"", value.text_trimmed());

write!(
f,
[
format_leading_comments(ident.syntax()),
format_replaced(
&value,
&syntax_token_cow_slice(
Cow::Owned(quoted),
&value,
value.text_trimmed_range().start()
)
),
format_trailing_comments(ident.syntax()),
format_dangling_comments(ident.syntax())
]
)
}
}
}
}
1 change: 0 additions & 1 deletion crates/biome_css_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ where
{
fn fmt(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()> {
if self.is_suppressed(node, f) {
println!("Printing suppressed for some reason");
return write!(f, [format_suppressed_node(node.syntax())]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Line width: 80
[svg|a] {
}

[foo|att=val] {
[foo|att="val"] {
}

[*|att] {
Expand All @@ -164,7 +164,7 @@ input[type="radio" s] {
img[alt~="person" s][src*="lorem" s] {
}

a[id=test] {
a[id="test"] {
}
a[id="test"] {
}
Expand Down Expand Up @@ -247,13 +247,13 @@ img[alt~='person'][src*='lorem'] {
img[alt~='person'][src*='lorem'] {
}

[foo|att=val] {
[foo|att="val"] {
}
[foo|att=val] {
[foo|att="val"] {
}
[foo|att=val] {
[foo|att="val"] {
}
[foo|att=val] {
[foo|att="val"] {
}
[*|att] {
}
Expand Down