LibWeb: Skip full document style update in getComputedStyle if possible

Before calling update_style() for a getComputedStyle property access,
we now check whether the target element actually needs a style update
by walking the flat tree ancestor chain. If neither the element nor any
of its ancestors have dirty style bits, and there are no document-level
reasons to recalculate style, we skip the update_style() call entirely.

We walk the flat tree (not the DOM tree) because style inheritance
follows slot assignment -- slotted elements inherit from their assigned
slot, not their DOM parent.

This avoids unnecessary work when scripts access computed style
properties on elements whose styles are already up-to-date, which is a
common pattern on the web.
This commit is contained in:
Andreas Kling
2026-02-12 19:35:56 +01:00
committed by Andreas Kling
parent b270b2cacb
commit 4a7ca32af0
Notes: github-actions[bot] 2026-02-13 09:24:01 +00:00
3 changed files with 36 additions and 1 deletions

View File

@@ -560,7 +560,7 @@ Optional<StyleProperty> CSSStyleProperties::get_direct_property(PropertyNameAndI
// always need update_layout() to ensure both style and layout tree are up to date.
abstract_element.document().update_layout(DOM::UpdateLayoutReason::ResolvedCSSStyleDeclarationProperty);
layout_node = abstract_element.layout_node();
} else {
} else if (abstract_element.document().element_needs_style_update(abstract_element)) {
// Just ensure styles are up to date.
abstract_element.document().update_style();
}