Skip to content

Commit

Permalink
fix(css): Handle custom properties properly (#2869)
Browse files Browse the repository at this point in the history
swc_css_codegen:
 - Fix code generation of custom properties.

swc_css_parser:
 - Fix parsing of custom properties.
  • Loading branch information
alexander-akait authored Nov 27, 2021
1 parent ffcb357 commit c94735e
Show file tree
Hide file tree
Showing 28 changed files with 13,457 additions and 1,951 deletions.
23 changes: 17 additions & 6 deletions crates/swc_css_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,27 @@ where
fn emit_declaration(&mut self, n: &Declaration) -> Result {
emit!(self, n.property);
punct!(self, ":");
formatting_space!(self);

self.emit_list(
&n.value,
ListFormat::SpaceDelimited | ListFormat::SingleLine,
)?;
let is_custom_property = n.property.value.starts_with("--");

if let Some(tok) = n.important {
if !is_custom_property {
formatting_space!(self);
}

let format = match is_custom_property {
true => ListFormat::NotDelimited,
false => ListFormat::SpaceDelimited | ListFormat::SingleLine,
};

self.emit_list(&n.value, format)?;

if let Some(tok) = n.important {
if !is_custom_property {
formatting_space!(self);
}

punct!(self, tok, "!");

self.wr.write_raw(Some(tok), "important")?;
}

Expand Down
8 changes: 8 additions & 0 deletions crates/swc_css_parser/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ macro_rules! tok {
swc_css_ast::Token::Function { .. }
};

("bad-string") => {
swc_css_ast::Token::BadStr { .. }
};

("bad-url") => {
swc_css_ast::Token::BadUrl { .. }
};

("[") => {
swc_css_ast::Token::LBracket
};
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_css_parser/src/parser/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,11 @@ where
Err(_) => {
self.input.reset(&state);

PseudoSelectorChildren::Tokens(self.parse_any_value(false)?)
PseudoSelectorChildren::Tokens(self.parse_any_value()?)
}
}
}
_ => PseudoSelectorChildren::Tokens(self.parse_any_value(false)?),
_ => PseudoSelectorChildren::Tokens(self.parse_any_value()?),
};

expect!(self, ")");
Expand Down Expand Up @@ -769,7 +769,7 @@ where
_ => unreachable!(),
};

let children = self.parse_any_value(false)?;
let children = self.parse_any_value()?;

expect!(self, ")");

Expand Down
42 changes: 26 additions & 16 deletions crates/swc_css_parser/src/parser/style_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,35 @@ where

self.input.skip_ws()?;

let property = self.parse()?;
let property: Ident = self.parse()?;

self.input.skip_ws()?;

expect!(self, ":");

let mut value = vec![];
let mut end = self.input.cur_span()?.hi;
let mut value = vec![];

if !is!(self, EOF) {
let ctx = Ctx {
allow_operation_in_value: false,
recover_from_property_value: true,
..self.ctx
};
let (mut parsed_value, parsed_last_pos) = self.with_ctx(ctx).parse_property_values()?;
match &*property.value {
str if str.starts_with("--") => {
let tokens = Value::Lazy(self.parse_declaration_value()?);

value.append(&mut parsed_value);
end = parsed_last_pos;
value.push(tokens);
}
_ => {
let ctx = Ctx {
allow_operation_in_value: false,
recover_from_property_value: true,
..self.ctx
};
let (parsed_value, parsed_last_pos) =
self.with_ctx(ctx).parse_property_values()?;

value.extend(parsed_value);
end = parsed_last_pos;
}
}
}

let important = self.parse_bang_important()?;
Expand Down Expand Up @@ -229,21 +239,21 @@ where
}
}

