Skip to content

Commit

Permalink
0.0.18
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Oct 27, 2023
1 parent 6de1b02 commit 42628bc
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 67 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [0.0.18](https://github.com/NervJS/parse-css-to-stylesheet/compare/v0.0.17...v0.0.18) (2023-10-26)


### Features

* 将样式转换处理拆分出去 ([6de1b02](https://github.com/NervJS/parse-css-to-stylesheet/commit/6de1b029b06a45e2dd5298e4ca883425b5d438c6))



## [0.0.17](https://github.com/NervJS/parse-css-to-stylesheet/compare/v0.0.15...v0.0.17) (2023-10-25)


Expand Down
18 changes: 9 additions & 9 deletions __test__/fixure/Mod.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
color:rgb(255,255,255);
font-size:12px;
line-height:1;
flex-shrink:0px;
flex-shrink:0;
display:inline-flex;
margin-top: -12px;
margin-right: 10;
Expand All @@ -31,7 +31,7 @@
display:flex;
align-items:center;
justify-content:center;
flex-shrink:0px;
flex-shrink:0;
gap:4px;
width:176px !important;
height:24px;
Expand All @@ -57,15 +57,15 @@
color:rgb(0,0,0);
font-size:16px;
line-height:1;
flex-shrink:0px;
flex-shrink:0;
width:116px;
display:inline-flex;
}
.txt {
color:rgb(153,153,153);
font-size:12px;
line-height:1;
flex-shrink:0px;
flex-shrink:0;
display:inline-flex;
}
.cnt_col {
Expand All @@ -86,15 +86,15 @@
font-size:11px;
line-height:1.36;
text-align:center;
flex-shrink:0px;
flex-shrink:0;
overflow:hidden;
}
.txt1 {
color:rgb(34,34,34);
font-size:11px;
line-height:1.36;
text-align:center;
flex-shrink:0px;
flex-shrink:0;
}
.cnt_row2 {
border-width:1px;
Expand All @@ -104,7 +104,7 @@
display:flex;
align-items:flex-start;
justify-content:space-between;
flex-shrink:0px;
flex-shrink:0;
gap:-94px;
position:absolute;
top:3px;
Expand All @@ -130,7 +130,7 @@
color:rgb(153,153,153);
font-size:12px;
line-height:1.83;
flex-shrink:0px;
flex-shrink:0;
width:290px;
display:inline-flex;
}
Expand All @@ -157,7 +157,7 @@
flex-direction:column;
align-items:flex-start;
justify-content:center;
flex-shrink:0px;
flex-shrink:0;
gap:8px;
padding-left:16px;
padding-right:16px;
Expand Down
10 changes: 5 additions & 5 deletions __test__/index.spec.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ Generated by [AVA](https://avajs.dev).
flexBasis: "10%",␊
flexGrow: 2,␊
flexOptions: {},␊
flexShrink: 2,␊
flexShrink: 0,␊
fontSize: "12px",␊
lineHeight: "1",␊
overflow: "hidden",␊
Expand Down Expand Up @@ -402,7 +402,7 @@ Generated by [AVA](https://avajs.dev).
flexBasis: "10%",␊
flexGrow: 2,␊
flexOptions: {},␊
flexShrink: 2,␊
flexShrink: 0,␊
fontSize: "12px",␊
lineHeight: "1",␊
overflow: "hidden",␊
Expand Down Expand Up @@ -510,7 +510,7 @@ Generated by [AVA](https://avajs.dev).
flexBasis: "10%",␊
flexGrow: 2,␊
flexOptions: {},␊
flexShrink: 2,␊
flexShrink: 0,␊
fontSize: "12px",␊
lineHeight: "1",␊
margin: {␊
Expand Down Expand Up @@ -641,7 +641,7 @@ Generated by [AVA](https://avajs.dev).
flexBasis: "10%",␊
flexGrow: 2,␊
flexOptions: {},␊
flexShrink: 2,␊
flexShrink: 0,␊
fontSize: "16px",␊
lineHeight: "1",␊
overflow: "hidden",␊
Expand Down Expand Up @@ -671,7 +671,7 @@ Generated by [AVA](https://avajs.dev).
flexBasis: "10%",␊
flexGrow: 2,␊
flexOptions: {},␊
flexShrink: 2,␊
flexShrink: 0,␊
fontSize: "12px",␊
lineHeight: "1",␊
overflow: "hidden",␊
Expand Down
Binary file modified __test__/index.spec.mjs.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion npm/android-arm-eabi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-android-arm-eabi",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"android"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/android-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-android-arm64",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"android"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-darwin-arm64",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-universal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-darwin-universal",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-darwin-x64",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm-gnueabihf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-linux-arm-gnueabihf",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-linux-arm64-gnu",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-linux-arm64-musl",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-linux-x64-gnu",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-linux-x64-musl",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-arm64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-win32-arm64-msvc",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-ia32-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-win32-ia32-msvc",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet-win32-x64-msvc",
"version": "0.0.17",
"version": "0.0.18",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/parse-css-to-stylesheet",
"version": "0.0.17",
"version": "0.0.18",
"main": "index.js",
"types": "index.d.ts",
"napi": {
Expand Down
56 changes: 37 additions & 19 deletions src/style_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
pub type StyleValue = HashMap<String, StyleValueType>;

pub struct StyleData<'i> {
pub style_record: Rc<RefCell<HashMap<SpanKey, HashMap<String, Property<'i>>>>>,
pub style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Property<'i>)>>>>,
pub all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
}

Expand All @@ -53,14 +53,14 @@ pub struct StyleDeclaration<'i> {
}

pub struct StyleVisitor<'i> {
pub all_style: Rc<RefCell<HashMap<String, Vec<StyleDeclaration<'i>>>>>,
pub all_style: Rc<RefCell<Vec<(String, Vec<StyleDeclaration<'i>>)>>>,
pub document: &'i JSXDocument,
}

impl<'i> StyleVisitor<'i> {
pub fn new(
document: &'i JSXDocument,
all_style: Rc<RefCell<HashMap<String, Vec<StyleDeclaration<'i>>>>>,
all_style: Rc<RefCell<Vec<(String, Vec<StyleDeclaration<'i>>)>>>,
) -> Self {
StyleVisitor {
all_style,
Expand All @@ -80,12 +80,21 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
for index in 0..selectors.len() {
let selector = selectors[index].trim().replace(".", "");
let mut all_style = self.all_style.borrow_mut();
let declarations: &mut Vec<StyleDeclaration<'_>> =
all_style.entry(selector.clone()).or_insert(vec![]);
declarations.push(StyleDeclaration {
specificity: style.selectors.0.get(index).unwrap().specificity(),
declaration: style.declarations.clone(),
});
let decorations = all_style.iter_mut().find(|(id, _)| id == &selector);
if let Some((_, declarations)) = decorations {
declarations.push(StyleDeclaration {
specificity: style.selectors.0.get(index).unwrap().specificity(),
declaration: style.declarations.clone(),
});
} else {
all_style.push((
selector.clone(),
vec![StyleDeclaration {
specificity: style.selectors.0.get(index).unwrap().specificity(),
declaration: style.declarations.clone(),
}],
));
}
}
}
_ => {}
Expand Down Expand Up @@ -520,14 +529,14 @@ pub fn parse_style_properties(properties: &Vec<(String, Property<'_>)>) -> Style
}

pub struct StyleParser<'i> {
pub all_style: Rc<RefCell<HashMap<String, Vec<StyleDeclaration<'i>>>>>,
pub all_style: Rc<RefCell<Vec<(String, Vec<StyleDeclaration<'i>>)>>>,
pub document: &'i JSXDocument,
}

impl<'i> StyleParser<'i> {
pub fn new(document: &'i JSXDocument) -> Self {
StyleParser {
all_style: Rc::new(RefCell::new(HashMap::new())),
all_style: Rc::new(RefCell::new(vec![])),
document,
}
}
Expand All @@ -540,9 +549,9 @@ impl<'i> StyleParser<'i> {

fn calc_style_record<T: Hash + Eq + Clone>(
&self,
style_record: &mut HashMap<T, Vec<StyleDeclaration<'i>>>,
) -> HashMap<T, StyleDeclaration<'i>> {
let mut final_style_record = HashMap::new();
style_record: &mut Vec<(T, Vec<StyleDeclaration<'i>>)>,
) -> Vec<(T, StyleDeclaration<'i>)> {
let mut final_style_record = vec![];
for (id, declarations) in style_record.iter_mut() {
declarations.sort_by(|a, b| a.specificity.cmp(&b.specificity));
let mut final_properties: Vec<Property<'i>> = Vec::new();
Expand Down Expand Up @@ -574,7 +583,7 @@ impl<'i> StyleParser<'i> {
}
}
}
final_style_record.insert(
final_style_record.push((
(*id).clone(),
StyleDeclaration {
specificity: 0,
Expand All @@ -583,7 +592,7 @@ impl<'i> StyleParser<'i> {
important_declarations: vec![],
},
},
);
));
}
final_style_record
}
Expand Down Expand Up @@ -614,11 +623,11 @@ impl<'i> StyleParser<'i> {
property.clone(),
)
})
.collect::<HashMap<_, _>>();
.collect::<Vec<(_, _)>>();
// (selector.to_owned(), parse_style_properties(&properties))
(selector.to_owned(), properties)
})
.collect::<HashMap<_, _>>();
.collect::<Vec<(_, _)>>();

for (selector, style_value) in final_all_style.iter_mut() {
let elements = self.document.select(selector);
Expand All @@ -635,7 +644,16 @@ impl<'i> StyleParser<'i> {
style_value
.iter_mut()
.reduce(|a, b| {
a.extend(b.drain());
for (key, value) in b.iter() {
let has_property_index = a
.iter()
.position(|property| property.0 == key.to_owned());
if let Some(index) = has_property_index {
a[index] = (key.to_owned(), value.clone());
} else {
a.push((key.to_owned(), value.clone()));
}
}
a
})
.unwrap()
Expand Down
Loading

0 comments on commit 42628bc

Please sign in to comment.