From 25c4c2397ec4482ea8cbcdf9fbcebd69823b58ac Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 13 Aug 2025 15:21:11 +0100 Subject: [PATCH] LibWeb/CSS: Add StyleValue getter to CSSStyleDeclarations Will be used by StylePropertyMap, as that wants a StyleValue to reify, not a string representation. --- Libraries/LibWeb/CSS/CSSDescriptors.cpp | 8 ++++++++ Libraries/LibWeb/CSS/CSSDescriptors.h | 1 + Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 1 + Libraries/LibWeb/CSS/CSSStyleProperties.cpp | 10 ++++++++++ Libraries/LibWeb/CSS/CSSStyleProperties.h | 1 + 5 files changed, 21 insertions(+) diff --git a/Libraries/LibWeb/CSS/CSSDescriptors.cpp b/Libraries/LibWeb/CSS/CSSDescriptors.cpp index e7415a93bee..5f7407d82e7 100644 --- a/Libraries/LibWeb/CSS/CSSDescriptors.cpp +++ b/Libraries/LibWeb/CSS/CSSDescriptors.cpp @@ -289,6 +289,14 @@ bool CSSDescriptors::has_property(StringView property_name) const return descriptor(*descriptor_id) != nullptr; } +RefPtr CSSDescriptors::get_property_style_value(StringView property_name) const +{ + auto descriptor_id = descriptor_id_from_string(m_at_rule_id, property_name); + if (!descriptor_id.has_value()) + return nullptr; + return descriptor(*descriptor_id); +} + bool is_shorthand(AtRuleID at_rule, DescriptorID descriptor) { if (at_rule == AtRuleID::Page && descriptor == DescriptorID::Margin) diff --git a/Libraries/LibWeb/CSS/CSSDescriptors.h b/Libraries/LibWeb/CSS/CSSDescriptors.h index 1da05ad8ddb..12ed30264eb 100644 --- a/Libraries/LibWeb/CSS/CSSDescriptors.h +++ b/Libraries/LibWeb/CSS/CSSDescriptors.h @@ -34,6 +34,7 @@ public: virtual WebIDL::ExceptionOr set_css_text(StringView) override; virtual bool has_property(StringView property_name) const override; + virtual RefPtr get_property_style_value(StringView property_name) const override; protected: CSSDescriptors(JS::Realm&, AtRuleID, Vector); diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 8f29294e46e..2504edb5ffe 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -58,6 +58,7 @@ public: void set_is_updating(bool value) { m_updating = value; } virtual bool has_property(StringView property_name) const = 0; + virtual RefPtr get_property_style_value(StringView property_name) const = 0; protected: enum class Computed : u8 { diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp index a03bf863c8d..63bd2df71b4 100644 --- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp @@ -434,6 +434,16 @@ bool CSSStyleProperties::has_property(StringView property_name) const return get_property_internal(*property_id).has_value(); } +RefPtr CSSStyleProperties::get_property_style_value(StringView property_name) const +{ + auto property_id = property_id_from_string(property_name); + if (!property_id.has_value()) + return nullptr; + if (auto style_property = get_property_internal(*property_id); style_property.has_value()) + return style_property->value; + return nullptr; +} + // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue Optional CSSStyleProperties::get_property_internal(PropertyID property_id) const { diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.h b/Libraries/LibWeb/CSS/CSSStyleProperties.h index 22f9be74f5b..b3ddd4b6878 100644 --- a/Libraries/LibWeb/CSS/CSSStyleProperties.h +++ b/Libraries/LibWeb/CSS/CSSStyleProperties.h @@ -50,6 +50,7 @@ public: size_t custom_property_count() const { return m_custom_properties.size(); } virtual bool has_property(StringView property_name) const override; + virtual RefPtr get_property_style_value(StringView property_name) const override; String css_float() const; WebIDL::ExceptionOr set_css_float(StringView);