Skip to content

Commit d0284e4

Browse files
authored
Merge pull request #281 from dev-five-git/optimize-aspect-ratio
Add aspect ratio
2 parents 75b4f15 + c1c4f52 commit d0284e4

File tree

7 files changed

+160
-2
lines changed

7 files changed

+160
-2
lines changed

.changeset/grumpy-heads-thank.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+
Optimize aspect-ratio by gcd

libs/extractor/src/extract_style/extract_static_style.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
extract_style::{
99
ExtractStyleProperty, constant::MAINTAIN_VALUE_PROPERTIES, style_property::StyleProperty,
1010
},
11-
utils::convert_value,
11+
utils::{convert_value, gcd},
1212
};
1313

1414
#[derive(Debug, PartialEq, Clone, Eq, Hash, Ord, PartialOrd)]
@@ -30,7 +30,20 @@ impl ExtractStaticStyle {
3030
pub fn new(property: &str, value: &str, level: u8, selector: Option<StyleSelector>) -> Self {
3131
Self {
3232
value: optimize_value(&if MAINTAIN_VALUE_PROPERTIES.contains(property) {
33-
value.to_string()
33+
if property == "aspect-ratio" && value.contains("/") {
34+
if let [Ok(a), Ok(b)] = value
35+
.split('/')
36+
.map(|v| v.trim().parse::<u32>())
37+
.collect::<Vec<_>>()[..]
38+
{
39+
let gcd = gcd(a, b);
40+
format!("{}/{}", a / gcd, b / gcd)
41+
} else {
42+
value.to_string()
43+
}
44+
} else {
45+
value.to_string()
46+
}
3447
} else {
3548
convert_value(value)
3649
}),

libs/extractor/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,6 +2962,55 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
29622962
));
29632963
}
29642964

2965+
#[test]
2966+
#[serial]
2967+
fn optimize_aspect_ratio() {
2968+
reset_class_map();
2969+
assert_debug_snapshot!(ToBTreeSet::from(
2970+
extract(
2971+
"test.jsx",
2972+
r#"import {Flex} from '@devup-ui/core'
2973+
<Flex aspectRatio={"200/400"} />
2974+
"#,
2975+
ExtractOption {
2976+
package: "@devup-ui/core".to_string(),
2977+
css_file: None
2978+
}
2979+
)
2980+
.unwrap()
2981+
));
2982+
2983+
reset_class_map();
2984+
assert_debug_snapshot!(ToBTreeSet::from(
2985+
extract(
2986+
"test.jsx",
2987+
r#"import {Flex} from '@devup-ui/core'
2988+
<Flex aspectRatio={" 200 / 400 "} />
2989+
"#,
2990+
ExtractOption {
2991+
package: "@devup-ui/core".to_string(),
2992+
css_file: None
2993+
}
2994+
)
2995+
.unwrap()
2996+
));
2997+
2998+
reset_class_map();
2999+
assert_debug_snapshot!(ToBTreeSet::from(
3000+
extract(
3001+
"test.jsx",
3002+
r#"import {Flex} from '@devup-ui/core'
3003+
<Flex aspectRatio={" 200.2 / 400.2 "} />
3004+
"#,
3005+
ExtractOption {
3006+
package: "@devup-ui/core".to_string(),
3007+
css_file: None
3008+
}
3009+
)
3010+
.unwrap()
3011+
));
3012+
}
3013+
29653014
#[test]
29663015
#[serial]
29673016
fn ternary_operator_in_selector() {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Flex} from '@devup-ui/core'\n <Flex aspectRatio={\" 200 / 400 \"} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "aspect-ratio",
10+
value: "1/2",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "display",
19+
value: "flex",
20+
level: 0,
21+
selector: None,
22+
style_order: Some(
23+
0,
24+
),
25+
},
26+
),
27+
},
28+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0 d1\" />;\n",
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Flex} from '@devup-ui/core'\n <Flex aspectRatio={\" 200.2 / 400.2 \"} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "aspect-ratio",
10+
value: "200.2 / 400.2",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "display",
19+
value: "flex",
20+
level: 0,
21+
selector: None,
22+
style_order: Some(
23+
0,
24+
),
25+
},
26+
),
27+
},
28+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0 d1\" />;\n",
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Flex} from '@devup-ui/core'\n <Flex aspectRatio={\"200/400\"} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "aspect-ratio",
10+
value: "1/2",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "display",
19+
value: "flex",
20+
level: 0,
21+
selector: None,
22+
style_order: Some(
23+
0,
24+
),
25+
},
26+
),
27+
},
28+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0 d1\" />;\n",
29+
}

libs/extractor/src/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ pub(super) fn get_string_by_literal_expression(expr: &Expression) -> Option<Stri
110110
})
111111
}
112112

113+
pub fn gcd(a: u32, b: u32) -> u32 {
114+
if b == 0 { a } else { gcd(b, a % b) }
115+
}
116+
113117
#[cfg(test)]
114118
mod tests {
115119
use oxc_allocator::Vec;

0 commit comments

Comments
 (0)