Skip to content

Commit

Permalink
feat(css_formatter): support for attribute selectors [#1285] (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
faultyserver authored Dec 22, 2023
1 parent 48e0ddd commit 4dac0be
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 13 deletions.
19 changes: 16 additions & 3 deletions crates/biome_css_formatter/src/css/auxiliary/attribute_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use crate::prelude::*;
use biome_css_syntax::CssAttributeMatcher;
use biome_rowan::AstNode;
use biome_css_syntax::{CssAttributeMatcher, CssAttributeMatcherFields};
use biome_formatter::write;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssAttributeMatcher;
impl FormatNodeRule<CssAttributeMatcher> for FormatCssAttributeMatcher {
fn fmt_fields(&self, node: &CssAttributeMatcher, f: &mut CssFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let CssAttributeMatcherFields {
operator,
value,
modifier,
} = node.as_fields();

write!(f, [operator.format(), value.format()])?;

if modifier.is_some() {
write!(f, [space(), modifier.format()])?;
}

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use biome_css_syntax::CssAttributeMatcherValue;
use biome_rowan::AstNode;
use biome_css_syntax::{CssAttributeMatcherValue, CssAttributeMatcherValueFields};
use biome_formatter::write;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssAttributeMatcherValue;
impl FormatNodeRule<CssAttributeMatcherValue> for FormatCssAttributeMatcherValue {
Expand All @@ -9,6 +10,8 @@ impl FormatNodeRule<CssAttributeMatcherValue> for FormatCssAttributeMatcherValue
node: &CssAttributeMatcherValue,
f: &mut CssFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let CssAttributeMatcherValueFields { name } = node.as_fields();

write!(f, [name.format()])
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::prelude::*;
use biome_css_syntax::CssAttributeName;
use biome_rowan::AstNode;
use biome_css_syntax::{CssAttributeName, CssAttributeNameFields};
use biome_formatter::write;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssAttributeName;
impl FormatNodeRule<CssAttributeName> for FormatCssAttributeName {
fn fmt_fields(&self, node: &CssAttributeName, f: &mut CssFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let CssAttributeNameFields { namespace, name } = node.as_fields();

write!(f, [namespace.format(), name.format()])
}
}
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/css/lists/rule_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl FormatRule<CssRuleList> for FormatCssRuleList {
let mut join = f.join_nodes_with_hardline();

for rule in node {
join.entry(rule.syntax(), &rule.format());
join.entry(rule.syntax(), &format_or_verbatim(rule.format()));
}

join.finish()
Expand Down
22 changes: 19 additions & 3 deletions crates/biome_css_formatter/src/css/selectors/attribute_selector.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use crate::prelude::*;
use biome_css_syntax::CssAttributeSelector;
use biome_rowan::AstNode;
use biome_css_syntax::{CssAttributeSelector, CssAttributeSelectorFields};
use biome_formatter::write;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssAttributeSelector;
impl FormatNodeRule<CssAttributeSelector> for FormatCssAttributeSelector {
fn fmt_fields(&self, node: &CssAttributeSelector, f: &mut CssFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let CssAttributeSelectorFields {
l_brack_token,
name,
matcher,
r_brack_token,
} = node.as_fields();

write!(
f,
[
l_brack_token.format(),
name.format(),
matcher.format(),
r_brack_token.format()
]
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[attr] {}

[ attr ] {}


[ svg | a ] {}

[ foo|att = val] {}

[ *| att] {}

[ |att] {}

input[type="radio" i] {}
img[alt~="person" i][src*="lorem" i] {}
input[type="radio" s] {}
img[alt~="person" s][src*="lorem" s] {}


a[id = test] {}
a[id= "test"] {}
a[id = 'test'] {}
a[id= func("foo")] {}
a[class= "(╯°□°)╯︵ ┻━┻" ]{}

[lang] {}
[ lang] {}
[lang ] {}
[ lang ] {}
[ lang ] {}
[
lang
] {}
span[lang] {}
span[ lang] {}
span[lang ] {}
span[ lang ] {}
span[ lang ] {}
span[lang='pt'] {}
span[lang ='pt'] {}
span[lang= 'pt'] {}
span[lang = 'pt'] {}
span[lang = 'pt'] {}
span[lang='pt' ] {}
span[lang='pt' ] {}
span[
lang
=
'pt'
] {}
span[ lang ~= 'en-us' ] {}
span[ lang ~= 'en-us' ] {}
span[ lang |='zh' ] {}
span[
lang
~=
'en-us'
] {}
a[ href ^= '#' ] {}
a[ href *= 'example' ] {}
a[
href
*=
'example'
] {}
input[ type = 'radio' i ] {}
input[ type = 'radio' i ] {}
input[ type ~= 'radio' i ] {}
input[ type ~= 'radio' i ] {}
input[
type
~=
'radio'
i
] {}
img[ alt = 'person' ][ src = 'lorem' ] {}
img[ alt = 'person' ][ src = 'lorem' ] {}
img[ alt ~= 'person' ][ src *= 'lorem' ] {}
img[ alt ~= 'person' ][ src *= 'lorem' ] {}
img[
alt
~=
'person'
][
src
*=
'lorem'
] {}

[foo|att=val] {}
[ foo | att = val ] {}
[ foo | att = val ] {}
[
foo
|
att
=
val
] {}
[*|att] {}
[ * | att ] {}
[ * | att ] {}
[
*
|
att
] {}
[|att] {}
[ | att ] {}
[ | att ] {}
[
|
att
] {}
Loading

0 comments on commit 4dac0be

Please sign in to comment.