Skip to content

Commit 7fe9036

Browse files
committed
feat: 完善代码
1 parent e9d6f07 commit 7fe9036

File tree

4 files changed

+23
-60
lines changed

4 files changed

+23
-60
lines changed

src/constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub const CONVERT_STYLE_PREFIX: &str = "_";
1+
pub const CONVERT_STYLE_PREFIX: &'static str = "_";

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn parse(component: String, styles: Vec<String>) -> String {
3434
let style_data = style_parser.calc();
3535

3636
let program = Rc::new(RefCell::new(document.program.as_ref().unwrap().clone()));
37-
let jsx_record: Rc<RefCell<std::collections::HashMap<visitor::SpanKey, scraper::Element>>> = Rc::new(RefCell::new(document.jsx_record.as_ref().unwrap().clone()));
37+
let jsx_record = Rc::new(RefCell::new(document.jsx_record.as_ref().unwrap().clone()));
3838
let mut style_write = StyleWrite::new(
3939
program.clone(),
4040
jsx_record.clone(),

src/style_transform/margin_padding.rs

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,40 @@ use lightningcss::{properties::Property, stylesheet::PrinterOptions, traits::ToC
22
use swc_common::DUMMY_SP;
33
use swc_ecma_ast::{Expr, Ident, KeyValueProp, Lit, ObjectLit, Prop, PropName, PropOrSpread, Str};
44

5-
use super::{traits::ToExpr, utils::StringNumber };
5+
use super::traits::ToExpr;
66

77
#[derive(Debug, Clone)]
88
pub struct MarginPadding {
9-
pub top: StringNumber,
10-
pub right: StringNumber,
11-
pub bottom: StringNumber,
12-
pub left: StringNumber,
9+
pub top: String,
10+
pub right: String,
11+
pub bottom: String,
12+
pub left: String,
1313
}
1414

1515
impl MarginPadding {
1616
pub fn new() -> Self {
1717
MarginPadding {
18-
top: StringNumber::String("0".to_string()),
19-
right: StringNumber::String("0".to_string()),
20-
bottom: StringNumber::String("0".to_string()),
21-
left: StringNumber::String("0".to_string()),
18+
top: "0".to_string(),
19+
right: "0".to_string(),
20+
bottom: "0".to_string(),
21+
left: "0".to_string()
2222
}
2323
}
2424

2525
pub fn set_top(&mut self, top: &str) {
26-
self.top = StringNumber::String(top.to_string());
27-
// match parse_px_string(top) {
28-
// Some(num) => {
29-
// self.top = StringNumber::Number(num);
30-
// },
31-
// None => {
32-
// self.top = StringNumber::String(top.to_string());
33-
// }
34-
// }
26+
self.top = top.to_string();
3527
}
3628

3729
pub fn set_right(&mut self, right: &str) {
38-
self.right = StringNumber::String(right.to_string());
30+
self.right = right.to_string();
3931
}
4032

4133
pub fn set_bottom(&mut self, bottom: &str) {
42-
self.bottom = StringNumber::String(bottom.to_string());
34+
self.bottom = bottom.to_string();
4335
}
4436

4537
pub fn set_left(&mut self, left: &str) {
46-
self.left = StringNumber::String(left.to_string());
38+
self.left = left.to_string();
4739
}
4840
}
4941

@@ -60,51 +52,19 @@ impl ToExpr for MarginPadding {
6052
props: vec![
6153
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
6254
key: PropName::Ident(Ident::new("top".into(), DUMMY_SP)),
63-
value: Expr::Lit(
64-
match &self.top {
65-
StringNumber::String(str) => Lit::Str(Str::from(str.to_string())),
66-
StringNumber::Number(num) => {
67-
let number: swc_ecma_ast::Number = (*num as f64).into();
68-
Lit::Num(number)
69-
},
70-
}
71-
).into(),
55+
value: Expr::Lit(Lit::Str(Str::from(self.top.to_string()))).into(),
7256
}))),
7357
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
7458
key: PropName::Ident(Ident::new("right".into(), DUMMY_SP)),
75-
value: Expr::Lit(
76-
match &self.right {
77-
StringNumber::String(str) => Lit::Str(Str::from(str.to_string())),
78-
StringNumber::Number(num) => {
79-
let number: swc_ecma_ast::Number = (*num as f64).into();
80-
Lit::Num(number)
81-
},
82-
}
83-
).into(),
59+
value: Expr::Lit(Lit::Str(Str::from(self.right.to_string()))).into(),
8460
}))),
8561
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
8662
key: PropName::Ident(Ident::new("bottom".into(), DUMMY_SP)),
87-
value: Expr::Lit(
88-
match &self.bottom {
89-
StringNumber::String(str) => Lit::Str(Str::from(str.to_string())),
90-
StringNumber::Number(num) => {
91-
let number: swc_ecma_ast::Number = (*num as f64).into();
92-
Lit::Num(number)
93-
},
94-
}
95-
).into(),
63+
value: Expr::Lit(Lit::Str(Str::from(self.bottom.to_string()))).into(),
9664
}))),
9765
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
9866
key: PropName::Ident(Ident::new("left".into(), DUMMY_SP)),
99-
value: Expr::Lit(
100-
match &self.left {
101-
StringNumber::String(str) => Lit::Str(Str::from(str.to_string())),
102-
StringNumber::Number(num) => {
103-
let number: swc_ecma_ast::Number = (*num as f64).into();
104-
Lit::Num(number)
105-
},
106-
}
107-
).into(),
67+
value: Expr::Lit(Lit::Str(Str::from(self.left.to_string()))).into(),
10868
}))),
10969
]
11070
.into(),

src/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ pub fn to_kebab_case(s: &str) -> String {
5757
}
5858

5959
pub fn prefix_style_key(s: &str) -> String {
60-
CONVERT_STYLE_PREFIX.to_string() + s
60+
let mut result = String::with_capacity(CONVERT_STYLE_PREFIX.len() + s.len());
61+
result.push_str(CONVERT_STYLE_PREFIX);
62+
result.push_str(s);
63+
result
6164
}
6265

6366
// pub fn parse_px_string(input: &str) -> Option<CSSNumber> {

0 commit comments

Comments
 (0)