Skip to content

Commit 580bc8c

Browse files
authored
Merge pull request #113 from dev-five-git/add-default-value
Add default value
2 parents ffbb489 + b5bb4d0 commit 580bc8c

18 files changed

+318
-31
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/react": patch
3+
---
4+
5+
Add selectors

.changeset/nasty-badgers-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Add default value

.changeset/sixty-wombats-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Fix conditional style issue

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
2+
cobertura.xml
23
build
34
dist
45
node_modules

libs/css/src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ impl Ord for StyleSelector {
6060

6161
impl From<&str> for StyleSelector {
6262
fn from(value: &str) -> Self {
63-
if value.contains(":") {
64-
let t: Vec<_> = value.split(":").collect();
63+
if value.contains("&") {
64+
let t: Vec<_> = value.split("&").collect();
6565
if let Prefix(v) = t[0].into() {
66-
Dual(v, t[1].to_string())
66+
if t[1].is_empty() {
67+
Prefix(v)
68+
} else {
69+
Dual(v, t[1].to_string())
70+
}
6771
} else {
6872
Postfix(t[1].to_string())
6973
}
@@ -84,6 +88,8 @@ impl From<&str> for StyleSelector {
8488
))
8589
} else if value == "print" {
8690
Media("print".to_string())
91+
} else if value.ends_with(" ") {
92+
Prefix(value.trim().to_string())
8793
} else {
8894
Postfix(to_kebab_case(value))
8995
}
@@ -132,6 +138,7 @@ pub fn merge_selector(class_name: &str, selector: Option<&StyleSelector>) -> Str
132138
pub enum SelectorSeparator {
133139
Single,
134140
Double,
141+
None,
135142
}
136143
impl Display for SelectorSeparator {
137144
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
@@ -141,6 +148,7 @@ impl Display for SelectorSeparator {
141148
match self {
142149
SelectorSeparator::Single => ":",
143150
SelectorSeparator::Double => "::",
151+
SelectorSeparator::None => "",
144152
}
145153
)
146154
}
@@ -166,7 +174,9 @@ static DOUBLE_SEPARATOR: Lazy<HashSet<&str>> = Lazy::new(|| {
166174
});
167175

168176
pub fn get_selector_separator(key: &str) -> SelectorSeparator {
169-
if DOUBLE_SEPARATOR.contains(key) {
177+
if key.starts_with(":") || key.is_empty() {
178+
SelectorSeparator::None
179+
} else if DOUBLE_SEPARATOR.contains(key) {
170180
SelectorSeparator::Double
171181
} else {
172182
SelectorSeparator::Single
@@ -669,7 +679,7 @@ mod tests {
669679
);
670680

671681
assert_eq!(
672-
StyleSelector::from("themeDark:placeholder"),
682+
StyleSelector::from("themeDark&placeholder"),
673683
Dual(
674684
":root[data-theme=dark]".to_string(),
675685
"placeholder".to_string()
@@ -683,6 +693,11 @@ mod tests {
683693
StyleSelector::from("themeLight"),
684694
Prefix(":root[data-theme=light]".to_string())
685695
);
696+
697+
assert_eq!(
698+
StyleSelector::from("*[aria=disabled='true'] &:hover"),
699+
Dual("*[aria=disabled='true']".to_string(), ":hover".to_string())
700+
);
686701
}
687702

688703
#[test]
@@ -712,7 +727,7 @@ mod tests {
712727
);
713728

714729
assert_eq!(
715-
merge_selector("cls", Some(&"themeDark:hover".into()),),
730+
merge_selector("cls", Some(&"themeDark&hover".into()),),
716731
":root[data-theme=dark] .cls:hover"
717732
);
718733
}

libs/extractor/src/gen_style.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,59 @@ fn gen_style<'a>(
7777
(None, None) => {
7878
return vec![];
7979
}
80-
(Some(c), None) | (None, Some(c)) => {
81-
gen_style(ast_builder, c)
82-
.into_iter()
83-
.for_each(|p| properties.push(p));
80+
(None, Some(c)) => {
81+
gen_style(ast_builder, c).into_iter().for_each(|p| {
82+
if let ObjectPropertyKind::ObjectProperty(p) = p {
83+
properties.push(ObjectPropertyKind::ObjectProperty(
84+
ast_builder.alloc_object_property(
85+
SPAN,
86+
PropertyKind::Init,
87+
p.key.clone_in(ast_builder.allocator),
88+
Expression::ConditionalExpression(
89+
ast_builder.alloc_conditional_expression(
90+
SPAN,
91+
condition.clone_in(ast_builder.allocator),
92+
Expression::Identifier(
93+
ast_builder
94+
.alloc_identifier_reference(SPAN, "undefined"),
95+
),
96+
p.value.clone_in(ast_builder.allocator),
97+
),
98+
),
99+
false,
100+
false,
101+
false,
102+
),
103+
))
104+
}
105+
});
106+
}
107+
(Some(c), None) => {
108+
gen_style(ast_builder, c).into_iter().for_each(|p| {
109+
if let ObjectPropertyKind::ObjectProperty(p) = p {
110+
properties.push(ObjectPropertyKind::ObjectProperty(
111+
ast_builder.alloc_object_property(
112+
SPAN,
113+
PropertyKind::Init,
114+
p.key.clone_in(ast_builder.allocator),
115+
Expression::ConditionalExpression(
116+
ast_builder.alloc_conditional_expression(
117+
SPAN,
118+
condition.clone_in(ast_builder.allocator),
119+
p.value.clone_in(ast_builder.allocator),
120+
Expression::Identifier(
121+
ast_builder
122+
.alloc_identifier_reference(SPAN, "undefined"),
123+
),
124+
),
125+
),
126+
false,
127+
false,
128+
false,
129+
),
130+
))
131+
}
132+
});
84133
}
85134
(Some(c), Some(a)) => {
86135
let collect_c = gen_style(ast_builder, c);

libs/extractor/src/lib.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,32 @@ mod tests {
655655
"test.tsx",
656656
r#"import { Box } from "@devup-ui/core";
657657
<Box margin={a === b ? undefined : 2} />;
658+
"#,
659+
ExtractOption {
660+
package: "@devup-ui/core".to_string(),
661+
css_file: None
662+
}
663+
)
664+
.unwrap());
665+
666+
reset_class_map();
667+
assert_debug_snapshot!(extract(
668+
"test.tsx",
669+
r#"import { Box } from "@devup-ui/core";
670+
<Box margin={a === b ? `${a}px` : undefined} />;
671+
"#,
672+
ExtractOption {
673+
package: "@devup-ui/core".to_string(),
674+
css_file: None
675+
}
676+
)
677+
.unwrap());
678+
679+
reset_class_map();
680+
assert_debug_snapshot!(extract(
681+
"test.tsx",
682+
r#"import { Box } from "@devup-ui/core";
683+
<Box margin={a === b ? null : `${b}px`} />;
658684
"#,
659685
ExtractOption {
660686
package: "@devup-ui/core".to_string(),
@@ -2117,4 +2143,59 @@ import {Button} from '@devup/ui'
21172143
)
21182144
.unwrap());
21192145
}
2146+
2147+
#[test]
2148+
#[serial]
2149+
fn custom_selector() {
2150+
reset_class_map();
2151+
assert_debug_snapshot!(extract(
2152+
"test.js",
2153+
r#"import {Box} from '@devup-ui/core'
2154+
<Box selectors={{
2155+
"&[aria-diabled='true']": {
2156+
opacity: 0.5
2157+
}
2158+
}} />
2159+
"#,
2160+
ExtractOption {
2161+
package: "@devup-ui/core".to_string(),
2162+
css_file: None
2163+
}
2164+
)
2165+
.unwrap());
2166+
2167+
reset_class_map();
2168+
assert_debug_snapshot!(extract(
2169+
"test.js",
2170+
r#"import {Box} from '@devup-ui/core'
2171+
<Box selectors={{
2172+
"*[aria-diabled='true'] &:hover": {
2173+
opacity: 0.5
2174+
}
2175+
}} />
2176+
"#,
2177+
ExtractOption {
2178+
package: "@devup-ui/core".to_string(),
2179+
css_file: None
2180+
}
2181+
)
2182+
.unwrap());
2183+
2184+
reset_class_map();
2185+
assert_debug_snapshot!(extract(
2186+
"test.js",
2187+
r#"import {Box} from '@devup-ui/core'
2188+
<Box selectors={{
2189+
"*[aria-diabled='true'] &": {
2190+
opacity: 0.5
2191+
}
2192+
}} />
2193+
"#,
2194+
ExtractOption {
2195+
package: "@devup-ui/core".to_string(),
2196+
css_file: None
2197+
}
2198+
)
2199+
.unwrap());
2200+
}
21202201
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.js\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"*[aria-diabled='true'] &:hover\": {\n opacity: 0.5\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Static(
8+
ExtractStaticStyle {
9+
property: "opacity",
10+
value: "0.5",
11+
level: 0,
12+
selector: Some(
13+
Dual(
14+
"*[aria-diabled='true']",
15+
":hover",
16+
),
17+
),
18+
basic: false,
19+
},
20+
),
21+
],
22+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0\" />;\n",
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.js\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"*[aria-diabled='true'] &\": {\n opacity: 0.5\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Static(
8+
ExtractStaticStyle {
9+
property: "opacity",
10+
value: "0.5",
11+
level: 0,
12+
selector: Some(
13+
Prefix(
14+
"*[aria-diabled='true']",
15+
),
16+
),
17+
basic: false,
18+
},
19+
),
20+
],
21+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0\" />;\n",
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.js\",\nr#\"import {Box} from '@devup-ui/core'\n <Box selectors={{\n \"&[aria-diabled='true']\": {\n opacity: 0.5\n }\n }} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Static(
8+
ExtractStaticStyle {
9+
property: "opacity",
10+
value: "0.5",
11+
level: 0,
12+
selector: Some(
13+
Postfix(
14+
"[aria-diabled='true']",
15+
),
16+
),
17+
basic: false,
18+
},
19+
),
20+
],
21+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0\" />;\n",
22+
}

0 commit comments

Comments
 (0)