|
1 | 1 | use lightningcss::{values::length::LengthValue, traits::ToCss, stylesheet::PrinterOptions}; |
2 | | -use regex::Regex; |
| 2 | +use pcre2::bytes::Regex; |
3 | 3 | use swc_common::DUMMY_SP; |
4 | 4 | use swc_ecma_ast::{ExprOrSpread, Expr, Callee, Ident, CallExpr, Lit, Number}; |
5 | 5 | use crate::{constants::{CONVERT_STYLE_PX_FN, RN_CONVERT_STYLE_PX_FN, RN_CONVERT_STYLE_VU_FN}, generate_expr_lit_num, generate_expr_lit_str, utils::fix_rgba}; |
@@ -125,63 +125,69 @@ pub fn generate_expr_by_length_value(length_value: &LengthValue, platform: Platf |
125 | 125 |
|
126 | 126 | pub fn generate_expr_with_css_input(input: String, platform: Platform) -> Expr { |
127 | 127 | // 定义匹配 '16px' 的正则表达式 |
128 | | - let re: Regex = Regex::new(r"(-?(\d+(\.\d*)?|\.\d+))((px)|(vw)|(vh))").unwrap(); |
| 128 | + let re = Regex::new(r"(-?(?P<num>\d+(\.\d*)?|\.\d+))(?P<unit>(px)|(vw)|(vh))").unwrap(); |
| 129 | + let bytes = input.as_bytes(); |
129 | 130 | // 使用正则表达式进行匹配 |
130 | | - if let Some(captures) = re.captures(&input) { |
131 | | - // 提取匹配到的数字部分 |
132 | | - let input_str = captures.get(1).unwrap().as_str(); |
133 | | - let unit = match captures.get(4) { |
134 | | - Some(m) => m.as_str(), |
135 | | - None => "vp" |
136 | | - }; |
| 131 | + if let Ok(caps) = re.captures(bytes) { |
| 132 | + if let Some(caps) = caps { |
| 133 | + // 提取匹配到的数字部分 |
| 134 | + let input_str = std::str::from_utf8(&caps["num"]); |
| 135 | + let unit = match std::str::from_utf8(&caps["unit"]) { |
| 136 | + Ok(s) => s, |
| 137 | + Err(_) => "vp", |
| 138 | + }; |
| 139 | + if let Ok(input_str) = input_str { |
| 140 | + if let Ok(number) = input_str.parse::<f64>() { |
137 | 141 |
|
138 | | - if let Ok(number) = input_str.parse::<f64>() { |
139 | | - |
140 | | - let mut args: Vec<Expr> = vec![]; |
141 | | - let mut handler: Option<String> = None; |
142 | | - |
143 | | - match unit { |
144 | | - "vw" | "vh" | "vmin" | "vmax" => { |
145 | | - handler = match platform { |
146 | | - Platform::ReactNative => Some(RN_CONVERT_STYLE_VU_FN.to_string()), |
147 | | - Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string()) |
148 | | - }; |
149 | | - args.push(generate_expr_lit_num!(number)); |
150 | | - args.push(generate_expr_lit_str!(unit)); |
151 | | - }, |
152 | | - "px" => { |
153 | | - handler = match platform { |
154 | | - Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()), |
155 | | - Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string()) |
156 | | - }; |
157 | | - args.push(generate_expr_lit_num!(number)); |
158 | | - }, |
159 | | - "rem" => { |
160 | | - handler = match platform { |
161 | | - Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()), |
162 | | - Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string()) |
163 | | - }; |
164 | | - args.push(generate_expr_lit_num!(number * 16.0)); |
165 | | - }, |
166 | | - _ => {} |
| 142 | + let mut args: Vec<Expr> = vec![]; |
| 143 | + let mut handler: Option<String> = None; |
| 144 | + |
| 145 | + match unit { |
| 146 | + "vw" | "vh" | "vmin" | "vmax" => { |
| 147 | + handler = match platform { |
| 148 | + Platform::ReactNative => Some(RN_CONVERT_STYLE_VU_FN.to_string()), |
| 149 | + Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string()) |
| 150 | + }; |
| 151 | + args.push(generate_expr_lit_num!(number)); |
| 152 | + args.push(generate_expr_lit_str!(unit)); |
| 153 | + }, |
| 154 | + "px" => { |
| 155 | + handler = match platform { |
| 156 | + Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()), |
| 157 | + Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string()) |
| 158 | + }; |
| 159 | + args.push(generate_expr_lit_num!(number)); |
| 160 | + }, |
| 161 | + "rem" => { |
| 162 | + handler = match platform { |
| 163 | + Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()), |
| 164 | + Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string()) |
| 165 | + }; |
| 166 | + args.push(generate_expr_lit_num!(number * 16.0)); |
| 167 | + }, |
| 168 | + _ => {} |
| 169 | + } |
| 170 | + |
| 171 | + // 替换原始字符串 |
| 172 | + if let Some(handler_name) = handler { |
| 173 | + return Expr::Call(CallExpr { |
| 174 | + span: DUMMY_SP, |
| 175 | + callee: Callee::Expr(Box::new(Expr::Ident(Ident::new( |
| 176 | + handler_name.into(), |
| 177 | + DUMMY_SP |
| 178 | + )))), |
| 179 | + args: args.into_iter().map(|arg| ExprOrSpread { |
| 180 | + spread: None, |
| 181 | + expr: Box::new(arg), |
| 182 | + }).collect(), |
| 183 | + type_args: None, |
| 184 | + }) |
| 185 | + } |
| 186 | + } |
167 | 187 | } |
| 188 | + } |
168 | 189 |
|
169 | | - // 替换原始字符串 |
170 | | - if let Some(handler_name) = handler { |
171 | | - return Expr::Call(CallExpr { |
172 | | - span: DUMMY_SP, |
173 | | - callee: Callee::Expr(Box::new(Expr::Ident(Ident::new( |
174 | | - handler_name.into(), |
175 | | - DUMMY_SP |
176 | | - )))), |
177 | | - args: args.into_iter().map(|arg| ExprOrSpread { |
178 | | - spread: None, |
179 | | - expr: Box::new(arg), |
180 | | - }).collect(), |
181 | | - type_args: None, |
182 | | - }) |
183 | | - } |
184 | | - } |
| 190 | + |
185 | 191 | } |
186 | 192 |
|
187 | 193 |
|
|
0 commit comments