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 declaration and important #6663

Merged
merged 3 commits into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 8 additions & 8 deletions crates/swc_css_codegen/src/lib.rs
Expand Up @@ -2763,27 +2763,27 @@ fn minify_hex_color(value: &str) -> String {
if length == 6 || chars[6] == b'f' && chars[7] == b'f' {
let mut minified = String::with_capacity(3);

minified.push((chars[0] as char).to_ascii_lowercase());
minified.push((chars[2] as char).to_ascii_lowercase());
minified.push((chars[4] as char).to_ascii_lowercase());
minified.push(chars[0] as char);
minified.push(chars[2] as char);
minified.push(chars[4] as char);

return minified;
}
// 8 -> 4
else if length == 8 && chars[6] == chars[7] {
let mut minified = String::with_capacity(4);

minified.push((chars[0] as char).to_ascii_lowercase());
minified.push((chars[2] as char).to_ascii_lowercase());
minified.push((chars[4] as char).to_ascii_lowercase());
minified.push((chars[6] as char).to_ascii_lowercase());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Forgot to remove it too, we don't need it, because we already normalize hex colors

minified.push(chars[0] as char);
minified.push(chars[2] as char);
minified.push(chars[4] as char);
minified.push(chars[6] as char);

return minified;
}
}
}

value.to_ascii_lowercase()
value.to_string()
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
}

fn serialize_string(value: &str) -> String {
Expand Down
12 changes: 6 additions & 6 deletions crates/swc_css_minifier/src/compressor/declaration.rs
Expand Up @@ -6,8 +6,8 @@ use super::Compressor;

impl Compressor {
pub(super) fn compress_declaration(&self, declaration: &mut Declaration) {
if let DeclarationName::Ident(Ident { value, .. }) = &declaration.name {
match value.to_ascii_lowercase() {
if let DeclarationName::Ident(Ident { value: name, .. }) = &declaration.name {
match *name {
js_word!("display") if declaration.value.len() > 1 => {
let mut outside = None;
let mut inside = None;
Expand Down Expand Up @@ -616,12 +616,12 @@ impl Compressor {

fn compress_from_initial(&self, declaration: &mut Declaration, span: Span) {
let name = if let DeclarationName::Ident(Ident { value, .. }) = &declaration.name {
value.to_ascii_lowercase()
value
} else {
return;
};

match name {
match *name {
js_word!("accent-color")
| js_word!("align-self")
| js_word!("animation-timeline")
Expand Down Expand Up @@ -1282,12 +1282,12 @@ impl Compressor {

fn _compress_to_initial(&self, declaration: &mut Declaration) {
let name = if let DeclarationName::Ident(Ident { value, .. }) = &declaration.name {
value.to_ascii_lowercase()
value
} else {
return;
};

match name {
match *name {
js_word!("background-clip") | js_word!("mask-clip") | js_word!("mask-origin") => {
if let Some(ComponentValue::Ident(box Ident { value, span, .. })) =
declaration.value.get(0)
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_modules/src/lib.rs
Expand Up @@ -316,7 +316,7 @@ where
}

if let DeclarationName::Ident(name) = &n.name {
match name.value.to_ascii_lowercase() {
match name.value {
js_word!("animation") => {
let mut can_change = true;

Expand Down
8 changes: 6 additions & 2 deletions crates/swc_css_parser/src/parser/syntax/mod.rs
Expand Up @@ -613,7 +613,11 @@ where
let name = if is_dashed_ident {
DeclarationName::DashedIdent(self.parse()?)
} else {
DeclarationName::Ident(self.parse()?)
let mut ident: Ident = self.parse()?;

ident.value = ident.value.to_ascii_lowercase();

DeclarationName::Ident(ident)
};
let mut declaration = Declaration {
span: Default::default(),
Expand Down Expand Up @@ -742,7 +746,7 @@ where
};
let value = Ident {
span: important_ident.span,
value: value.0,
value: value.0.to_ascii_lowercase(),
raw: Some(value.1),
};

Expand Down
Expand Up @@ -9257,7 +9257,7 @@
"end": 4781,
"ctxt": 0
},
"value": "DISPLAY",
"value": "display",
"raw": "DISPLAY"
},
"value": [
Expand Down
Expand Up @@ -453,7 +453,7 @@
"end": 219,
"ctxt": 0
},
"value": "IMPORTANT",
"value": "important",
"raw": "IMPORTANT"
}
}
Expand Down Expand Up @@ -501,7 +501,7 @@
"end": 251,
"ctxt": 0
},
"value": "IMPORTANT",
"value": "important",
"raw": "IMPORTANT"
}
}
Expand Down Expand Up @@ -930,7 +930,7 @@
"end": 519,
"ctxt": 0
},
"value": "iMpOrTaNt",
"value": "important",
"raw": "iMpOrTaNt"
}
}
Expand Down
Expand Up @@ -651,7 +651,7 @@
"end": 234,
"ctxt": 0
},
"value": "CONTENT",
"value": "content",
"raw": "CONTENT"
},
"value": [
Expand Down Expand Up @@ -685,7 +685,7 @@
"end": 255,
"ctxt": 0
},
"value": "MARGIN-LEFT",
"value": "margin-left",
"raw": "MARGIN-LEFT"
},
"value": [
Expand Down
Expand Up @@ -55,7 +55,7 @@
"end": 17,
"ctxt": 0
},
"value": "DISPLAY",
"value": "display",
"raw": "DISPLAY"
},
"value": [
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_prefixer/src/prefixer.rs
Expand Up @@ -1594,7 +1594,7 @@ impl VisitMut for Prefixer {
}};
}

let property_name = &*name.to_ascii_lowercase();
let property_name: &str = name;

match property_name {
"appearance" => {
Expand Down
Expand Up @@ -5,3 +5,13 @@ a {
b {
APPEARANCE: AUTO;
}

c {
-WEBKIT-APPEARANCE: NONE;
APPEARANCE: NONE;
}

d {
-webkit-appearance: NONE;
APPEARANCE: NONE;
}
Expand Up @@ -10,3 +10,15 @@ b {
-ms-appearance: AUTO;
APPEARANCE: AUTO;
}
c {
-WEBKIT-APPEARANCE: NONE;
-moz-appearance: NONE;
-ms-appearance: NONE;
APPEARANCE: NONE;
}
d {
-webkit-appearance: NONE;
-moz-appearance: NONE;
-ms-appearance: NONE;
APPEARANCE: NONE;
}
Expand Up @@ -6,3 +6,11 @@ b {
-webkit-appearance: AUTO;
APPEARANCE: AUTO;
}
c {
-WEBKIT-APPEARANCE: NONE;
APPEARANCE: NONE;
}
d {
-webkit-appearance: NONE;
APPEARANCE: NONE;
}