Skip to content

Commit 75c5b2e

Browse files
committed
fix: 修复webkit适配错误,background:none\border:none解析异常
1 parent 894ff5e commit 75c5b2e

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export interface ParseOptions {
99
export interface ParseResult {
1010
code: string
1111
}
12-
export function parse(styles: Array<string>, options: ParseOptions): ParseResult
12+
export declare function parse(styles: Array<string>, options: ParseOptions): ParseResult

src/parse_style_properties.rs

Lines changed: 17 additions & 5 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::{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}};
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}, utils::lowercase_first};
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![];
@@ -32,9 +32,21 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>, keyframes_ma
3232
continue;
3333
}
3434

35-
36-
let property_name = id.as_str();
37-
match property_name {
35+
let mut property_name = id.as_str();
36+
37+
// 移除部分厂商前缀: Webkit, Moz, 并且把首字母小写
38+
if property_name.starts_with("Webkit") {
39+
property_name = &property_name[6..];
40+
} else if property_name.starts_with("Moz") {
41+
property_name = &property_name[3..];
42+
}
43+
44+
// 将property_name首字母小写
45+
let mut property_name = property_name.to_string();
46+
lowercase_first(&mut property_name);
47+
48+
49+
match property_name.as_str() {
3850
// 基础样式
3951
"alignContent" => {
4052
final_properties.push(StyleValueType::FlexAlign(FlexAlign::from((id.to_string(), value))));
@@ -226,7 +238,7 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>, keyframes_ma
226238
"zIndex" => {
227239
final_properties.push(StyleValueType::Normal(Normal::new(CSSPropertyType::ZIndex, value.value_to_css_string(PrinterOptions::default()).unwrap())));
228240
}
229-
"WebkitLineClamp" => {
241+
"lineClamp" => {
230242
final_properties.push(StyleValueType::Normal(Normal::new(CSSPropertyType::WebkitLineClamp, value.value_to_css_string(PrinterOptions::default()).unwrap())));
231243
}
232244
"wordBreak" => {

src/style_propetries/background.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use lightningcss::{
2-
properties::{ background::Background as LNBackground, Property },
3-
values::color::CssColor,
2+
printer::PrinterOptions, properties::{ background::Background as LNBackground, Property }, traits::ToCss, values::color::CssColor
43
};
54
use smallvec::SmallVec;
65

76
use crate::generate_expr_lit_color;
87

98
use super::{
10-
background_image::{ parse_background_image_item, BackgroundImage },
9+
background_image::{ parse_background_image_item, BackgroundImage, BackgroundImageKind },
1110
background_position::{ parse_background_position_item, BackgroundPosition },
1211
background_repeat::{ parse_background_repeat_item, BackgroundRepeat },
1312
background_size::{ parse_background_size_item, BackgroundSize },
@@ -35,6 +34,12 @@ fn parse_background(background: &SmallVec<[LNBackground<'_>; 1]>) -> Background
3534
if item.color != CssColor::default() {
3635
background_color = Some(item.color.clone());
3736
}
37+
38+
if item.to_css_string(PrinterOptions::default()).unwrap() == "none" {
39+
// 如果是none,就清空所有的值
40+
background_image = vec![BackgroundImageKind::String("".to_string())];
41+
background_color = Some(item.color.clone());
42+
}
3843
}
3944
let mut bg = Background::new();
4045
if background_image.len() > 0 {

src/style_propetries/background_image.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
149149
Gradient::RepeatingConic(_) => None,
150150
Gradient::WebKitGradient(_) => None,
151151
}
152-
}
152+
},
153+
Image::None => {
154+
Some(BackgroundImageKind::String("".to_string()))
155+
},
153156
_ => None,
154157
}
155158
}

src/style_propetries/border.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use lightningcss::properties::Property;
2-
1+
use lightningcss::{printer::PrinterOptions, properties::{border::{self, BorderSideWidth}, Property}, traits::ToCss, values::{color, length}};
32
use swc_core::ecma::ast::*;
43
use crate::{generate_expr_by_length, generate_invalid_expr, generate_expr_by_border_side_width, generate_expr_by_line_style, generate_expr_lit_color };
54

@@ -38,6 +37,21 @@ impl From<(String, &Property<'_>)> for Border {
3837
color: Some(color),
3938
width: Some(width),
4039
};
40+
41+
if value.to_css_string(PrinterOptions::default()).unwrap() == "none" {
42+
let mut style = BorderStyle::new("borderStyle".to_string());
43+
style.set_all(border::LineStyle::Solid);
44+
let mut color = BorderColor::new("borderColor".to_string());
45+
color.set_all(color::CssColor::default());
46+
let mut width = BorderWidth::new("borderWidth".to_string());
47+
width.set_all(BorderSideWidth::Length(length::Length::Value(length::LengthValue::Px(0.0))));
48+
border = Border {
49+
id: prop.0.clone(),
50+
style: Some(style),
51+
color: Some(color),
52+
width: Some(width),
53+
};
54+
}
4155
}
4256
Property::BorderTop(value) => {
4357
let mut style = BorderStyle::new("borderTopStyle".to_string());

src/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ use swc_core::ecma::{ast::{ArrayLit, CallExpr, Expr, Function, JSXMemberExpr, JS
77

88
use crate::{constants::SelectorType, style_propetries::unit::Platform};
99

10+
pub fn lowercase_first(s: &mut str) {
11+
if let Some(c) = s.get_mut(0..1) {
12+
c.make_ascii_lowercase();
13+
}
14+
}
15+
1016
pub fn to_camel_case(s: &str, is_first: bool) -> String {
1117
let mut result = String::new();
1218
let mut next_cap = if is_first { true } else { false };

0 commit comments

Comments
 (0)