Skip to content

Commit bfa898a

Browse files
authored
Merge pull request #449 from dev-five-git/add-params-to-advenced-selector
Implement advencedSelector
2 parents 0555f15 + b0eff39 commit bfa898a

28 files changed

+664
-153
lines changed

.changeset/wacky-snails-show.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@devup-ui/wasm': patch
3+
'@devup-ui/react': patch
4+
---
5+
6+
Support selector with params

Cargo.lock

Lines changed: 23 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/devup-ui-wasm/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ crate-type = ["cdylib", "rlib"]
1515
default = ["console_error_panic_hook"]
1616

1717
[dependencies]
18-
wasm-bindgen = "0.2.104"
18+
wasm-bindgen = "0.2.105"
1919
extractor = { path = "../../libs/extractor" }
2020
sheet = { path = "../../libs/sheet" }
2121
css = { path = "../../libs/css" }
@@ -26,13 +26,13 @@ css = { path = "../../libs/css" }
2626
# code size when deploying.
2727
console_error_panic_hook = { version = "0.1.7", optional = true }
2828
once_cell = "1.21.3"
29-
js-sys = "0.3.81"
29+
js-sys = "0.3.82"
3030
serde_json = "1.0.145"
3131
serde-wasm-bindgen = "0.6.5"
3232
bimap = { version = "0.6.3", features = ["serde"] }
3333

3434
[dev-dependencies]
35-
wasm-bindgen-test = "0.3.54"
35+
wasm-bindgen-test = "0.3.55"
3636
serial_test = "3.2.0"
3737
insta = "1.43.2"
3838
rstest = "0.26.1"

libs/css/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ pub fn disassemble_property(property: &str) -> Vec<String> {
6868
})
6969
}
7070

