Skip to content

Commit c81c62c

Browse files
committed
feat(css_formatter): support for attribute selectors [#1285] (#1286)
1 parent 5da1b3b commit c81c62c

File tree

7 files changed

+438
-13
lines changed

7 files changed

+438
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
use crate::prelude::*;
2-
use biome_css_syntax::CssAttributeMatcher;
3-
use biome_rowan::AstNode;
2+
use biome_css_syntax::{CssAttributeMatcher, CssAttributeMatcherFields};
3+
use biome_formatter::write;
4+
45
#[derive(Debug, Clone, Default)]
56
pub(crate) struct FormatCssAttributeMatcher;
67
impl FormatNodeRule<CssAttributeMatcher> for FormatCssAttributeMatcher {
78
fn fmt_fields(&self, node: &CssAttributeMatcher, f: &mut CssFormatter) -> FormatResult<()> {
8-
format_verbatim_node(node.syntax()).fmt(f)
9+
let CssAttributeMatcherFields {
10+
operator,
11+
value,
12+
modifier,
13+
} = node.as_fields();
14+
15+
write!(f, [operator.format(), value.format()])?;
16+
17+
if modifier.is_some() {
18+
write!(f, [space(), modifier.format()])?;
19+
}
20+
21+
Ok(())
922
}
1023
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::prelude::*;
2-
use biome_css_syntax::CssAttributeMatcherValue;
3-
use biome_rowan::AstNode;
2+
use biome_css_syntax::{CssAttributeMatcherValue, CssAttributeMatcherValueFields};
3+
use biome_formatter::write;
4+
45
#[derive(Debug, Clone, Default)]
56
pub(crate) struct FormatCssAttributeMatcherValue;
67
impl FormatNodeRule<CssAttributeMatcherValue> for FormatCssAttributeMatcherValue {
@@ -9,6 +10,8 @@ impl FormatNodeRule<CssAttributeMatcherValue> for FormatCssAttributeMatcherValue
910
node: &CssAttributeMatcherValue,
1011
f: &mut CssFormatter,
1112
) -> FormatResult<()> {
12-
format_verbatim_node(node.syntax()).fmt(f)
13+
let CssAttributeMatcherValueFields { name } = node.as_fields();
14+
15+
write!(f, [name.format()])
1316
}
1417
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::prelude::*;
2-
use biome_css_syntax::CssAttributeName;
3-
use biome_rowan::AstNode;
2+
use biome_css_syntax::{CssAttributeName, CssAttributeNameFields};
3+
use biome_formatter::write;
4+
45
#[derive(Debug, Clone, Default)]
56
pub(crate) struct FormatCssAttributeName;
67
impl FormatNodeRule<CssAttributeName> for FormatCssAttributeName {
78
fn fmt_fields(&self, node: &CssAttributeName, f: &mut CssFormatter) -> FormatResult<()> {
8-
format_verbatim_node(node.syntax()).fmt(f)
9+
let CssAttributeNameFields { namespace, name } = node.as_fields();
10+
11+
write!(f, [namespace.format(), name.format()])
912
}
1013
}

crates/biome_css_formatter/src/css/lists/rule_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ impl FormatRule<CssRuleList> for FormatCssRuleList {
88
let mut join = f.join_nodes_with_hardline();
99

1010
for rule in node {
11-
join.entry(rule.syntax(), &rule.format());
11+
join.entry(rule.syntax(), &format_or_verbatim(rule.format()));
1212
}
1313

1414
join.finish()
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
use crate::prelude::*;
2-
use biome_css_syntax::CssAttributeSelector;
3-
use biome_rowan::AstNode;
2+
use biome_css_syntax::{CssAttributeSelector, CssAttributeSelectorFields};
3+
use biome_formatter::write;
4+
45
#[derive(Debug, Clone, Default)]
56
pub(crate) struct FormatCssAttributeSelector;
67
impl FormatNodeRule<CssAttributeSelector> for FormatCssAttributeSelector {
78
fn fmt_fields(&self, node: &CssAttributeSelector, f: &mut CssFormatter) -> FormatResult<()> {
8-
format_verbatim_node(node.syntax()).fmt(f)
9+
let CssAttributeSelectorFields {
10+
l_brack_token,
11+
name,
12+
matcher,
13+
r_brack_token,
14+
} = node.as_fields();
15+
16+
write!(
17+
f,
18+
[
19+
l_brack_token.format(),
20+
name.format(),
21+
matcher.format(),
22+
r_brack_token.format()
23+
]
24+
)
925
}
1026
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
[attr] {}
2+
3+
[ attr ] {}
4+
5+
6+
[ svg | a ] {}
7+
8+
[ foo|att = val] {}
9+
10+
[ *| att] {}
11+
12+
[ |att] {}
13+
14+
input[type="radio" i] {}
15+
img[alt~="person" i][src*="lorem" i] {}
16+
input[type="radio" s] {}
17+
img[alt~="person" s][src*="lorem" s] {}
18+
19+
20+
a[id = test] {}
21+
a[id= "test"] {}
22+
a[id = 'test'] {}
23+
a[id= func("foo")] {}
24+
a[class= "(╯°□°)╯︵ ┻━┻" ]{}
25+
26+
[lang] {}
27+
[ lang] {}
28+
[lang ] {}
29+
[ lang ] {}
30+
[ lang ] {}
31+
[
32+
lang
33+
] {}
34+
span[lang] {}
35+
span[ lang] {}
36+
span[lang ] {}
37+
span[ lang ] {}
38+
span[ lang ] {}
39+
span[lang='pt'] {}
40+
span[lang ='pt'] {}
41+
span[lang= 'pt'] {}
42+
span[lang = 'pt'] {}
43+
span[lang = 'pt'] {}
44+
span[lang='pt' ] {}
45+
span[lang='pt' ] {}
46+
span[
47+
lang
48+
=
49+
'pt'
50+
] {}
51+
span[ lang ~= 'en-us' ] {}
52+
span[ lang ~= 'en-us' ] {}
53+
span[ lang |='zh' ] {}
54+
span[
55+
lang
56+
~=
57+
'en-us'
58+
] {}
59+
a[ href ^= '#' ] {}
60+
a[ href *= 'example' ] {}
61+
a[
62+
href
63+
*=
64+
'example'
65+
] {}
66+
input[ type = 'radio' i ] {}
67+
input[ type = 'radio' i ] {}
68+
input[ type ~= 'radio' i ] {}
69+
input[ type ~= 'radio' i ] {}
70+
input[
71+
type
72+
~=
73+
'radio'
74+
i
75+
] {}
76+
img[ alt = 'person' ][ src = 'lorem' ] {}
77+
img[ alt = 'person' ][ src = 'lorem' ] {}
78+
img[ alt ~= 'person' ][ src *= 'lorem' ] {}
79+
img[ alt ~= 'person' ][ src *= 'lorem' ] {}
80+
img[
81+
alt
82+
~=
83+
'person'
84+
][
85+
src
86+
*=
87+
'lorem'
88+
] {}
89+
90+
[foo|att=val] {}
91+
[ foo | att = val ] {}
92+
[ foo | att = val ] {}
93+
[
94+
foo
95+
|
96+
att
97+
=
98+
val
99+
] {}
100+
[*|att] {}
101+
[ * | att ] {}
102+
[ * | att ] {}
103+
[
104+
*
105+
|
106+
att
107+
] {}
108+
[|att] {}
109+
[ | att ] {}
110+
[ | att ] {}
111+
[
112+
|
113+
att
114+
] {}

0 commit comments

Comments
 (0)