Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css/parser): normalize at-rule name #6676

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 0 additions & 8 deletions crates/swc_css_codegen/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_css_compat/src/compiler/custom_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -29,7 +29,7 @@ impl CustomMediaHandler {
pub(crate) fn process_rules(&mut self, n: &mut Vec<Rule>) {
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;
}

Expand Down
21 changes: 11 additions & 10 deletions crates/swc_css_minifier/src/compressor/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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")
)
}

Expand Down Expand Up @@ -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")
}
7 changes: 4 additions & 3 deletions crates/swc_css_parser/src/parser/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
"end": 136,
"ctxt": 0
},
"value": "IMPORT",
"value": "import",
"raw": "IMPORT"
},
"prelude": {
Expand Down Expand Up @@ -2016,7 +2016,7 @@
"end": 904,
"ctxt": 0
},
"value": "IMPORT",
"value": "import",
"raw": "IMPORT"
},
"prelude": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@
"end": 330,
"ctxt": 0
},
"value": "SUPPORTS",
"value": "supports",
"raw": "SUPPORTS"
},
"prelude": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@
"end": 146,
"ctxt": 0
},
"value": "--custom",
"value": "custom",
"raw": "--custom"
},
"prelude": {
Expand Down Expand Up @@ -515,7 +515,7 @@
"end": 168,
"ctxt": 0
},
"value": "--library1-custom",
"value": "library1-custom",
"raw": "--library1-custom"
},
"prelude": {
Expand Down
8 changes: 2 additions & 6 deletions crates/swc_css_prefixer/src/prefixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down