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: Invalidate an element with CSS custom props on attribute changes #3853

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Libraries/LibWeb/DOM/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2200,10 +2200,20 @@ void Element::invalidate_style_after_attribute_change(FlyString const& attribute
style_invalidation_options.invalidate_self = true;
}

if (style_uses_css_custom_properties()) {
// A css custom property can be hooked on to this element by any attribute
// so invalidate elements and rerender them in that scenario
style_invalidation_options.invalidate_elements_that_use_css_custom_properties = true;
}

if (attribute_name == HTML::AttributeNames::style) {
style_invalidation_options.invalidate_self = true;
// even if we don't have custom properties, the new "style" attribute could add one
style_invalidation_options.invalidate_elements_that_use_css_custom_properties = true;
} else if (attribute_name == HTML::AttributeNames::class_) {
// adding or removing classes can add new custom properties to this element
style_invalidation_options.invalidate_elements_that_use_css_custom_properties = true;

Vector<StringView> old_classes;
Vector<StringView> new_classes;
if (old_value.has_value())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rgb(255, 0, 0)
rgb(0, 128, 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<script src="./include.js"></script>
<style>
body {
--color: red;
}
body.green {
--color: green;
}
</style>
<body></body>
<script>
test(() => {
const div = document.createElement('div');
document.body.appendChild(div);

div.style["backgroundColor"] = "var(--color)";
div.style["width"] = "100px";
div.style["height"] = "100px";

const bgColorBefore = getComputedStyle(div).backgroundColor;

document.body.className = "green";
const bgColorAfter = getComputedStyle(div).backgroundColor;

println(bgColorBefore);
println(bgColorAfter);
});
</script>
Loading