From 37ea9a4ce3fda051dd991f84a79ebc47fef5da52 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 13 Aug 2025 13:07:28 +0100 Subject: [PATCH] LibWeb/CSS: Add `has_property()` to CSSStyleDeclaration This isn't part of the CSSOM API directly, but will be used by StylePropertyMapReadOnly.has() to avoid doing the work to serialize a string version of the property's value, just to throw it away again. --- Libraries/LibWeb/CSS/CSSDescriptors.cpp | 8 ++++++++ Libraries/LibWeb/CSS/CSSDescriptors.h | 2 ++ Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 2 ++ Libraries/LibWeb/CSS/CSSStyleProperties.cpp | 8 ++++++++ Libraries/LibWeb/CSS/CSSStyleProperties.h | 2 ++ 5 files changed, 22 insertions(+) diff --git a/Libraries/LibWeb/CSS/CSSDescriptors.cpp b/Libraries/LibWeb/CSS/CSSDescriptors.cpp index 425003c9845..e7415a93bee 100644 --- a/Libraries/LibWeb/CSS/CSSDescriptors.cpp +++ b/Libraries/LibWeb/CSS/CSSDescriptors.cpp @@ -281,6 +281,14 @@ RefPtr CSSDescriptors::descriptor_or_initial_value(DescriptorI return descriptor_initial_value(m_at_rule_id, descriptor_id); } +bool CSSDescriptors::has_property(StringView property_name) const +{ + auto descriptor_id = descriptor_id_from_string(m_at_rule_id, property_name); + if (!descriptor_id.has_value()) + return false; + return descriptor(*descriptor_id) != nullptr; +} + 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 3749465a9ae..1da05ad8ddb 100644 --- a/Libraries/LibWeb/CSS/CSSDescriptors.h +++ b/Libraries/LibWeb/CSS/CSSDescriptors.h @@ -33,6 +33,8 @@ public: virtual WebIDL::ExceptionOr set_css_text(StringView) override; + virtual bool has_property(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 6bcb8ee322e..8f29294e46e 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -57,6 +57,8 @@ public: [[nodiscard]] bool is_updating() const { return m_updating; } void set_is_updating(bool value) { m_updating = value; } + virtual bool has_property(StringView property_name) const = 0; + protected: enum class Computed : u8 { No, diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp index 16b938fd24f..a03bf863c8d 100644 --- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp @@ -426,6 +426,14 @@ StringView CSSStyleProperties::get_property_priority(StringView property_name) c return maybe_property->important == Important::Yes ? "important"sv : ""sv; } +bool CSSStyleProperties::has_property(StringView property_name) const +{ + auto property_id = property_id_from_string(property_name); + if (!property_id.has_value()) + return false; + return get_property_internal(*property_id).has_value(); +} + // 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 1d6c8050b35..22f9be74f5b 100644 --- a/Libraries/LibWeb/CSS/CSSStyleProperties.h +++ b/Libraries/LibWeb/CSS/CSSStyleProperties.h @@ -49,6 +49,8 @@ public: size_t custom_property_count() const { return m_custom_properties.size(); } + virtual bool has_property(StringView property_name) const override; + String css_float() const; WebIDL::ExceptionOr set_css_float(StringView);