Skip to content

Commit 0752a13

Browse files
committed
fix: 去掉样式继承逻辑,仅支持类选择器
1 parent eec198f commit 0752a13

File tree

8 files changed

+134
-1166
lines changed

8 files changed

+134
-1166
lines changed

__test__/fixure/Mod.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@import url('https://fonts.googleapis.com/css2?family=Source+Han+Sans+CN:wght@500&display=swap');
22

3-
.mod .cnt_row,.icon {
3+
.cnt_row,.icon {
44
width:18px;
55
height:18px;
66
}

__test__/index.spec.mjs.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Generated by [AVA](https://avajs.dev).
1313
function Cc() {␊
1414
return <div className='cc'>␊
1515
16-
<span className='line1 txt'>成员123: 4000+</span>␊
16+
<span className='line1 txt' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#999;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;">成员123: 4000+</span>␊
1717
1818
<div className='cnt_row4'>␊
1919
@@ -31,7 +31,7 @@ Generated by [AVA](https://avajs.dev).
3131
getDom() {␊
3232
return <div className='cc'>␊
3333
34-
<span className='line1 txt' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#999;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;font-family:Source Han Sans CN;font-weight:500;">成员123: 4000+</span>␊
34+
<span className='line1 txt' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#999;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;">成员123: 4000+</span>␊
3535
3636
<div className='cnt_row4'>␊
3737
@@ -65,57 +65,57 @@ Generated by [AVA](https://avajs.dev).
6565
padding-bottom: "29px"␊
6666
}}>␊
6767
68-
<div className='cnt_row' style="border-radius:100px;background-color:#000000b3;display:flex;align-items:center;justify-content:center;flex-shrink:0px;gap:4px;height:18px;padding-left:4px;padding-right:8px;padding-top:8px;padding-bottom:8px;width:176px;font-family:Source Han Sans CN;font-weight:500;">␊
68+
<div className='cnt_row' style="width:176px;height:24px;border-radius:100px;background-color:#000000b3;display:flex;align-items:center;justify-content:center;flex-shrink:0px;gap:4px;padding-left:4px;padding-right:8px;padding-top:8px;padding-bottom:8px;">␊
6969
7070
<>␊
7171
72-
<img className='icon' src='//img20.360buyimg.com/img/jfs/t1/166410/12/38783/3147/64f58062Fd7737e2b/5aaf0205cd1ce175.png' style="width:18px;height:18px;font-family:Source Han Sans CN;font-weight:500;"></img>␊
72+
<img className='icon' src='//img20.360buyimg.com/img/jfs/t1/166410/12/38783/3147/64f58062Fd7737e2b/5aaf0205cd1ce175.png' style="width:18px;height:18px;"></img>␊
7373
7474
<>␊
7575
76-
<span className='line1 instruction' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#fff;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;font-family:Source Han Sans CN;font-weight:500;">超能芭比 5分钟前查看团购</span>␊
76+
<span className='line1 instruction' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#fff;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;">超能芭比 5分钟前查看团购</span>␊
7777
7878
</>␊
7979
8080
</>␊
8181
8282
</div>␊
8383
84-
<div className='cnt_row1' style="display:flex;align-items:center;justify-content:flex-start;gap:10px;position:relative;width:311px;height:50px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;">␊
84+
<div className='cnt_row1' style="display:flex;align-items:center;justify-content:flex-start;gap:10px;position:relative;width:311px;height:50px;">␊
8585
86-
<img className='img' src='//img12.360buyimg.com/img/jfs/t1/100881/15/44805/18567/64f58062F1b45e0cb/caf065a7410087ce.png' style="width:50px;height:50px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;"></img>␊
86+
<img className='img' src='//img12.360buyimg.com/img/jfs/t1/100881/15/44805/18567/64f58062F1b45e0cb/caf065a7410087ce.png' style="width:50px;height:50px;"></img>␊
8787
88-
<div className='cnt_col' style="display:flex;flex-direction:column;align-items:flex-start;justify-content:center;gap:8px;width:145px;height:50px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;">␊
88+
<div className='cnt_col' style="display:flex;flex-direction:column;align-items:flex-start;justify-content:center;gap:8px;width:145px;height:50px;">␊
8989
90-
<span className='line1 instruction1' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#000;font-size:16px;line-height:1;flex-shrink:0px;width:116px;display:inline-flex;font-family:Source Han Sans CN;font-weight:500;">巴拉巴拉小魔仙</span>␊
90+
<span className='line1 instruction1' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#000;font-size:16px;line-height:1;flex-shrink:0px;width:116px;display:inline-flex;">巴拉巴拉小魔仙</span>␊
9191
92-
<span className='line1 txt' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#999;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;font-family:Source Han Sans CN;font-weight:500;">成员: 4000+</span>␊
92+
<span className='line1 txt' style="text-overflow:ellipsis;white-space:nowrap;overflow:hidden;color:#999;font-size:12px;line-height:1;flex-shrink:0px;display:inline-flex;">成员: 4000+</span>␊
9393
9494
</div>␊
9595
96-
<div className='cnt_row2' style="border-width:1px;border-style:solid;border-color:#999;border-radius:12px;display:flex;align-items:flex-start;justify-content:space-between;flex-shrink:0px;gap:-94px;position:absolute;top:3px;left:201px;width:110px;height:23px;padding-left:8px;padding-right:8px;padding-top:4px;padding-bottom:4px;z-index:12;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;">␊
96+
<div className='cnt_row2' style="border-width:1px;border-style:solid;border-color:#999;border-radius:12px;display:flex;align-items:flex-start;justify-content:space-between;flex-shrink:0px;gap:-94px;position:absolute;top:3px;left:201px;width:110px;height:23px;padding-left:8px;padding-right:8px;padding-top:4px;padding-bottom:4px;z-index:12;">␊
9797
9898
{this.getDom()}␊
9999
100-
<img className='icon1' src='//img11.360buyimg.com/img/jfs/t1/175578/35/40256/1981/64f58062Fddaf1a21/f1111d9988a65ccc.png' style="width:15px;height:15px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;"></img>␊
100+
<img className='icon1' src='//img11.360buyimg.com/img/jfs/t1/175578/35/40256/1981/64f58062Fddaf1a21/f1111d9988a65ccc.png' style="width:15px;height:15px;"></img>␊
101101
102-
<span className='instruction2' style="color:#999;font-size:11px;line-height:1.36;text-align:center;flex-shrink:0px;overflow:hidden;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;">slslsl-jsj</span>␊
102+
<span className='instruction2' style="color:#999;font-size:11px;line-height:1.36;text-align:center;flex-shrink:0px;overflow:hidden;">slslsl-jsj</span>␊
103103
104-
<span className='txt1' style="color:#222;font-size:11px;line-height:1.36;text-align:center;flex-shrink:0px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;">复制</span>␊
104+
<span className='txt1' style="color:#222;font-size:11px;line-height:1.36;text-align:center;flex-shrink:0px;">复制</span>␊
105105
106106
</div>␊
107107
108108
</div>␊
109109
110-
<div className='cnt_row3' style="display:flex;align-items:center;justify-content:space-between;gap:-311px;width:311px;height:44px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;text-align:center;">␊
110+
<div className='cnt_row3' style="display:flex;align-items:center;justify-content:space-between;gap:-311px;width:311px;height:44px;">␊
111111
112-
<span className='line2 txt2' style="color:#999;font-size:12px;line-height:1.83;flex-shrink:0px;width:290px;display:inline-flex;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;text-align:center;">␊
112+
<span className='line2 txt2' style="color:#999;font-size:12px;line-height:1.83;flex-shrink:0px;width:290px;display:inline-flex;">␊
113113
114114
团长介绍:售前售后进群售前售后进群售前售后进群售前售后进群VXklsidohh...␊
115115
116116
</span>␊
117117
118-
<img className='img1' src='//img14.360buyimg.com/img/jfs/t1/206378/24/25778/195/64eca527F378f17a2/c1623681708609fd.png' style="width:6px;height:10px;font-family:Source Han Sans CN;font-weight:500;white-space:nowrap;color:#fff;font-size:12px;line-height:1;text-align:center;"></img>␊
118+
<img className='img1' src='//img14.360buyimg.com/img/jfs/t1/206378/24/25778/195/64eca527F378f17a2/c1623681708609fd.png' style="width:6px;height:10px;"></img>␊
119119
120120
</div>␊
121121

__test__/index.spec.mjs.snap

-61 Bytes
Binary file not shown.

src/document.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
use std::collections::HashMap;
22

3-
use ego_tree::Tree;
3+
use selectors::attr::CaseSensitivity;
44
use swc_common::{
55
errors::{ColorConfig, Handler},
66
sync::Lrc,
77
SourceMap, Mark, comments::SingleThreadedComments, Globals, GLOBALS,
88
};
99
use swc_ecma_ast::{EsVersion, Program};
1010
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
11-
use swc_ecma_visit::{VisitWith, FoldWith};
11+
use swc_ecma_visit::{VisitWith, FoldWith, VisitAllWith};
1212
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver};
1313
use swc_ecmascript::transforms::typescript::strip;
1414

1515
use crate::{
16-
scraper::{ElementRef, Node, Selector},
16+
scraper::Element,
1717
visitor::{AstVisitor, JSXRecord, CollectVisitor},
1818
};
1919

2020
pub struct JSXDocument {
21-
pub tree: Tree<Node>,
2221
pub program: Option<Program>,
2322
pub jsx_record: Option<JSXRecord>,
2423
}
2524

2625
impl JSXDocument {
2726
pub fn new() -> Self {
2827
JSXDocument {
29-
tree: Tree::new(Node::Document),
3028
program: None,
3129
jsx_record: None,
3230
}
@@ -70,19 +68,25 @@ impl JSXDocument {
7068
let mut jsx_record: JSXRecord = HashMap::new();
7169
let mut visitor = CollectVisitor::new();
7270
program.visit_with(&mut visitor);
73-
let mut vistor = AstVisitor::new(&program, &mut self.tree, &mut jsx_record, &visitor.export_default_name, &visitor.taro_components);
74-
program.visit_with(&mut vistor);
71+
let mut vistor = AstVisitor::new(&mut jsx_record, &visitor.taro_components);
72+
program.visit_all_with(&mut vistor);
7573
self.program = Some(program);
7674
self.jsx_record = Some(jsx_record);
7775
});
7876
}
7977

80-
pub fn select<'a>(&self, selector: &'a Selector) -> Vec<ElementRef> {
81-
let nodes = self.tree.nodes();
82-
let elements = nodes
83-
.filter_map(|node| ElementRef::wrap(node))
84-
.filter(|element| element.parent().is_some() && selector.matches(&element))
85-
.collect::<Vec<_>>();
86-
elements
78+
pub fn select<'a>(&self, selector: &'a str) -> Vec<Element> {
79+
match self.jsx_record {
80+
Some(ref jsx_record) => {
81+
let mut elements = Vec::new();
82+
for (_, element) in jsx_record.iter() {
83+
if element.has_class(selector, CaseSensitivity::CaseSensitive) {
84+
elements.push(element.clone());
85+
}
86+
}
87+
elements
88+
}
89+
None => Vec::new(),
90+
}
8791
}
8892
}

0 commit comments

Comments
 (0)