Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a179cfc

Browse files
committedApr 29, 2025·
feat: 修复bg的转义词,增加allow_inherit的配置
1 parent df6bb21 commit a179cfc

File tree

10 files changed

+68
-18
lines changed

10 files changed

+68
-18
lines changed
 

‎__test__/fixure/pesudo.scss

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
.hello {
2-
margin-top: 10px !important;
3-
margin: 24px auto;
4-
margin-bottom: env(safe-area-inset-bottom);
2+
height: 100px;
3+
width: 50%;
4+
flex-direction: column;
5+
flex-grow: 1;
6+
color: #00f;
57
}
68

7-
.bbb {
8-
margin-top: 30px;
9+
.child {
10+
background-size: contain;
11+
background-image: linear-gradient(to bottom, #f00, #00f 10%, #0f0);
912
}
1013

11-
.hello .bbb:nth-child(-n + 3) {
12-
border: 2px solid orange;
13-
margin-bottom: 1px;
14-
}
15-
16-
.bbb:nth-child(even) {
17-
background-color: lightyellow;
14+
@media screen and (max-width: 100px) {
15+
.hello {
16+
height: 200px;
17+
width: 100%;
18+
color: #f00;
19+
background-image: radial-gradient(circle, #f00, #00f 10%, #0f0);
20+
}
1821
}

‎__test__/fixure/style.bin

296 Bytes
Binary file not shown.

‎flatbuffers/stylesheet.fbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ table StyleSheet {
132132
medias:[Media];
133133
styles:[Style];
134134
design_width:uint16;
135+
allow_inherit:bool;
135136
}
136137

137138
root_type StyleSheet;

‎flatbuffers/stylesheet_generated.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,8 @@ struct StyleSheet FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
18341834
VT_KEYFRAMES = 6,
18351835
VT_MEDIAS = 8,
18361836
VT_STYLES = 10,
1837-
VT_DESIGN_WIDTH = 12
1837+
VT_DESIGN_WIDTH = 12,
1838+
VT_ALLOW_INHERIT = 14
18381839
};
18391840
const ::flatbuffers::Vector<::flatbuffers::Offset<Styles::Font>> *fonts() const {
18401841
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<Styles::Font>> *>(VT_FONTS);
@@ -1851,6 +1852,9 @@ struct StyleSheet FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
18511852
uint16_t design_width() const {
18521853
return GetField<uint16_t>(VT_DESIGN_WIDTH, 0);
18531854
}
1855+
bool allow_inherit() const {
1856+
return GetField<uint8_t>(VT_ALLOW_INHERIT, 0) != 0;
1857+
}
18541858
bool Verify(::flatbuffers::Verifier &verifier) const {
18551859
return VerifyTableStart(verifier) &&
18561860
VerifyOffset(verifier, VT_FONTS) &&
@@ -1866,6 +1870,7 @@ struct StyleSheet FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
18661870
verifier.VerifyVector(styles()) &&
18671871
verifier.VerifyVectorOfTables(styles()) &&
18681872
VerifyField<uint16_t>(verifier, VT_DESIGN_WIDTH, 2) &&
1873+
VerifyField<uint8_t>(verifier, VT_ALLOW_INHERIT, 1) &&
18691874
verifier.EndTable();
18701875
}
18711876
};
@@ -1889,6 +1894,9 @@ struct StyleSheetBuilder {
18891894
void add_design_width(uint16_t design_width) {
18901895
fbb_.AddElement<uint16_t>(StyleSheet::VT_DESIGN_WIDTH, design_width, 0);
18911896
}
1897+
void add_allow_inherit(bool allow_inherit) {
1898+
fbb_.AddElement<uint8_t>(StyleSheet::VT_ALLOW_INHERIT, static_cast<uint8_t>(allow_inherit), 0);
1899+
}
18921900
explicit StyleSheetBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
18931901
: fbb_(_fbb) {
18941902
start_ = fbb_.StartTable();
@@ -1906,13 +1914,15 @@ inline ::flatbuffers::Offset<StyleSheet> CreateStyleSheet(
19061914
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::flatbuffers::String>>> keyframes = 0,
19071915
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<Styles::Media>>> medias = 0,
19081916
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<Styles::Style>>> styles = 0,
1909-
uint16_t design_width = 0) {
1917+
uint16_t design_width = 0,
1918+
bool allow_inherit = false) {
19101919
StyleSheetBuilder builder_(_fbb);
19111920
builder_.add_styles(styles);
19121921
builder_.add_medias(medias);
19131922
builder_.add_keyframes(keyframes);
19141923
builder_.add_fonts(fonts);
19151924
builder_.add_design_width(design_width);
1925+
builder_.add_allow_inherit(allow_inherit);
19161926
return builder_.Finish();
19171927
}
19181928

@@ -1922,7 +1932,8 @@ inline ::flatbuffers::Offset<StyleSheet> CreateStyleSheetDirect(
19221932
const std::vector<::flatbuffers::Offset<::flatbuffers::String>> *keyframes = nullptr,
19231933
const std::vector<::flatbuffers::Offset<Styles::Media>> *medias = nullptr,
19241934
const std::vector<::flatbuffers::Offset<Styles::Style>> *styles = nullptr,
1925-
uint16_t design_width = 0) {
1935+
uint16_t design_width = 0,
1936+
bool allow_inherit = false) {
19261937
auto fonts__ = fonts ? _fbb.CreateVector<::flatbuffers::Offset<Styles::Font>>(*fonts) : 0;
19271938
auto keyframes__ = keyframes ? _fbb.CreateVector<::flatbuffers::Offset<::flatbuffers::String>>(*keyframes) : 0;
19281939
auto medias__ = medias ? _fbb.CreateVector<::flatbuffers::Offset<Styles::Media>>(*medias) : 0;
@@ -1933,7 +1944,8 @@ inline ::flatbuffers::Offset<StyleSheet> CreateStyleSheetDirect(
19331944
keyframes__,
19341945
medias__,
19351946
styles__,
1936-
design_width);
1947+
design_width,
1948+
allow_inherit);
19371949
}
19381950

19391951
inline bool VerifyValue(::flatbuffers::Verifier &verifier, const void *obj, Value type) {

‎src/json_writer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct JsonWriter {
1919
medias: Vec<StyleMedia>,
2020
fonts: Vec<FontFaceItem>,
2121
design_width: Option<i32>,
22+
allow_inherit: Option<bool>,
2223
}
2324

2425
impl JsonWriter {
@@ -28,13 +29,15 @@ impl JsonWriter {
2829
medias: Vec<StyleMedia>,
2930
fonts: Vec<FontFaceItem>,
3031
design_width: Option<i32>,
32+
allow_inherit: Option<bool>,
3133
) -> Self {
3234
Self {
3335
styles,
3436
keyframes,
3537
medias,
3638
fonts,
3739
design_width,
40+
allow_inherit,
3841
}
3942
}
4043

@@ -325,6 +328,10 @@ impl JsonWriter {
325328
let map = json_value.as_object_mut().unwrap();
326329
map.insert("design_width".to_string(), Value::Number(serde_json::Number::from(design_width)));
327330
}
331+
if let Some(allow_inherit) = self.allow_inherit {
332+
let map = json_value.as_object_mut().unwrap();
333+
map.insert("allow_inherit".to_string(), Value::Bool(allow_inherit));
334+
}
328335

329336
// 打印 JSON 值
330337

‎src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct ParseOptions {
3737
pub platform_string: String,
3838
pub design_width: Option<i32>,
3939
pub output: Option<OutputOptions>,
40+
pub allow_inherit: Option<bool>,
4041
}
4142

4243
#[napi(object)]
@@ -53,6 +54,7 @@ pub fn parse(styles: Vec<String>, options: ParseOptions) -> ParseResult {
5354
_ => Platform::Harmony,
5455
};
5556
let design_width = options.design_width;
57+
let allow_inherit = options.allow_inherit;
5658
let output = options.output.unwrap_or(OutputOptions {
5759
is_bin: Some(false)
5860
});
@@ -73,6 +75,7 @@ pub fn parse(styles: Vec<String>, options: ParseOptions) -> ParseResult {
7375
style_data.all_medias.borrow().clone(),
7476
style_data.all_fonts.borrow().clone(),
7577
design_width,
78+
allow_inherit,
7679
);
7780

7881
let style_json = style_map.to_json();

‎src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub fn main() {
3131
style_data.all_keyframes.borrow().clone(),
3232
style_data.all_medias.borrow().clone(),
3333
style_data.all_fonts.borrow().clone(),
34-
None
34+
None,
35+
None,
3536
);
3637

3738
let style_json = style_map.to_json();

‎src/style_propetries/background_image.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ impl ToExpr for BackgroundImage {
197197
span: DUMMY_SP,
198198
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
199199
key: PropName::Ident(Ident::new("src".into(), DUMMY_SP)),
200-
value: Expr::Lit(Lit::Str(Str::from(src.to_string()))).into(),
200+
value: Expr::Lit(Lit::Str(Str {
201+
span: DUMMY_SP,
202+
value: src.as_str().into(),
203+
raw: None,
204+
})).into(),
201205
})))]
202206
.into(),
203207
}),

