From d17f666a8c5b84fa5c55ed1445ab72b31d06e672 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 20 Oct 2025 11:21:55 +0200 Subject: [PATCH] LibWeb: Better CSS inheritance for nodes that represent a pseudo-element When we compute style for elements inside a UA-internal shadow tree that represent a pseudo-element (e.g ::placeholder), we actually run the StyleComputer machinery for (host element :: pseudo-element). While that lets us match the correct selectors, it was incorrectly applying CSS inheritance, since we'd also then inherit from whatever was above the host element in the tree. This patch fixes the issue by introducing an inheritance override in AbstractElement and then using that to force inheritance from whatever is actually directly above in the DOM for these elements instead of jumping all the way up past the host. This fixes an issue where `text-align: center` on input type=text elements would render the main text centered but placeholder text was still left-aligned. --- Libraries/LibWeb/CSS/StyleComputer.cpp | 15 ++++++++-- Libraries/LibWeb/DOM/AbstractElement.cpp | 4 +++ Libraries/LibWeb/DOM/AbstractElement.h | 4 +++ .../expected/empty-editable-shows-cursor.txt | 15 ++++------ .../input-placeholder-with-line-height.txt | 15 ++++------ .../input-placeholder-with-text-align.txt | 27 +++++++++++++++++ .../Layout/expected/input-placeholder.txt | 30 ++++++++----------- .../input-placeholder-with-text-align.html | 8 +++++ 8 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 Tests/LibWeb/Layout/expected/input-placeholder-with-text-align.txt create mode 100644 Tests/LibWeb/Layout/input/input-placeholder-with-text-align.html diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp index 459991f48e5..2f2c9de87cf 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -2461,11 +2461,20 @@ GC::Ptr StyleComputer::compute_style_impl(DOM::AbstractEleme { build_rule_cache_if_needed(); - // Special path for elements that use pseudo element as style selector + // Special path for elements that represent a pseudo-element in some element's internal shadow tree. if (abstract_element.element().use_pseudo_element().has_value()) { auto& element = abstract_element.element(); - auto& parent_element = as(*element.root().parent_or_shadow_host()); - auto style = compute_style({ parent_element, element.use_pseudo_element() }); + auto& host_element = *element.root().parent_or_shadow_host_element(); + + // We have to decide where to inherit from. If the pseudo-element has a parent element, + // we inherit from that. Otherwise, we inherit from the host element in the light DOM. + DOM::AbstractElement abstract_element_for_pseudo_element { host_element, element.use_pseudo_element() }; + if (auto parent_element = element.parent_element()) + abstract_element_for_pseudo_element.set_inheritance_override(*parent_element); + else + abstract_element_for_pseudo_element.set_inheritance_override(host_element); + + auto style = compute_style(abstract_element_for_pseudo_element); // Merge back inline styles if (auto inline_style = element.inline_style()) { diff --git a/Libraries/LibWeb/DOM/AbstractElement.cpp b/Libraries/LibWeb/DOM/AbstractElement.cpp index 1cd2c5ef449..25365eed142 100644 --- a/Libraries/LibWeb/DOM/AbstractElement.cpp +++ b/Libraries/LibWeb/DOM/AbstractElement.cpp @@ -19,6 +19,7 @@ AbstractElement::AbstractElement(GC::Ref element, Optional AbstractElement::parent_element() const Optional AbstractElement::element_to_inherit_style_from() const { + if (m_inheritance_override) + return AbstractElement { *m_inheritance_override }; + GC::Ptr element = m_element->element_to_inherit_style_from(m_pseudo_element); if (!element) diff --git a/Libraries/LibWeb/DOM/AbstractElement.h b/Libraries/LibWeb/DOM/AbstractElement.h index 88379e75a9e..691b4639ed3 100644 --- a/Libraries/LibWeb/DOM/AbstractElement.h +++ b/Libraries/LibWeb/DOM/AbstractElement.h @@ -35,6 +35,8 @@ public: Optional previous_sibling_in_tree_order() { return walk_layout_tree(WalkMethod::PreviousSibling); } bool is_before(AbstractElement const&) const; + void set_inheritance_override(GC::Ref element) { m_inheritance_override = element; } + GC::Ptr computed_properties() const; void set_custom_properties(OrderedHashMap&& custom_properties); @@ -63,6 +65,8 @@ private: GC::Ref m_element; Optional m_pseudo_element; + + GC::Ptr m_inheritance_override; }; } diff --git a/Tests/LibWeb/Layout/expected/empty-editable-shows-cursor.txt b/Tests/LibWeb/Layout/expected/empty-editable-shows-cursor.txt index 042f2ce1d02..2a3ebec554b 100644 --- a/Tests/LibWeb/Layout/expected/empty-editable-shows-cursor.txt +++ b/Tests/LibWeb/Layout/expected/empty-editable-shows-cursor.txt @@ -23,12 +23,10 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] children: not- BlockContainer
at [11,10] flex-item [0+0+0 0 0+0+0] [0+0+0 18 0+0+0] [BFC] children: inline frag 0 from TextNode start: 0, length: 0, rect: [11,10 0x18] baseline: 13.796875 TextNode <#text> (not painted) - BlockContainer <(anonymous)> at [11,10] flex-item [0+0+0 36.84375 0+0+0] [0+0+0 18 0+0+0] [BFC] children: inline - frag 0 from BlockContainer start: 0, length: 0, rect: [11,10 36.84375x18] baseline: 13.796875 - BlockContainer
at [11,10] inline-block [0+0+0 36.84375 0+0+0] [0+0+0 18 0+0+0] [BFC] children: inline - frag 0 from TextNode start: 0, length: 5, rect: [11,10 36.84375x18] baseline: 13.796875 - "hello" - TextNode <#text> (not painted) + BlockContainer
at [11,10] flex-item [0+0+0 196 0+0+0] [0+0+0 18 0+0+0] [BFC] children: inline + frag 0 from TextNode start: 0, length: 5, rect: [11,10 36.84375x18] baseline: 13.796875 + "hello" + TextNode <#text> (not painted) TextNode <#text> (not painted) BlockContainer