Skip to content

Fix linear-gradient direction conversion for legacy vendor-prefixed values#936

Merged
devongovett merged 5 commits intoparcel-bundler:masterfrom
lucasweng:fix-linear-gradient-to-keyword-conversion
Apr 25, 2025
Merged

Fix linear-gradient direction conversion for legacy vendor-prefixed values#936
devongovett merged 5 commits intoparcel-bundler:masterfrom
lucasweng:fix-linear-gradient-to-keyword-conversion

Conversation

@lucasweng
Copy link
Contributor

@lucasweng lucasweng commented Mar 22, 2025

indoc! { r#"
.foo {
background: -webkit-linear-gradient(#ff0f0e, #7773ff);
background: -webkit-linear-gradient(top, #ff0f0e, #7773ff);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some test results include an extra top keyword in vendor-prefixed linear-gradient values because the original input omits the direction. When unspecified, the new syntax defaults to to bottom, which is converted to top in legacy syntax.

@lucasweng lucasweng force-pushed the fix-linear-gradient-to-keyword-conversion branch from c0231b9 to 38b1855 Compare March 22, 2025 09:53
@lucasweng lucasweng force-pushed the fix-linear-gradient-to-keyword-conversion branch from 38b1855 to b30f0d3 Compare March 22, 2025 09:59
@yisibl
Copy link
Contributor

yisibl commented Apr 10, 2025

There are more test cases in Autoprefixer, and I think it would be nice to be able to synchronize them here for testing.

@lucasweng
Copy link
Contributor Author

Good idea! I’ll add more cases from https://github.com/postcss/autoprefixer/blob/main/test/cases/gradient.css

@lucasweng lucasweng changed the title Fix linear-gradient to keyword conversion Fix linear-gradient direction conversion for legacy vendor-prefixed values Apr 11, 2025
.foo {
background-image: -webkit-gradient(linear, 0 0, 100% 0, from(red), to(#00f));
background-image: -webkit-linear-gradient(90deg, red, #00f);
background-image: -webkit-linear-gradient(0deg, red, #00f);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new angle conversion works quite well. Take these test cases for example:

background-image: linear-gradient(90deg, red, blue);

test

  • Before (angle unchanged): -webkit-linear-gradient(90deg, red, #00f);
    before

  • After background-image: -webkit-linear-gradient(0deg, red, #00f);
    Screenshot 2025-04-11 at 22 19 06

background: linear-gradient(350.5deg, white, black) from the first test case ported from Autoprefixer

test2

  • Before (angle unchanged): background: -webkit-linear-gradient(350.5deg, #fff, #000);
    Screenshot 2025-04-11 at 22 36 16

  • After background: -webkit-linear-gradient(99.5deg, #fff, #000)
    Screenshot 2025-04-11 at 22 37 41

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, more bugs in lightningcss can be fixed with more comprehensive test cases.

let number = (number * 1000.0).round() / 1000.0;
LineDirection::Angle(Angle::Deg(number))
}
Angle::Rad(_) | Angle::Grad(_) | Angle::Turn(_) => LineDirection::Angle(angle),
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to adjust these angles as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I’ll look into fixing the angle conversion for those.

Comment on lines +13662 to +13678
.grad {
background: -webkit-linear-gradient(89.1deg, #fff, #000);
background: -o-linear-gradient(89.1deg, #fff, #000);
background: linear-gradient(1grad, #fff, #000);
}

.rad {
background: -webkit-linear-gradient(32.704deg, #fff, #000);
background: -o-linear-gradient(32.704deg, #fff, #000);
background: linear-gradient(57.2958deg, #fff, #000);
}

.turn {
background: -webkit-linear-gradient(342deg, #fff, #000);
background: -o-linear-gradient(342deg, #fff, #000);
background: linear-gradient(.3turn, #fff, #000);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Angle conversion now handles grad, rad, and turn.

background: linear-gradient(1grad, white, black);

test1

Output background: -webkit-linear-gradient(89.1deg, #fff, #000);
out1

background: linear-gradient(1rad, white, black);

test2

Output background: -webkit-linear-gradient(32.704deg, #fff, #000);
out2

background: linear-gradient(0.3turn, white, black);

test3

Output background: -webkit-linear-gradient(342deg, #fff, #000);
out3

@devongovett devongovett merged commit c5cbcfa into parcel-bundler:master Apr 25, 2025
3 checks passed
@lucasweng lucasweng deleted the fix-linear-gradient-to-keyword-conversion branch April 25, 2025 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linear-gradient to keyword conversion error

3 participants