Skip to content

Commit 0ed8cb3

Browse files
committed
feat: 增加伪类
1 parent 42a42a8 commit 0ed8cb3

File tree

10 files changed

+258
-181
lines changed

10 files changed

+258
-181
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Debug Rust",
66
"type": "lldb",
77
"request": "launch",
8-
"program": "/Users/zhuminghui2/.nvm/versions/node/v16.20.2/bin/node",
8+
// "program": "/Users/zhuminghui2/.nvm/versions/node/v16.20.2/bin/node",
99
"cwd": "${workspaceFolder}",
1010
"args": [
1111
"${workspaceFolder}/__test__/index.js"

__test__/fixure/mod.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export default class Mod extends React.Component {
123123
src='//img14.360buyimg.com/img/jfs/t1/206378/24/25778/195/64eca527F378f17a2/c1623681708609fd.png'
124124
></img>
125125
</div>
126-
<div className='normal'></div>
126+
<div className='pesudo'></div>
127127
</div>
128128
)
129129
}

__test__/fixure/pesudo.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { View } from '@tarojs/components'
2+
import './pesudo.scss'
3+
4+
function Pesudo() {
5+
6+
return <View className='pesudo'></View>
7+
}

__test__/fixure/pesudo.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
.pesudo {
4+
height: 200px;
5+
width: 200px;
6+
}
7+
8+
.pesudo::before {
9+
content: '';
10+
color: #00f;
11+
}
12+
.pesudo::after {
13+
content: '';
14+
color: #f00;
15+
height: 20px;
16+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub fn parse(component: String, styles: Vec<String>, platform_string: String) ->
4545
program.clone(),
4646
jsx_record.clone(),
4747
style_data.style_record.clone(),
48+
style_data.pesudo_style_record.clone(),
4849
style_data.all_style.clone(),
4950
);
5051
match platform_string.as_str() {

src/style_parser.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub type StyleValue = Vec<StyleValueType>;
1010

1111
pub struct StyleData<'i> {
1212
pub style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Property<'i>)>>>>,
13+
pub pesudo_style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Vec<(String, Property<'i>)>)>>>>,
1314
pub all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
1415
}
1516

@@ -42,7 +43,7 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
4243
match rule {
4344
CssRule::Style(style) => {
4445
let selectors_str = style.selectors.to_string();
45-
let selectors = selectors_str.split(",").collect::<Vec<&str>>();
46+
let selectors: Vec<&str> = selectors_str.split(",").collect::<Vec<&str>>();
4647
for index in 0..selectors.len() {
4748
let selector = selectors[index].trim().replace(".", "");
4849
let mut all_style = self.all_style.borrow_mut();
@@ -92,11 +93,11 @@ impl<'i> StyleParser<'i> {
9293
// 遍历 style_record,计算每个节点的最终样式
9394
let mut all_style = self.all_style.borrow_mut();
9495
let mut style_record = HashMap::new();
96+
let mut pesudo_style_record = HashMap::new();
9597
let mut final_all_style = self.calc_style_record(&mut all_style);
9698

9799
// final_all_style 转换为驼峰命名
98100
let mut final_all_style = final_all_style.iter_mut().map(|(selector, style_value)| {
99-
// calc_style_record 已经处理掉了所有important_declarations,所以这里不需要再处理
100101
let properties = style_value.declaration.declarations.iter().map(|property| {
101102
(
102103
to_camel_case(
@@ -115,13 +116,48 @@ impl<'i> StyleParser<'i> {
115116
})
116117
.collect::<Vec<(_, _)>>();
117118

119+
let mut pesudo_selector = None;
118120
for (selector, style_value) in final_all_style.iter_mut() {
121+
// 判断是否伪类
122+
123+
if selector.contains(":") {
124+
let selectors = selector.split(":").collect::<Vec<&str>>();
125+
let new_selector = selectors[0].to_string();
126+
pesudo_selector = selectors[1].parse::<String>().ok();
127+
*selector = new_selector;
128+
}
129+
119130
let elements = self.document.select(selector);
120131
for element in elements {
121-
let declarations = style_record.entry(element.span).or_insert(vec![]);
122-
declarations.push(style_value.clone());
132+
match pesudo_selector {
133+
Some(ref selector) => {
134+
let declarations= pesudo_style_record.entry(element.span).or_insert(vec![]);
135+
declarations.push((selector.clone(), style_value.clone()));
136+
}
137+
None => {
138+
let declarations: &mut Vec<Vec<(String, Property<'_>)>> = style_record.entry(element.span).or_insert(vec![]);
139+
declarations.push(style_value.clone());
140+
}
141+
}
123142
}
124-
}
143+
}
144+
145+
// 进行样式解析优化,提前解析 ArkUI 的样式,减少运行时的计算
146+
let final_all_style = final_all_style
147+
.iter_mut()
148+
.map(|(selector, properties)| {
149+
(
150+
selector.to_owned(),
151+
parse_style_properties(
152+
&properties
153+
.iter()
154+
.map(|(k, v)| (k.to_owned(), v.clone()))
155+
.collect::<Vec<_>>(),
156+
),
157+
)
158+
})
159+
.collect::<HashMap<_, _>>();
160+
125161

126162
let final_style_record = style_record
127163
.iter_mut()
@@ -147,24 +183,12 @@ impl<'i> StyleParser<'i> {
147183
})
148184
.collect::<HashMap<_, _>>();
149185

150-
// 进行样式解析优化,提前解析 ArkUI 的样式,减少运行时的计算
151-
let final_all_style = final_all_style
152-
.iter_mut()
153-
.map(|(selector, properties)| {
154-
(
155-
selector.to_owned(),
156-
parse_style_properties(
157-
&properties
158-
.iter()
159-
.map(|(k, v)| (k.to_owned(), v.clone()))
160-
.collect::<Vec<_>>(),
161-
),
162-
)
163-
})
164-
.collect::<HashMap<_, _>>();
186+
let final_pesudo_style_record = pesudo_style_record;
187+
165188

166189
StyleData {
167190
style_record: Rc::new(RefCell::new(final_style_record)),
191+
pesudo_style_record: Rc::new(RefCell::new(final_pesudo_style_record)),
168192
all_style: Rc::new(RefCell::new(final_all_style)),
169193
}
170194
}

src/style_propetries/background.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn parse_background(background: &SmallVec<[LNBackground<'_>; 1]>) -> Background
7373
value: background_repeat,
7474
});
7575
}
76-
if (background_color.is_some()) {
76+
if background_color.is_some() {
7777
bg.color = background_color;
7878
}
7979
bg

0 commit comments

Comments
 (0)