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

LibWeb/CSS: Implement background-blend-mode CSS property #3157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vukanovics
Copy link

Hi friends!
This patch implements the background-blend-mode CSS property:
ladybird-patch-1
Addresses a part of #3140:
ladybird-patch-2
There two immediate problems this patch has:

  1. I didn't sort the new definitions in Libraries/LibWeb/CSS/Keywords.json - please let me know what would be the best method to sort these :)
  2. In Libraries/LibWeb/Layout/Node.cpp:471, layers missing a blend mode are assigned the Normal blend mode. This isn't correct: "If a property doesn’t have enough comma-separated values to match the number of layers, the UA must calculate its used value by repeating the list of values until there are enough.".
    Unfortunately, this code doesn't handle the lack of a normal blend mode layer under non-normal layers properly.
    I'm looking into this, but I'd appreciate help :)

@vukanovics vukanovics requested a review from AtkinsSJ as a code owner January 6, 2025 12:30
@ladybird-bot
Copy link
Collaborator

Hello!

One or more of the commit messages in this PR do not match the Ladybird code submission policy, please check the lint_commits CI job for more details on which commits were flagged and why.
Please do not close this PR and open another, instead modify your commit message(s) with git commit --amend and force push those changes to update this PR.

@vukanovics vukanovics force-pushed the background-blend-mode branch from ad5eac6 to 80212fe Compare January 6, 2025 12:32
@vukanovics
Copy link
Author

Looking further into the specification, this paragraph also is currently incorrectly implemented: "Each background layer must blend with the element’s background layer that is below it and the element’s background color. Background layers must not blend with the content that is behind the element, instead they must act as if they are rendered into an isolated group."

Copy link
Member

@AtkinsSJ AtkinsSJ left a comment

Choose a reason for hiding this comment

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

Welcome! This is nice work. :^)

I think I've answered the two questions you had. As for "Background layers must not blend with the content that is behind the element", you can try fixing that if you like, but putting a // FIXME comment about it somewhere appropriate is fine too.

As for the commit, this feels like at least 2 separate things: Adding blend-mode support to the painting code, and then adding the CSS background-blend-mode property. If you can split that into two commits it'd be good.

Also, it would be nice to have a test for this! There are some WPT tests linked from the spec which you can import with the script described here.

Comment on lines +461 to +475
"darken",
"multiply",
"color-burn",
"lighten",
"screen",
"color-dodge",
"overlay",
"soft-light",
"hard-light",
"difference",
"exclusion",
"hue",
"saturation",
"color",
"luminosity"
Copy link
Member

Choose a reason for hiding this comment

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

As you asked: This whole file should be alphabetically sorted.

Comment on lines +8416 to 8417
case PropertyID::BackgroundBlendMode:
case PropertyID::BackgroundAttachment:
Copy link
Member

Choose a reason for hiding this comment

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

This should be alphabetical too.

@@ -79,6 +79,24 @@
"local",
"scroll"
],
"blend-mode": [
Copy link
Member

Choose a reason for hiding this comment

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

This should go after background-box.

Comment on lines +469 to +473
auto const num_blend_modes = blend_modes.is_value_list() ? blend_modes.as_value_list().size() : 1;

if (layer_index >= num_blend_modes) {
layer.blend_mode = CSS::BlendMode::Normal;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Re your 2nd question: These lines aren't necessary. value_for_layer() already deals with the background layers and repeating the list if there aren't enough, so you can just use blend_mode_value directly.

If that's not working, let me know and I'll try and figure out what's wrong.

Comment on lines +474 to +477
switch (blend_mode_value->to_keyword()) {
case CSS::Keyword::Normal:
layer.blend_mode = CSS::BlendMode::Normal;
break;
Copy link
Member

Choose a reason for hiding this comment

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

Adding an enum to Enums.json automatically generates a function that converts this for you. The whole switch can be replaced with:

layer.blend_mode = CSS::keyword_to_blend_mode(blend_mode_value->to_keyword()).value_or(CSS::BlendMode::Normal);

It returns Optional, so the value_or() is equivalent to having the default: branch in the switch.

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.

3 participants