Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 08b378d

Browse files
committedMar 13, 2025
feat: 增加对important的识别
1 parent e286742 commit 08b378d

File tree

9 files changed

+94
-113
lines changed

9 files changed

+94
-113
lines changed
 

‎Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ swc_core = { version = "0.90.24", features = ["__common", "ecma_transforms_types
2828
flatbuffers = "24.3.25"
2929
rust_decimal = "1.36.0"
3030
rust_decimal_macros = "1.36.0"
31+
bitflags = "2.4.1"
3132

3233
[build-dependencies]
3334
napi-build = "2.0.1"

‎__test__/fixure/pesudo.scss

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,9 @@
1-
.act {
2-
width:10px;
3-
animation: anim 1s;
4-
font-family: 'Courier New', Courier, monospace;
5-
}
6-
@media (max-width:100px) {
7-
.act {
8-
width:101px;
9-
}
10-
@keyframes anim{
11-
0% {
12-
height:100px;
13-
}
14-
100% {
15-
height:1000px;
16-
}
17-
}
18-
}
19-
@keyframes anim{
20-
0% {
21-
height:100px;
22-
}
23-
100% {
24-
height:1000px;
25-
}
1+
.hello {
2+
margin-top: 10px !important;
3+
margin: 24px auto;
4+
margin-bottom: env(safe-area-inset-bottom);
265
}
276

28-
@font-face {
29-
font-family: JDZH-Light;
30-
src: url('https://wq.360buyimg.com/data/ppms/others/JDZH_Light.ttf') format('truetype');
31-
}
32-
33-
@font-face {
34-
font-family: JDZH-Regular;
35-
src: url('https://wq.360buyimg.com/data/ppms/others/JDZH_Regular.ttf') format('truetype');
36-
}
37-
38-
@font-face {
39-
font-family: JDZH-Bold;
40-
src: url('https://wq.360buyimg.com/data/ppms/others/JDZH_Bold.ttf') format('truetype');
41-
}
42-
43-
@font-face {
44-
font-family: JDZhengHT-EN-Bold;
45-
src: url('https://wq.360buyimg.com/data/ppms/others/JDZhengHei_01_Bold.ttf') format('truetype');
46-
}
47-
48-
@font-face {
49-
font-family: JDZhengHT-EN-Regular;
50-
src: url('https://wq.360buyimg.com/data/ppms/others/JDZH_Regular.ttf') format('truetype');
51-
}
52-
7+
.bbb {
8+
margin-top: 30px;
9+
}

‎__test__/fixure/style.bin

-80 Bytes
Binary file not shown.

‎src/constants.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use bitflags::bitflags;
2+
13
// pub const CONVERT_STYLE_PREFIX: &'static str = "_";
24
pub const CONVERT_STYLE_PX_FN: &'static str = "convertNumber2VP";
35
pub const ENV_FUN: &'static str = "__env__";
@@ -58,16 +60,18 @@ impl SelectorType {
5860
}
5961
}
6062

61-
#[repr(u32)]
62-
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
63-
pub enum ValueFlag {
64-
None, // 普通类型:0 [22, "100%"]
65-
Variable, // 变量类型:1 [22, "var(--w)", 1]
63+
bitflags! {
64+
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
65+
pub struct ValueFlag: u32 {
66+
const NONE = 0;
67+
const VARIABLE = 1;
68+
const IMPORTANT = 2;
69+
}
6670
}
6771

6872
impl ValueFlag {
6973
// 将 SelectorType 枚举值转换为 f64
7074
pub fn to_f64(self) -> f64 {
71-
self as u32 as f64
75+
self.bits() as f64
7276
}
7377
}

‎src/json_writer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ impl JsonWriter {
114114
key: PropName::Ident(Ident::new("declarations".into(), DUMMY_SP)),
115115
value: Box::new(Expr::Array(ArrayLit {
116116
span: DUMMY_SP,
117-
elems: parse_style_values(rule_item.declarations.clone(), Platform::Harmony),
117+
elems: parse_style_values(rule_item.declarations.clone(), rule_item.important_declarections.clone(), Platform::Harmony),
118118
})),
119-
})))
119+
}))),
120120
];
121121
if rule_item.has_env {
122122
lit_props.push(

‎src/style_parser.rs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct RuleItem {
3636
pub selector: String,
3737
pub media: u32,
3838
pub declarations: Vec<StyleValueType>,
39+
pub important_declarections: Vec<StyleValueType>,
3940
pub variables: Vec<CssVariable>,
4041
pub has_env: bool
4142
}
@@ -69,7 +70,7 @@ impl KeyFrameItem {
6970
key: PropName::Str("event".into()),
7071
value: Box::new(Expr::Array(ArrayLit {
7172
span: DUMMY_SP,
72-
elems: parse_style_values(self.declarations.clone(), Platform::Harmony),
73+
elems: parse_style_values(self.declarations.clone(), vec![], Platform::Harmony),
7374
})),
7475
}))),
7576
];
@@ -357,41 +358,54 @@ impl<'i> StyleParser<'i> {
357358
binding
358359
.iter_mut()
359360
.for_each(|(media_index, selector, style_value)| {
360-
let properties = style_value
361-
.declaration
362-
.declarations
363-
.iter()
364-
.map(|property| {
365-
(
366-
to_camel_case(
367-
property
368-
.property_id()
369-
.to_css_string(PrinterOptions::default())
370-
.unwrap()
371-
.as_str(),
372-
false,
373-
),
374-
property.clone(),
375-
)
376-
})
377-
.collect::<Vec<(_, _)>>(); // Specify the lifetime of the tuple elements to match the input data
378-
final_all_style.push((media_index, selector.to_owned(), properties));
361+
// 辅助函数,用于处理属性转换,减少代码重复
362+
let convert_properties = |props: &Vec<Property<'i>>| -> Vec<(String, Property<'i>)> {
363+
props
364+
.iter()
365+
.map(|property| {
366+
(
367+
to_camel_case(
368+
property
369+
.property_id()
370+
.to_css_string(PrinterOptions::default())
371+
.unwrap()
372+
.as_str(),
373+
false,
374+
),
375+
property.clone(),
376+
)
377+
})
378+
.collect()
379+
};
380+
381+
// 处理普通属性和important属性
382+
let properties = convert_properties(&style_value.declaration.declarations);
383+
let important_properties = convert_properties(&style_value.declaration.important_declarations);
384+
385+
final_all_style.push((media_index, selector.to_owned(), properties, important_properties));
379386
});
380387