impl<I> Parse<Vec<Declaration>> for Parser<I>
impl<I> Parse<Vec<DeclarationBlockItem>> for Parser<I>
where
I: ParserInput,
{
fn parse(&mut self) -> PResult<Vec<Declaration>> {
self.parse_declaration_list()
fn parse(&mut self) -> PResult<Vec<DeclarationBlockItem>> {
self.parse_decl_block_items()
}
}

impl<I> Parse<Vec<DeclarationBlockItem>> for Parser<I>
impl<I> Parse<Vec<Declaration>> for Parser<I>
where
I: ParserInput,
{
fn parse(&mut self) -> PResult<Vec<DeclarationBlockItem>> {
self.parse_decl_block_items()
fn parse(&mut self) -> PResult<Vec<Declaration>> {
self.parse_declaration_list()
}
}

Expand Down
Loading

1 comment on commit c94735e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: c94735e Previous: ffcb357 Ratio
base_tr_fixer 23172 ns/iter (± 296) 23265 ns/iter (± 381) 1.00
base_tr_resolver_and_hygiene 127665 ns/iter (± 24296) 125377 ns/iter (± 24290) 1.02
codegen_es2015 51398 ns/iter (± 224) 50830 ns/iter (± 202) 1.01
codegen_es2016 51598 ns/iter (± 302) 51046 ns/iter (± 267) 1.01
codegen_es2017 51873 ns/iter (± 286) 51199 ns/iter (± 449) 1.01
codegen_es2018 51618 ns/iter (± 270) 51001 ns/iter (± 297) 1.01
codegen_es2019 51552 ns/iter (± 352) 51128 ns/iter (± 261) 1.01
codegen_es2020 51520 ns/iter (± 270) 51341 ns/iter (± 334) 1.00
codegen_es3 51570 ns/iter (± 310) 51030 ns/iter (± 305) 1.01
codegen_es5 51603 ns/iter (± 286) 50930 ns/iter (± 531) 1.01
full_es2015 178528017 ns/iter (± 7079156) 179566300 ns/iter (± 6242886) 0.99
full_es2016 143896251 ns/iter (± 5435775) 145024621 ns/iter (± 4599175) 0.99
full_es2017 149835388 ns/iter (± 8507530) 148508301 ns/iter (± 7946790) 1.01
full_es2018 148395830 ns/iter (± 7068735) 147734743 ns/iter (± 7589642) 1.00
full_es2019 148202205 ns/iter (± 6856171) 146787518 ns/iter (± 8578365) 1.01
full_es2020 146849697 ns/iter (± 7618616) 145922830 ns/iter (± 7954601) 1.01
full_es3 207130599 ns/iter (± 13947150) 203770224 ns/iter (± 17207966) 1.02
full_es5 193266717 ns/iter (± 11854561) 188965076 ns/iter (± 11748558) 1.02
parser 651842 ns/iter (± 24096) 651476 ns/iter (± 18759) 1.00
ser_ast_node 149 ns/iter (± 3) 149 ns/iter (± 4) 1
ser_serde 162 ns/iter (± 3) 162 ns/iter (± 2) 1
emit_colors 17686917 ns/iter (± 23055808) 18694650 ns/iter (± 25440454) 0.95
emit_large 115314981 ns/iter (± 174453176) 115154457 ns/iter (± 177696592) 1.00
base_clone 2300586 ns/iter (± 120554) 2274764 ns/iter (± 43268) 1.01
fold_span 3828964 ns/iter (± 166427) 3826134 ns/iter (± 101089) 1.00
fold_span_panic 4077567 ns/iter (± 297386) 4004610 ns/iter (± 52234) 1.02
visit_mut_span 2823255 ns/iter (± 151693) 2775797 ns/iter (± 36506) 1.02
visit_mut_span_panic 2894904 ns/iter (± 100374) 2829217 ns/iter (± 19156) 1.02
ast_clone 16882 ns/iter (± 151) 16741 ns/iter (± 142) 1.01
ast_clone_to_stable 51026 ns/iter (± 314) 50764 ns/iter (± 537) 1.01
ast_clone_to_stable_then_to_unstable 92004 ns/iter (± 616) 91912 ns/iter (± 652) 1.00
json_deserialize 1937863 ns/iter (± 5282) 1940492 ns/iter (± 11554) 1.00
json_serialize 84862 ns/iter (± 288) 85024 ns/iter (± 360) 1.00
boxing_boxed 143 ns/iter (± 0) 150 ns/iter (± 1) 0.95
boxing_boxed_clone 68 ns/iter (± 1) 67 ns/iter (± 0) 1.01
boxing_unboxed 138 ns/iter (± 0) 129 ns/iter (± 0) 1.07
boxing_unboxed_clone 63 ns/iter (± 0) 64 ns/iter (± 0) 0.98
time_10 307 ns/iter (± 1) 315 ns/iter (± 4) 0.97
time_15 648 ns/iter (± 2) 710 ns/iter (± 3) 0.91
time_20 1257 ns/iter (± 2) 1447 ns/iter (± 7) 0.87
time_40 7365 ns/iter (± 23) 7140 ns/iter (± 15) 1.03
time_5 99 ns/iter (± 0) 101 ns/iter (± 1) 0.98
time_60 16320 ns/iter (± 31) 16362 ns/iter (± 35) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.