‎src/stylesheet_generated.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,7 @@ impl<'a> StyleSheet<'a> {
36193619
pub const VT_MEDIAS: flatbuffers::VOffsetT = 8;
36203620
pub const VT_STYLES: flatbuffers::VOffsetT = 10;
36213621
pub const VT_DESIGN_WIDTH: flatbuffers::VOffsetT = 12;
3622+
pub const VT_ALLOW_INHERIT: flatbuffers::VOffsetT = 14;
36223623

36233624
#[inline]
36243625
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
@@ -3635,6 +3636,7 @@ impl<'a> StyleSheet<'a> {
36353636
if let Some(x) = args.keyframes { builder.add_keyframes(x); }
36363637
if let Some(x) = args.fonts { builder.add_fonts(x); }
36373638
builder.add_design_width(args.design_width);
3639+
builder.add_allow_inherit(args.allow_inherit);
36383640
builder.finish()
36393641
}
36403642

@@ -3674,6 +3676,13 @@ impl<'a> StyleSheet<'a> {
36743676
// which contains a valid value in this slot
36753677
unsafe { self._tab.get::<u16>(StyleSheet::VT_DESIGN_WIDTH, Some(0)).unwrap()}
36763678
}
3679+
#[inline]
3680+
pub fn allow_inherit(&self) -> bool {
3681+
// Safety:
3682+
// Created from valid Table for this object
3683+
// which contains a valid value in this slot
3684+
unsafe { self._tab.get::<bool>(StyleSheet::VT_ALLOW_INHERIT, Some(false)).unwrap()}
3685+
}
36773686
}
36783687