381388
// 进行样式解析优化,提前解析 ArkUI 的样式,减少运行时的计算
382389
let final_all_style = final_all_style
383390
.iter_mut()
384-
.map(|(media_index, selector, properties)| {
391+
.map(|(media_index, selector, properties, important_properties)| {
385392
let decls_and_vars = parse_style_properties(
386393
&properties
387394
.iter()
388395
.map(|(k, v)| (k.to_owned(), v.clone()))
389396
.collect::<Vec<_>>()
390397
);
398+
let import_decls_and_vars = parse_style_properties(
399+
&important_properties
400+
.iter()
401+
.map(|(k, v)| (k.to_owned(), v.clone()))
402+
.collect::<Vec<_>>()
403+
);
391404
RuleItem {
392405
selector: selector.to_owned(),
393406
media: media_index.to_owned(),
394407
declarations: decls_and_vars.decls,
408+
important_declarections: import_decls_and_vars.decls,
395409
variables: decls_and_vars.vars,
396410
has_env: decls_and_vars.has_env
397411
}
@@ -431,28 +445,15 @@ impl<'i> StyleParser<'i> {
431445
let declaration = &declaration.declaration;
432446
let declarations = &declaration.declarations;
433447
for declaration in declarations.iter() {
434-
let has_property_index = final_properties
435-
.iter()
436-
.position(|property| property.property_id() == declaration.property_id());
437-
if let Some(index) = has_property_index {
438-
final_properties[index] = declaration.clone();
439-
} else {
440-
final_properties.push(declaration.clone());
441-
}
448+
final_properties.push(declaration.clone());
442449
}
443450
}
451+
let mut important_properties: Vec<Property<'i>> = Vec::new();
444452
for declaration in declarations.iter() {
445453
let declaration = &declaration.declaration;
446454
let important_declarations = &declaration.important_declarations;
447455
for declaration in important_declarations.iter() {
448-
let has_property_index = final_properties
449-
.iter()
450-
.position(|property| property.property_id() == declaration.property_id());
451-
if let Some(index) = has_property_index {
452-
final_properties[index] = declaration.clone();
453-
} else {
454-
final_properties.push(declaration.clone());
455-
}
456+
important_properties.push(declaration.clone());
456457
}
457458
}
458459
final_style_record.push((
@@ -462,7 +463,7 @@ impl<'i> StyleParser<'i> {
462463
specificity: 0,
463464
declaration: DeclarationBlock {
464465
declarations: final_properties,
465-
important_declarations: vec![],
466+
important_declarations: important_properties,
466467
},
467468
},
468469
));

‎src/style_propetries/animation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl ToExpr for Animation {
225225
key: PropName::Str("event".into()),
226226
value: Box::new(Expr::Array(ArrayLit {
227227
span: DUMMY_SP,
228-
elems: parse_style_values(item.declarations.clone(), Platform::Harmony),
228+
elems: parse_style_values(item.declarations.clone(), vec![], Platform::Harmony),
229229
})),
230230
}))),
231231
],

