From 7f8ba9c22c9910e03a3b6122dd4ca4324314bbb5 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Mon, 19 Dec 2022 11:30:06 +0300 Subject: [PATCH] feat(css/parser): Normalize at-rule names (#6676) --- crates/swc_css_codegen/tests/fixture.rs | 8 ------- .../src/compiler/custom_media.rs | 4 ++-- .../swc_css_minifier/src/compressor/rules.rs | 21 ++++++++++--------- .../swc_css_parser/src/parser/syntax/mod.rs | 7 ++++--- .../tests/fixture/at-rule/import/output.json | 4 ++-- .../fixture/at-rule/supports/output.json | 2 +- .../tests/fixture/dashed-ident/output.json | 4 ++-- crates/swc_css_prefixer/src/prefixer.rs | 8 ++----- 8 files changed, 24 insertions(+), 34 deletions(-) diff --git a/crates/swc_css_codegen/tests/fixture.rs b/crates/swc_css_codegen/tests/fixture.rs index 9965716ecefa..cbfdae7cad2e 100644 --- a/crates/swc_css_codegen/tests/fixture.rs +++ b/crates/swc_css_codegen/tests/fixture.rs @@ -113,14 +113,6 @@ fn run(input: &Path, minify: bool) { struct NormalizeTest; impl VisitMut for NormalizeTest { - fn visit_mut_at_rule(&mut self, n: &mut AtRule) { - n.visit_mut_children_with(self); - - if let AtRuleName::Ident(ident) = &mut n.name { - ident.value = ident.value.to_lowercase().into(); - } - } - fn visit_mut_media_query(&mut self, n: &mut MediaQuery) { n.visit_mut_children_with(self); diff --git a/crates/swc_css_compat/src/compiler/custom_media.rs b/crates/swc_css_compat/src/compiler/custom_media.rs index 503c2c3bdeff..f84ffdfc5ddc 100644 --- a/crates/swc_css_compat/src/compiler/custom_media.rs +++ b/crates/swc_css_compat/src/compiler/custom_media.rs @@ -18,7 +18,7 @@ pub(super) struct CustomMediaHandler { impl CustomMediaHandler { pub(crate) fn store_custom_media(&mut self, n: &mut AtRule) { if let AtRuleName::Ident(name) = &n.name { - if name.value.eq_ignore_ascii_case(&js_word!("custom-media")) { + if name.value == js_word!("custom-media") { if let Some(box AtRulePrelude::CustomMediaPrelude(prelude)) = &mut n.prelude { self.medias.push(prelude.take()); } @@ -29,7 +29,7 @@ impl CustomMediaHandler { pub(crate) fn process_rules(&mut self, n: &mut Vec) { n.retain(|n| match n { Rule::AtRule(n) => { - if matches!(&n.name, AtRuleName::Ident(ident) if ident.value.eq_ignore_ascii_case(&js_word!("custom-media"))) { + if matches!(&n.name, AtRuleName::Ident(ident) if ident.value == js_word!("custom-media")) { return false; } diff --git a/crates/swc_css_minifier/src/compressor/rules.rs b/crates/swc_css_minifier/src/compressor/rules.rs index 900def2024f0..29c72cec18dd 100644 --- a/crates/swc_css_minifier/src/compressor/rules.rs +++ b/crates/swc_css_minifier/src/compressor/rules.rs @@ -53,8 +53,8 @@ impl Visit for CompatibilityChecker { impl Compressor { fn get_at_rule_name(&self, at_rule: &AtRule) -> JsWord { match &at_rule.name { - AtRuleName::Ident(Ident { value, .. }) => value.to_ascii_lowercase(), - AtRuleName::DashedIdent(DashedIdent { value, .. }) => value.to_ascii_lowercase(), + AtRuleName::Ident(Ident { value, .. }) => value.clone(), + AtRuleName::DashedIdent(DashedIdent { value, .. }) => value.clone(), } } @@ -329,13 +329,13 @@ impl Compressor { _ => return false, }; - matches_eq_ignore_ascii_case!( - name, - js_word!("media"), - js_word!("supports"), - js_word!("container"), - js_word!("layer"), - js_word!("nest") + matches!( + *name, + js_word!("media") + | js_word!("supports") + | js_word!("container") + | js_word!("layer") + | js_word!("nest") ) } @@ -643,6 +643,7 @@ impl Compressor { } } +#[inline] fn need_keep_by_name(name: &JsWord) -> bool { - matches_eq_ignore_ascii_case!(name, js_word!("color-profile")) + *name == js_word!("color-profile") } diff --git a/crates/swc_css_parser/src/parser/syntax/mod.rs b/crates/swc_css_parser/src/parser/syntax/mod.rs index 9f22658e622e..7e62d22675cc 100644 --- a/crates/swc_css_parser/src/parser/syntax/mod.rs +++ b/crates/swc_css_parser/src/parser/syntax/mod.rs @@ -148,16 +148,17 @@ where unreachable!() } }; - let name = if at_keyword_name.0.starts_with("--") { + let is_dashed_ident = at_keyword_name.0.starts_with("--"); + let name = if is_dashed_ident { AtRuleName::DashedIdent(DashedIdent { span: Span::new(span.lo + BytePos(1), span.hi, Default::default()), - value: at_keyword_name.0, + value: at_keyword_name.0[2..].into(), raw: Some(at_keyword_name.1), }) } else { AtRuleName::Ident(Ident { span: Span::new(span.lo + BytePos(1), span.hi, Default::default()), - value: at_keyword_name.0, + value: at_keyword_name.0.to_ascii_lowercase(), raw: Some(at_keyword_name.1), }) }; diff --git a/crates/swc_css_parser/tests/fixture/at-rule/import/output.json b/crates/swc_css_parser/tests/fixture/at-rule/import/output.json index 6635166ad75a..9fcaa41f6130 100644 --- a/crates/swc_css_parser/tests/fixture/at-rule/import/output.json +++ b/crates/swc_css_parser/tests/fixture/at-rule/import/output.json @@ -272,7 +272,7 @@ "end": 136, "ctxt": 0 }, - "value": "IMPORT", + "value": "import", "raw": "IMPORT" }, "prelude": { @@ -2016,7 +2016,7 @@ "end": 904, "ctxt": 0 }, - "value": "IMPORT", + "value": "import", "raw": "IMPORT" }, "prelude": { diff --git a/crates/swc_css_parser/tests/fixture/at-rule/supports/output.json b/crates/swc_css_parser/tests/fixture/at-rule/supports/output.json index aeca4634604e..f09a10f36d01 100644 --- a/crates/swc_css_parser/tests/fixture/at-rule/supports/output.json +++ b/crates/swc_css_parser/tests/fixture/at-rule/supports/output.json @@ -825,7 +825,7 @@ "end": 330, "ctxt": 0 }, - "value": "SUPPORTS", + "value": "supports", "raw": "SUPPORTS" }, "prelude": { diff --git a/crates/swc_css_parser/tests/fixture/dashed-ident/output.json b/crates/swc_css_parser/tests/fixture/dashed-ident/output.json index bd470678586b..20e8d17b559a 100644 --- a/crates/swc_css_parser/tests/fixture/dashed-ident/output.json +++ b/crates/swc_css_parser/tests/fixture/dashed-ident/output.json @@ -456,7 +456,7 @@ "end": 146, "ctxt": 0 }, - "value": "--custom", + "value": "custom", "raw": "--custom" }, "prelude": { @@ -515,7 +515,7 @@ "end": 168, "ctxt": 0 }, - "value": "--library1-custom", + "value": "library1-custom", "raw": "--library1-custom" }, "prelude": { diff --git a/crates/swc_css_prefixer/src/prefixer.rs b/crates/swc_css_prefixer/src/prefixer.rs index b90649a40d2b..a8b006e779a9 100644 --- a/crates/swc_css_prefixer/src/prefixer.rs +++ b/crates/swc_css_prefixer/src/prefixer.rs @@ -743,9 +743,7 @@ impl VisitMut for Prefixer { at_rule.visit_mut_children_with(self); match &at_rule.name { - AtRuleName::Ident(Ident { span, value, .. }) - if value.as_ref().eq_ignore_ascii_case("viewport") => - { + AtRuleName::Ident(Ident { span, value, .. }) if value == "viewport" => { if should_prefix("@-o-viewport", self.env, false) { self.add_at_rule( Prefix::Ms, @@ -778,9 +776,7 @@ impl VisitMut for Prefixer { ); } } - AtRuleName::Ident(Ident { span, value, .. }) - if value.as_ref().eq_ignore_ascii_case("keyframes") => - { + AtRuleName::Ident(Ident { span, value, .. }) if value == "keyframes" => { if should_prefix("@-webkit-keyframes", self.env, false) { self.add_at_rule( Prefix::Webkit,