Skip to content

Commit 59b0e58

Browse files
committed
alpha() can be evaluated even in unknown functions
1 parent fdad065 commit 59b0e58

3 files changed

Lines changed: 64 additions & 9 deletions

File tree

src/lib.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19335,8 +19335,8 @@ mod tests {
1933519335
);
1933619336

1933719337
// Supplementary testing
19338-
// TODO: 目前的 calc 不支持除数为 0,导致 alpha / 0 报错,而 alpha / 1 可以通过。
19339-
// 依赖:https://github.com/parcel-bundler/lightningcss/pull/1122
19338+
// TODO: calc does not support division by zero yet, so `alpha / 0` fails while `alpha / 1` works.
19339+
// Depends on: https://github.com/parcel-bundler/lightningcss/pull/1122
1934019340
// test(
1934119341
// "alpha(from green / calc(alpha / 0))",
1934219342
// "rgb(0, 128, 0)",
@@ -19390,6 +19390,7 @@ mod tests {
1939019390
// Supplementary testing
1939119391
// nested alpha() precision.
1939219392
test("alpha(from alpha(from red / 0.5) / calc(alpha + 0.25))", "#ff0000bf");
19393+
test("alpha(from alpha(from red / -0.5) / calc(alpha + 0.6))", "#f009");
1939319394
test("alpha(from alpha(from green / 0%) / 100%)", "green");
1939419395

1939519396
// Unresolved relative colors keep their tokens.
@@ -19432,9 +19433,33 @@ mod tests {
1943219433
"color(from alpha(from color(display-p3 1 0 0) / 0.5) display-p3 r g b / alpha)",
1943319434
"color(display-p3 1 0 0 / 0.5)",
1943419435
);
19436+
test(
19437+
"color(from alpha(from red / 1.5) srgb r g b / calc(alpha - 0.2))",
19438+
"color(srgb 1 0 0 / 0.8)", // alpha = 1 - 0.2
19439+
);
19440+
19441+
// Test in image()
19442+
minify_test(
19443+
".foo { mask: image(alpha(from red / 1))}",
19444+
".foo{mask:image(red)}",
19445+
);
19446+
19447+
// Test in linear-gradient()
19448+
minify_test(
19449+
".foo { mask: linear-gradient(90deg, alpha(from red / 0%), red) }",
19450+
".foo{mask:linear-gradient(90deg,#f000,red)}",
19451+
);
19452+
// Compare relative color
19453+
// TODO: Support <color-interpolation-method>
19454+
minify_test(
19455+
".foo { mask: linear-gradient(90deg in hsl longer hue, rgb(from red r g b / 0), red) }",
19456+
".foo{mask:linear-gradient(90deg in hsl longer hue, #f000, red)}",
19457+
);
19458+
minify_test(
19459+
".foo { mask: linear-gradient(90deg in hsl longer hue, alpha(from red / 0%), red) }",
19460+
".foo{mask:linear-gradient(90deg in hsl longer hue, #f000, red)}",
19461+
);
1943519462

19436-
// TODO:添加一个带有 fallback 的输出,需要设置浏览器为较旧的版本
19437-
// 最终输出:.foo{color:#ff0f0e;color:color(display-p3 1 0 0)}
1943819463
minify_test(
1943919464
".foo { color: alpha(from color(display-p3 1 0 0) / 0.5) }",
1944019465
".foo{color:color(display-p3 1 0 0/.5)}",
@@ -21217,6 +21242,36 @@ mod tests {
2121721242
}
2121821243
}
2121921244

21245+
#[test]
21246+
fn test_relative_alpha_color_fallbacks() {
21247+
prefix_test(
21248+
".foo { color: alpha(from color(srgb 1 0 0) / 0.5) }",
21249+
indoc! { r#"
21250+
.foo {
21251+
color: #ff000080;
21252+
color: color(srgb 1 0 0 / .5);
21253+
}
21254+
"#},
21255+
Browsers {
21256+
chrome: Some(90 << 16),
21257+
..Browsers::default()
21258+
},
21259+
);
21260+
prefix_test(
21261+
".foo { color: alpha(from color(display-p3 1 0 0) / 0.5) }",
21262+
indoc! { r#"
21263+
.foo {
21264+
color: #ff0f0e80;
21265+
color: color(display-p3 1 0 0 / .5);
21266+
}
21267+
"#},
21268+
Browsers {
21269+
chrome: Some(90 << 16),
21270+
..Browsers::default()
21271+
},
21272+
);
21273+
}
21274+
2122021275
#[test]
2122121276
fn test_color_mix() {
2122221277
minify_test(

src/properties/custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ fn try_parse_color_token<'i, 't>(
470470
input: &mut Parser<'i, 't>,
471471
) -> Option<CssColor> {
472472
match_ignore_ascii_case! { &*f,
473-
"rgb" | "rgba" | "hsl" | "hsla" | "hwb" | "lab" | "lch" | "oklab" | "oklch" | "color" | "color-mix" | "light-dark" => {
473+
"rgb" | "rgba" | "hsl" | "hsla" | "hwb" | "lab" | "lch" | "oklab" | "oklch" | "color" | "color-mix" | "light-dark" | "alpha" => {
474474
let s = input.state();
475475
input.reset(&state);
476476
if let Ok(color) = CssColor::parse(input) {

src/values/color.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ impl RelativeComponentParser {
794794
}
795795
}
796796

797-
fn alpha(alpha: f32) -> Self {
797+
fn alpha_only(alpha: f32) -> Self {
798798
Self {
799799
names: ("", "", ""),
800800
components: (0.0, 0.0, 0.0, alpha),
@@ -1272,15 +1272,15 @@ fn parse_relative_alpha_from<'i, 't>(
12721272
return Ok(CssColor::LightDark(Box::new(light), Box::new(dark)));
12731273
}
12741274

1275-
let alpha = from.alpha().map_err(|_| input.new_custom_error(ParserError::InvalidValue))?;
1275+
let from_alpha = from.alpha().map_err(|_| input.new_custom_error(ParserError::InvalidValue))?;
12761276
let alpha = if input.try_parse(|input| input.expect_delim('/')).is_ok() {
12771277
let parser = ComponentParser {
12781278
allow_none: true,
1279-
from: Some(RelativeComponentParser::alpha(alpha)),
1279+
from: Some(RelativeComponentParser::alpha_only(from_alpha)),
12801280
};
12811281
parse_number_or_percentage(input, &parser, 1.0)?.clamp(0.0, 1.0)
12821282
} else {
1283-
alpha
1283+
from_alpha
12841284
};
12851285

12861286
Ok(from.with_alpha(alpha))

0 commit comments

Comments
 (0)