‎src/visitor.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,61 @@ impl Hash for SpanKey {
2929
}
3030
}
3131

32-
pub fn parse_style_values(
33-
value: Vec<StyleValueType>,
34-
platform: Platform,
35-
) -> Vec<Option<ExprOrSpread>> {
36-
let mut prop_or_spread = vec![];
37-
38-
// 使用有序表
39-
let mut index_map = IndexMap::new();
40-
41-
value.into_iter().for_each(|style_value| {
32+
// 辅助函数,处理样式值并添加到索引映射中
33+
fn process_style_values(
34+
style_values: Vec<StyleValueType>,
35+
index_map: &mut IndexMap<u32, (Box<Expr>, ValueFlag)>,
36+
platform: &Platform,
37+
base_flag: ValueFlag,
38+
) {
39+
style_values.into_iter().for_each(|style_value| {
4240
// 匹配style_value是否Variable类型
43-
if let StyleValueType::Variable(_) = style_value.clone() {
41+
let variable_flag = if let StyleValueType::Variable(_) = style_value.clone() {
4442
let prop = style_value.to_expr(platform.clone());
4543
if let PropertyTuple::One(id, expr) = prop {
4644
if let Expr::Invalid(_) = expr {
4745
return;
4846
}
49-
index_map.insert(id.clone(), (Box::new(expr), ValueFlag::Variable));
47+
index_map.insert(id.clone() as u32, (Box::new(expr), base_flag | ValueFlag::VARIABLE));
5048
}
5149
return;
52-
}
50+
} else {
51+
ValueFlag::NONE
52+
};
53+
5354
let prop = style_value.to_expr(platform.clone());
5455
match prop {
5556
PropertyTuple::One(id, expr) => {
5657
if let Expr::Invalid(_) = expr {
5758
return;
5859
}
59-
index_map.insert(id.clone(), (Box::new(expr), ValueFlag::None));
60+
index_map.insert(id.clone() as u32, (Box::new(expr), base_flag | variable_flag));
6061
}
6162
PropertyTuple::Array(prop_arr) => prop_arr.into_iter().for_each(|(id, expr)| {
6263
if let Expr::Invalid(_) = expr {
6364
return;
6465
}
65-
index_map.insert(id.clone(), (Box::new(expr), ValueFlag::None));
66+
index_map.insert(id.clone() as u32, (Box::new(expr), base_flag | variable_flag));
6667
}),
6768
_ => {}
6869
}
6970
});
71+
}
72+
73+
pub fn parse_style_values(
74+
value: Vec<StyleValueType>,
75+
import_value: Vec<StyleValueType>,
76+
platform: Platform,
77+
) -> Vec<Option<ExprOrSpread>> {
78+
let mut prop_or_spread = vec![];
79+
80+
// 使用有序表
81+
let mut index_map = IndexMap::new();
82+
83+
// 处理普通样式值
84+
process_style_values(value, &mut index_map, &platform, ValueFlag::NONE);
85+
// 处理important样式值
86+
process_style_values(import_value, &mut index_map, &platform, ValueFlag::IMPORTANT);
7087

7188
index_map.into_iter().for_each(|(id, (expr, value_type))| {
7289
let id_num = id.clone() as u32;

0 commit comments

Comments
 (0)
Please sign in to comment.