71+
pub fn add_selector_params(selector: StyleSelector, params: &str) -> StyleSelector {
72+
match selector {
73+
StyleSelector::Selector(value) => StyleSelector::Selector(format!("{}({})", value, params)),
74+
StyleSelector::Global(value, file) => {
75+
StyleSelector::Global(format!("{}({})", value, params), file)
76+
}
77+
StyleSelector::Media { query, selector } => StyleSelector::Media {
78+
query: query.to_string(),
79+
selector: selector.map(|s| format!("{}({})", s, params)),
80+
},
81+
}
82+
}
83+
7184
pub fn get_enum_property_value(property: &str, value: &str) -> Option<Vec<(String, String)>> {
7285
if let Some(map) = GLOBAL_ENUM_STYLE_PROPERTY.get(property) {
7386
if let Some(map) = map.get(value) {
@@ -677,4 +690,32 @@ mod tests {
677690
assert_eq!(keyframes_to_keyframes_name("spin", None), "k-spin");
678691
assert_eq!(keyframes_to_keyframes_name("spin1", None), "k-spin1");
679692
}
693+
694+
#[test]
695+
fn test_add_selector_params() {
696+
assert_eq!(
697+
add_selector_params(StyleSelector::Selector("hover:is".to_string()), "test"),
698+
StyleSelector::Selector("hover:is(test)".to_string())
699+
);
700+
assert_eq!(
701+
add_selector_params(
702+
StyleSelector::Global("&:is".to_string(), "file.ts".to_string()),
703+
"test"
704+
),
705+
StyleSelector::Global("&:is(test)".to_string(), "file.ts".to_string())
706+
);
707+
assert_eq!(
708+
add_selector_params(
709+
StyleSelector::Media {
710+
query: "print".to_string(),
711+
selector: Some("&:is".to_string())
712+
},
713+
"test"
714+
),
715+
StyleSelector::Media {
716+
query: "print".to_string(),
717+
selector: Some("&:is(test)".to_string())
718+
}
719+
);
720+
}
680721
}

libs/extractor/src/extractor/extract_global_style_from_expression.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
extractor::{
1111
GlobalExtractResult, extract_style_from_expression::extract_style_from_expression,
1212
},
13-
utils::get_string_by_literal_expression,
13+
utils::{get_string_by_literal_expression, get_string_by_property_key},
1414
};
1515
use css::{
1616
disassemble_property,
@@ -19,7 +19,7 @@ use css::{
1919
};
2020
use oxc_ast::{
2121
AstBuilder,
22-
ast::{ArrayExpressionElement, Expression, ObjectPropertyKind, PropertyKey},
22+
ast::{ArrayExpressionElement, Expression, ObjectPropertyKind},
2323
};
2424

2525
pub fn extract_global_style_from_expression<'a>(
@@ -33,20 +33,7 @@ pub fn extract_global_style_from_expression<'a>(
3333
for p in obj.properties.iter_mut() {
3434
match p {
3535
ObjectPropertyKind::ObjectProperty(o) => {
36-
if let Some(name) = if let PropertyKey::StaticIdentifier(ident) = &o.key {
37-
Some(ident.name.to_string())
38-
} else if let PropertyKey::StringLiteral(s) = &o.key {
39-
Some(s.value.to_string())
40-
} else if let PropertyKey::TemplateLiteral(t) = &o.key {
41-
Some(
42-
t.quasis
43-
.iter()
44-
.map(|q| q.value.raw.as_str())
45-
.collect::<String>(),
46-
)
47-
} else {
48-
None
49-
} {
36+
if let Some(name) = get_string_by_property_key(&o.key) {
5037
if name == "imports" {
5138
if let Expression::ArrayExpression(arr) = &o.value {
5239
for p in arr.elements.iter() {
@@ -108,11 +95,11 @@ pub fn extract_global_style_from_expression<'a>(
10895
.iter()
10996
.filter_map(|p| {
11097
if let ObjectPropertyKind::ObjectProperty(o) = p
111-
&& let PropertyKey::StaticIdentifier(ident) = &o.key
98+
&& let Some(property_name) = get_string_by_property_key(&o.key)
11299
&& let Some(s) = get_string_by_literal_expression(&o.value)
113100
{
114101
Some(
115-
disassemble_property(&ident.name)
102+
disassemble_property(&property_name)
116103
.iter()
117104
.map(|p| {
118105
let v = if check_multi_css_optimize(p) { optimize_mutli_css_value(&s) } else { s.clone() };

libs/extractor/src/extractor/extract_keyframes_from_expression.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::{
55
ExtractResult, KeyframesExtractResult,
66
extract_style_from_expression::extract_style_from_expression,
77
},
8+
utils::get_string_by_property_key,
89
};
910
use oxc_ast::{
1011
AstBuilder,
11-
ast::{Expression, ObjectPropertyKind, PropertyKey},
12+
ast::{Expression, ObjectPropertyKind},
1213
};
1314

1415
pub fn extract_keyframes_from_expression<'a>(
@@ -20,22 +21,7 @@ pub fn extract_keyframes_from_expression<'a>(
2021
if let Expression::ObjectExpression(obj) = expression {
2122
for p in obj.properties.iter_mut() {
2223
if let ObjectPropertyKind::ObjectProperty(o) = p
23-
&& let Some(name) = if let PropertyKey::StaticIdentifier(ident) = &o.key {
24-
Some(ident.name.to_string())
25-
} else if let PropertyKey::StringLiteral(s) = &o.key {
26-
Some(s.value.to_string())
27-
} else if let PropertyKey::TemplateLiteral(t) = &o.key {
28-
Some(
29-
t.quasis
30-
.iter()
31-
.map(|q| q.value.raw.as_str())
32-
.collect::<String>(),
33-
)
34-
} else if let PropertyKey::NumericLiteral(n) = &o.key {
35-
Some(n.value.to_string())
36-
} else {
37-
None
38-
}
24+
&& let Some(name) = get_string_by_property_key(&o.key)
3925
{
4026
let ExtractResult { styles, .. } =
4127
extract_style_from_expression(ast_builder, None, &mut o.value, 0, &None);

0 commit comments

Comments
 (0)