Skip to content

Commit c860d55

Browse files
committed
feat: 增加对env的解析
1 parent 6454e50 commit c860d55

File tree

9 files changed

+557
-562
lines changed

9 files changed

+557
-562
lines changed

Cargo.lock

Lines changed: 413 additions & 501 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"universal": "napi universal",
4848
"format": "run-p format:source format:rs",
4949
"format:rs": "cargo fmt",
50+
"run:rs": "cargo run",
5051
"format:source": "prettier . -w",
5152
"version": "napi version && conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
5253
},

src/parse_style_properties.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use lightningcss::{properties::{custom::TokenOrValue, Property}, stylesheet::Pri
44
use swc_core::{common::DUMMY_SP, ecma::{ast::{self}, utils::quote_ident}};
55
use swc_core::ecma::ast::*;
66

7-
use crate::{style_parser::KeyFrameItem, style_propetries::{animation::Animation, transition::Transition, aspect_ratio::AspectRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, box_shadow::BoxShadow, color::ColorProperty, display::Display, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, style_property_type::CSSPropertyType, style_value_type::StyleValueType, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, transform::Transform, transform_origin::TransformOrigin, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak}};
7+
use crate::{generate_expr_lit_str, style_parser::KeyFrameItem, style_propetries::{animation::Animation, aspect_ratio::AspectRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, box_shadow::BoxShadow, color::ColorProperty, display::Display, expr::Expr, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, style_property_type::{string_to_css_property_type, CSSPropertyType}, style_value_type::StyleValueType, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, transform::Transform, transform_origin::TransformOrigin, transition::Transition, unit::{generate_expr_by_length_value, Platform}, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak}};
88

99
pub fn parse_style_properties(properties: &Vec<(String, Property)>, keyframes_map: Option<Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>>>) -> Vec<StyleValueType> {
1010
let mut final_properties = vec![];
@@ -16,33 +16,11 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>, keyframes_ma
1616
unparsed.value.0.iter().for_each(|item| {
1717
match item {
1818
TokenOrValue::Env(env) => {
19-
// is_env = true;
20-
// let mut args = vec![
21-
// ExprOrSpread {
22-
// spread: None,
23-
// expr: Box::new(ast::Expr::Lit(Lit::Str(env.name.to_css_string(PrinterOptions::default()).unwrap().into())))
24-
// }
25-
// ];
26-
// // env.name.to_css_string(PrinterOptions::default()).unwrap()))
27-
// if env.fallback.is_some() {
28-
// let fallback = env.fallback.as_ref().unwrap().0.get(0);
29-
// if let Some(token) = fallback {
30-
// if let TokenOrValue::Length(length) = token {
31-
// args.push(
32-
// ExprOrSpread {
33-
// spread: None,
34-
// expr: Box::new(generate_expr_by_length_value(length, Platform::Harmony))
35-
// }
36-
// )
37-
// }
38-
// }
39-
// }
40-
// final_properties.push(StyleValueType::Expr(Expr::new(id.to_string(), ast::Expr::Call(CallExpr {
41-
// span: DUMMY_SP,
42-
// callee: Callee::Expr(Box::new(ast::Expr::Ident(quote_ident!(ENV_FUN)))),
43-
// args: args,
44-
// type_args: None
45-
// }))));
19+
is_env = true;
20+
let env_result = value.value_to_css_string(PrinterOptions::default());
21+
if (env_result.is_ok()) {
22+
final_properties.push(StyleValueType::Expr(Expr::new(string_to_css_property_type(id), generate_expr_lit_str!(env_result.unwrap().to_string()))));
23+
}
4624
},
4725
_ => {}
4826
}

src/style_parser.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ impl<'i> StyleVisitor<'i> {
5050
// 收集所有的样式到 all_style 中,以元祖的形式存在 (selector, vec[declaration1, declaration2, ...])
5151
impl<'i> Visitor<'i> for StyleVisitor<'i> {
5252
type Error = Infallible;
53-
const TYPES: VisitTypes = visit_types!(RULES);
5453

5554
fn visit_rule(&mut self, rule: &mut CssRule<'i>) -> Result<(), Self::Error> {
5655
match rule {
@@ -125,20 +124,23 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
125124
}
126125
Ok(())
127126
}
127+
128+
fn visit_types(&self) -> VisitTypes {
129+
visit_types!(RULES)
130+
}
131+
128132
}
129133

130134
pub struct StyleParser<'i> {
131135
pub all_style: Rc<RefCell<Vec<(String, Vec<StyleDeclaration<'i>>)>>>,
132136
pub keyframes: Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>>,
133-
pub platform: Platform
134137
}
135138

136139
impl<'i> StyleParser<'i> {
137-
pub fn new(platform:Platform) -> Self {
140+
pub fn new(_: Platform) -> Self {
138141
StyleParser {
139142
all_style: Rc::new(RefCell::new(vec![])),
140143
keyframes: Rc::new(RefCell::new(HashMap::new())),
141-
platform
142144
}
143145
}
144146

src/style_propetries/background.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use lightningcss::{
22
properties::{ background::Background as LNBackground, Property },
3-
stylesheet::PrinterOptions,
4-
targets::{ Features, Targets },
5-
traits::ToCss,
63
values::color::CssColor,
74
};
85
use smallvec::SmallVec;
96

10-
use crate::{ generate_expr_lit_color, generate_expr_lit_str, generate_invalid_expr };
7+
use crate::generate_expr_lit_color;
118

129
use super::{
1310
background_image::{ parse_background_image_item, BackgroundImage },
@@ -16,7 +13,7 @@ use super::{
1613
background_size::{ parse_background_size_item, BackgroundSize },
1714
style_property_type::CSSPropertyType,
1815
traits::ToExpr,
19-
unit::{ convert_color_keywords_to_hex, PropertyTuple },
16+
unit::PropertyTuple,
2017
};
2118

2219
fn parse_background(background: &SmallVec<[LNBackground<'_>; 1]>) -> Background {

src/style_propetries/expr.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
// use swc_core::ecma::ast;
1+
use swc_core::ecma::ast;
22

3-
// use super::{traits::ToExpr, unit::PropertyTuple};
3+
use super::{style_property_type::CSSPropertyType, traits::ToExpr, unit::PropertyTuple};
44

55

66

7-
// #[derive(Debug, Clone)]
8-
// pub struct Expr(String, ast::Expr);
7+
#[derive(Debug, Clone)]
8+
pub struct Expr(CSSPropertyType, ast::Expr);
99

10-
// impl Expr {
11-
// pub fn new(id: String, value: ast::Expr) -> Self {
12-
// Self(id, value)
13-
// }
14-
// }
10+
impl Expr {
11+
pub fn new(id: CSSPropertyType, value: ast::Expr) -> Self {
12+
Self(id, value)
13+
}
14+
}
1515

1616

17-
// impl ToExpr for Expr {
18-
// fn to_expr(&self) -> PropertyTuple {
19-
// PropertyTuple::One(
20-
// self.0.clone(),
21-
// self.1.clone()
22-
// )
23-
// }
24-
// }
25-
17+
impl ToExpr for Expr {
18+
fn to_expr(&self) -> PropertyTuple {
19+
PropertyTuple::One(
20+
self.0,
21+
self.1.clone()
22+
)
23+
}
24+
}

src/style_propetries/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod traits;
55
pub mod macros;
66
pub mod unit;
77
pub mod normal;
8-
// pub mod expr;
8+
pub mod expr;
99
pub mod length_value;
1010
pub mod size;
1111
pub mod max_size;

src/style_propetries/style_property_type.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,109 @@ pub enum CSSPropertyType {
104104
Padding = 99,
105105
BorderRadius = 100,
106106
}
107+
108+
pub fn string_to_css_property_type(property: &str) -> CSSPropertyType {
109+
match property {
110+
"alignContent" => CSSPropertyType::AlignContent,
111+
"justifyContent" => CSSPropertyType::JustifyContent,
112+
"alignItems" => CSSPropertyType::AlignItems,
113+
"alignSelf" => CSSPropertyType::AlignSelf,
114+
"flexBasis" => CSSPropertyType::FlexBasis,
115+
"flexDirection" => CSSPropertyType::FlexDirection,
116+
"flexGrow" => CSSPropertyType::FlexGrow,
117+
"flexShrink" => CSSPropertyType::FlexShrink,
118+
"flexWrap" => CSSPropertyType::FlexWrap,
119+
"aspectRatio" => CSSPropertyType::AspectRatio,
120+
"display" => CSSPropertyType::Display,
121+
"columnGap" => CSSPropertyType::ColumnGap,
122+
"rowGap" => CSSPropertyType::RowGap,
123+
"marginLeft" => CSSPropertyType::MarginLeft,
124+
"marginRight" => CSSPropertyType::MarginRight,
125+
"marginTop" => CSSPropertyType::MarginTop,
126+
"marginBottom" => CSSPropertyType::MarginBottom,
127+
"paddingLeft" => CSSPropertyType::PaddingLeft,
128+
"paddingRight" => CSSPropertyType::PaddingRight,
129+
"paddingTop" => CSSPropertyType::PaddingTop,
130+
"paddingBottom" => CSSPropertyType::PaddingBottom,
131+
"width" => CSSPropertyType::Width,
132+
"minWidth" => CSSPropertyType::MinWidth,
133+
"maxWidth" => CSSPropertyType::MaxWidth,
134+
"height" => CSSPropertyType::Height,
135+
"minHeight" => CSSPropertyType::MinHeight,
136+
"maxHeight" => CSSPropertyType::MaxHeight,
137+
"overflow" => CSSPropertyType::Overflow,
138+
"fontSize" => CSSPropertyType::FontSize,
139+
"fontStyle" => CSSPropertyType::FontStyle,
140+
"fontFamily" => CSSPropertyType::FontFamily,
141+
"fontWeight" => CSSPropertyType::FontWeight,
142+
"lineHeight" => CSSPropertyType::LineHeight,
143+
"letterSpacing" => CSSPropertyType::LetterSpacing,
144+
"verticalAlign" => CSSPropertyType::VerticalAlign,
145+
"textAlign" => CSSPropertyType::TextAlign,
146+
"textDecoration" => CSSPropertyType::TextDecoration,
147+
"textShadow" => CSSPropertyType::TextShadow,
148+
"textOverflow" => CSSPropertyType::TextOverflow,
149+
"textTransform" => CSSPropertyType::TextTransform,
150+
"color" => CSSPropertyType::Color,
151+
"backgroundColor" => CSSPropertyType::BackgroundColor,
152+
"backgroundImage" => CSSPropertyType::BackgroundImage,
153+
"backgroundPosition" => CSSPropertyType::BackgroundPosition,
154+
"backgroundSize" => CSSPropertyType::BackgroundSize,
155+
"backgroundRepeat" => CSSPropertyType::BackgroundRepeat,
156+
"borderTopColor" => CSSPropertyType::BorderTopColor,
157+
"borderRightColor" => CSSPropertyType::BorderRightColor,
158+
"borderBottomColor" => CSSPropertyType::BorderBottomColor,
159+
"borderLeftColor" => CSSPropertyType::BorderLeftColor,
160+
"borderTopStyle" => CSSPropertyType::BorderTopStyle,
161+
"borderRightStyle" => CSSPropertyType::BorderRightStyle,
162+
"borderBottomStyle" => CSSPropertyType::BorderBottomStyle,
163+
"borderLeftStyle" => CSSPropertyType::BorderLeftStyle,
164+
"borderTopWidth" => CSSPropertyType::BorderTopWidth,
165+
"borderRightWidth" => CSSPropertyType::BorderRightWidth,
166+
"borderBottomWidth" => CSSPropertyType::BorderBottomWidth,
167+
"borderLeftWidth" => CSSPropertyType::BorderLeftWidth,
168+
"borderTopLeftRadius" => CSSPropertyType::BorderTopLeftRadius,
169+
"borderTopRightRadius" => CSSPropertyType::BorderTopRightRadius,
170+
"borderBottomLeftRadius" => CSSPropertyType::BorderBottomLeftRadius,
171+
"borderBottomRightRadius" => CSSPropertyType::BorderBottomRightRadius,
172+
"boxShadow" => CSSPropertyType::BoxShadow,
173+
"zIndex" => CSSPropertyType::ZIndex,
174+
"position" => CSSPropertyType::Position,
175+
"top" => CSSPropertyType::Top,
176+
"right" => CSSPropertyType::Right,
177+
"bottom" => CSSPropertyType::Bottom,
178+
"left" => CSSPropertyType::Left,
179+
"visibility" => CSSPropertyType::Visibility,
180+
"opacity" => CSSPropertyType::Opacity,
181+
"transform" => CSSPropertyType::Transform,
182+
"transformOrigin" => CSSPropertyType::TransformOrigin,
183+
"animationKeyFrames" => CSSPropertyType::AnimationKeyFrames,
184+
"animationDuration" => CSSPropertyType::AnimationDuration,
185+
"animationTimingFunction" => CSSPropertyType::AnimationTimingFunction,
186+
"animationDelay" => CSSPropertyType::AnimationDelay,
187+
"animationIterationCount" => CSSPropertyType::AnimationIterationCount,
188+
"content" => CSSPropertyType::Content,
189+
"wordBreak" => CSSPropertyType::WordBreak,
190+
"webkitLineClamp" => CSSPropertyType::WebkitLineClamp,
191+
"animationFillMode" => CSSPropertyType::AnimationFillMode,
192+
"backgroundPositionX" => CSSPropertyType::BackgroundPositionX,
193+
"backgroundPositionY" => CSSPropertyType::BackgroundPositionY,
194+
"transition" => CSSPropertyType::Transition,
195+
"transitionProperty" => CSSPropertyType::TransitionProperty,
196+
"transitionDuration" => CSSPropertyType::TransitionDuration,
197+
"transitionTimingFunction" => CSSPropertyType::TransitionTimingFunction,
198+
"transitionDelay" => CSSPropertyType::TransitionDelay,
199+
"whiteSpace" => CSSPropertyType::WhiteSpace,
200+
"textDecorationLine" => CSSPropertyType::TextDecorationLine,
201+
"textDecorationThickness" => CSSPropertyType::TextDecorationThickness,
202+
"textDecorationStyle" => CSSPropertyType::TextDecorationStyle,
203+
"textDecorationColor" => CSSPropertyType::TextDecorationColor,
204+
"animationName" => CSSPropertyType::AnimationName,
205+
"borderWidth" => CSSPropertyType::BorderWidth,
206+
"borderColor" => CSSPropertyType::BorderColor,
207+
"margin" => CSSPropertyType::Margin,
208+
"padding" => CSSPropertyType::Padding,
209+
"borderRadius" => CSSPropertyType::BorderRadius,
210+
_ => CSSPropertyType::Invalid,
211+
}
212+
}

src/style_propetries/style_value_type.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
use crate::generate_expr_based_on_platform;
33

4-
use super::{animation::Animation, transition::Transition, aspect_ratio::AspectRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, box_shadow::BoxShadow, color::ColorProperty, display::Display, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, traits::{ToExpr, ToStyleValue}, transform::Transform, transform_origin::TransformOrigin, unit::{Platform, PropertyTuple}, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak};
4+
use super::{animation::Animation, aspect_ratio::AspectRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, box_shadow::BoxShadow, color::ColorProperty, display::Display, expr::Expr, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, traits::{ToExpr, ToStyleValue}, transform::Transform, transform_origin::TransformOrigin, transition::Transition, unit::{Platform, PropertyTuple}, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak};
55

66

77
#[derive(Debug, Clone)]
88
pub enum StyleValueType {
99
Normal(Normal),
10-
// Expr(Expr),
10+
Expr(Expr),
1111
NumberProperty(NumberProperty),
1212
ColorProperty(ColorProperty),
1313
LengthValueProperty(LengthValueProperty),
@@ -63,9 +63,9 @@ impl ToStyleValue for StyleValueType {
6363
StyleValueType::Normal(value) => {
6464
generate_expr_based_on_platform!(platform, value)
6565
},
66-
// StyleValueType::Expr(value) => {
67-
// generate_expr_based_on_platform!(platform, value)
68-
// },
66+
StyleValueType::Expr(value) => {
67+
generate_expr_based_on_platform!(platform, value)
68+
},
6969
StyleValueType::NumberProperty(value) => {
7070
generate_expr_based_on_platform!(platform, value)
7171
},

0 commit comments

Comments
 (0)