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 HEX colors #6652

Merged
merged 4 commits into from Dec 15, 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
Expand Up @@ -23,3 +23,8 @@ div {
.foo {
border: thick double #32a1ce;
}

.bar {
color: #123abcFF;
color: #123F;
}
Expand Up @@ -21,3 +21,7 @@ div {
.foo {
border: thick double #32a1ce;
}
.bar {
color: #123abcFF;
color: #123F;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions crates/swc_css_compat/src/compiler/color_hex_alpha.rs
Expand Up @@ -28,12 +28,9 @@ fn shorten_hex_color(value: &str) -> Option<&str> {
let length = value.len();
let chars = value.as_bytes();

if length == 8
&& (chars[6] == b'f' || chars[6] == b'F')
&& (chars[7] == b'f' || chars[7] == b'F')
{
if length == 8 && chars[6] == b'f' && chars[7] == b'f' {
return Some(&value[0..6]);
} else if length == 4 && chars[3] == b'f' || chars[3] == b'F' {
} else if length == 4 && chars[3] == b'f' {
return Some(&value[0..3]);
}

Expand Down
Expand Up @@ -3,7 +3,7 @@ body {
color: red;
color: #f00;
color: #f00;
color: #FC0;
color: #fc0;
color: #0000ff;
color: rgba(0, 0, 255, 0);
color: #FFFFFF;
Expand Down
@@ -1,5 +1,5 @@

x Hex color value '#FFF' should be written into: '#FFFFFF'.
x Hex color value '#fff' should be written into: '#ffffff'.
,-[$DIR/tests/rules/fail/color-hex-length/long/input.css:1:1]
1 | a {
2 | color: #FFF;
Expand Down
@@ -1,5 +1,5 @@

x Hex color value '#FFFFFF' should be written into: '#FFF'.
x Hex color value '#ffffff' should be written into: '#fff'.
,-[$DIR/tests/rules/fail/color-hex-length/short/input.css:1:1]
1 | a {
2 | color: #FFFFFF;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_minifier/src/compressor/color.rs
Expand Up @@ -414,7 +414,7 @@ impl Compressor {
Color::AbsoluteColorBase(AbsoluteColorBase::HexColor(HexColor {
span, value, ..
})) => {
if let Some(value) = self.get_named_color_by_hex(&value.to_ascii_lowercase()) {
if let Some(value) = self.get_named_color_by_hex(value) {
*color = Color::AbsoluteColorBase(AbsoluteColorBase::NamedColorOrTransparent(
Ident {
span: *span,
Expand Down
Expand Up @@ -423,4 +423,9 @@

.class-83 {
color: rgba(100, 100, 100, -300%);
}

.color {
color: #ff0000;
color: #FF0000;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc_css_parser/src/parser/values_and_units/mod.rs
Expand Up @@ -2381,7 +2381,7 @@ where
match bump!(self) {
Token::Hash { value, raw, .. } => Ok(HexColor {
span,
value,
value: value.to_ascii_lowercase(),
raw: Some(raw),
}),
_ => {
Expand Down
10 changes: 5 additions & 5 deletions crates/swc_css_parser/tests/fixture/hex-colors/output.json
Expand Up @@ -174,7 +174,7 @@
"end": 57,
"ctxt": 0
},
"value": "FFFFFF",
"value": "ffffff",
"raw": "FFFFFF"
}
],
Expand Down Expand Up @@ -236,7 +236,7 @@
"end": 97,
"ctxt": 0
},
"value": "0000FFCC",
"value": "0000ffcc",
"raw": "0000FFCC"
}
],
Expand Down Expand Up @@ -329,7 +329,7 @@
"end": 142,
"ctxt": 0
},
"value": "FFF",
"value": "fff",
"raw": "FFF"
}
],
Expand Down Expand Up @@ -422,7 +422,7 @@
"end": 190,
"ctxt": 0
},
"value": "FFFF",
"value": "ffff",
"raw": "FFFF"
}
],
Expand Down Expand Up @@ -484,7 +484,7 @@
"end": 217,
"ctxt": 0
},
"value": "FF",
"value": "ff",
"raw": "FF"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 17,
"ctxt": 0
},
"value": "ABCD",
"value": "abcd",
"raw": "ABCD"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "ABBBCCDD",
"value": "abbbccdd",
"raw": "ABBBCCDD"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABBCCFF",
"value": "aabbccff",
"raw": "AABBCCFF"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABCCCDD",
"value": "aabcccdd",
"raw": "AABCCCDD"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 19,
"ctxt": 0
},
"value": "ABBBCC",
"value": "abbbcc",
"raw": "ABBBCC"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 19,
"ctxt": 0
},
"value": "AABCCC",
"value": "aabccc",
"raw": "AABCCC"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABBCCDE",
"value": "aabbccde",
"raw": "AABBCCDE"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABBCCEF",
"value": "aabbccef",
"raw": "AABBCCEF"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 17,
"ctxt": 0
},
"value": "ABCF",
"value": "abcf",
"raw": "ABCF"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 19,
"ctxt": 0
},
"value": "AABBCD",
"value": "aabbcd",
"raw": "AABBCD"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABBCCDD",
"value": "aabbccdd",
"raw": "AABBCCDD"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABBCDFF",
"value": "aabbcdff",
"raw": "AABBCDFF"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABCCCFF",
"value": "aabcccff",
"raw": "AABCCCFF"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "AABBCDDD",
"value": "aabbcddd",
"raw": "AABBCDDD"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 19,
"ctxt": 0
},
"value": "AABBCC",
"value": "aabbcc",
"raw": "AABBCC"
}
],
Expand Down
Expand Up @@ -112,7 +112,7 @@
"end": 21,
"ctxt": 0
},
"value": "ABBBCCFF",
"value": "abbbccff",
"raw": "ABBBCCFF"
}
],
Expand Down