36793688
impl flatbuffers::Verifiable for StyleSheet<'_> {
@@ -3688,6 +3697,7 @@ impl flatbuffers::Verifiable for StyleSheet<'_> {
36883697
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, flatbuffers::ForwardsUOffset<Media>>>>("medias", Self::VT_MEDIAS, false)?
36893698
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, flatbuffers::ForwardsUOffset<Style>>>>("styles", Self::VT_STYLES, false)?
36903699
.visit_field::<u16>("design_width", Self::VT_DESIGN_WIDTH, false)?
3700+
.visit_field::<bool>("allow_inherit", Self::VT_ALLOW_INHERIT, false)?
36913701
.finish();
36923702
Ok(())
36933703
}
@@ -3698,6 +3708,7 @@ pub struct StyleSheetArgs<'a> {
36983708
pub medias: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Media<'a>>>>>,
36993709
pub styles: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Style<'a>>>>>,
37003710
pub design_width: u16,
3711+
pub allow_inherit: bool,
37013712
}
37023713
impl<'a> Default for StyleSheetArgs<'a> {
37033714
#[inline]
@@ -3708,6 +3719,7 @@ impl<'a> Default for StyleSheetArgs<'a> {
37083719
medias: None,
37093720
styles: None,
37103721
design_width: 0,
3722+
allow_inherit: false,
37113723
}
37123724
}
37133725
}
@@ -3738,6 +3750,10 @@ impl<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> StyleSheetBuilder<'a, 'b, A> {
37383750
self.fbb_.push_slot::<u16>(StyleSheet::VT_DESIGN_WIDTH, design_width, 0);
37393751
}
37403752
#[inline]
3753+
pub fn add_allow_inherit(&mut self, allow_inherit: bool) {
3754+
self.fbb_.push_slot::<bool>(StyleSheet::VT_ALLOW_INHERIT, allow_inherit, false);
3755+
}
3756+
#[inline]
37413757
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a, A>) -> StyleSheetBuilder<'a, 'b, A> {
37423758
let start = _fbb.start_table();
37433759
StyleSheetBuilder {
@@ -3760,6 +3776,7 @@ impl core::fmt::Debug for StyleSheet<'_> {
37603776
ds.field("medias", &self.medias());
37613777
ds.field("styles", &self.styles());
37623778
ds.field("design_width", &self.design_width());
3779+
ds.field("allow_inherit", &self.allow_inherit());
37633780
ds.finish()
37643781
}
37653782
}

‎src/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,14 @@ pub fn convert_json_to_flatbuffer(json_str: &str) -> Result<Vec<u8>, serde_json:
596596
}).collect();
597597
let styles = builder.create_vector(&styles);
598598
let design_width = json["design_width"].as_u64().unwrap_or(0) as u16;
599+
let allow_inherit = json["allow_inherit"].as_bool().unwrap_or(false);
599600
let stylesheet = styles::StyleSheet::create(&mut builder, &styles::StyleSheetArgs {
600601
fonts: Some(fonts),
601602
keyframes: None,
602603
medias: Some(medias),
603604
styles: Some(styles),
604605
design_width: design_width,
606+
allow_inherit: allow_inherit,
605607
});
606608

607609
builder.finish(stylesheet, None);

0 commit comments

Comments
 (0)