From ce26e5d75751f4630a1892a43e23ed2484a4e6b5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 20 Nov 2024 17:21:22 +0100 Subject: [PATCH] LibWeb: Allow custom properties in CSSStyleDeclaration.getPropertyValue --- Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp | 8 ++++++++ Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 4 +++- Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp | 6 ++++++ Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h | 1 + .../css/CSSStyleDeclaration-custom-properties.txt | 1 + .../input/css/CSSStyleDeclaration-custom-properties.html | 1 + 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index c95524c7d6d..1fe76a2751d 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -258,6 +258,14 @@ String CSSStyleDeclaration::get_property_value(StringView property_name) const if (!property_id.has_value()) return {}; + if (property_id.value() == PropertyID::Custom) { + auto maybe_custom_property = custom_property(FlyString::from_utf8_without_validation(property_name.bytes())); + if (maybe_custom_property.has_value()) { + return maybe_custom_property.value().value->to_string(); + } + return {}; + } + // 2. If property is a shorthand property, then follow these substeps: if (property_is_shorthand(property_id.value())) { // 1. Let list be a new empty array. diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index be01254ad40..f94660dcdf5 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -29,6 +29,7 @@ public: virtual String item(size_t index) const = 0; virtual Optional property(PropertyID) const = 0; + virtual Optional custom_property(FlyString const& custom_property_name) const = 0; virtual WebIDL::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority = ""sv); virtual WebIDL::ExceptionOr remove_property(PropertyID) = 0; @@ -75,13 +76,14 @@ public: virtual String item(size_t index) const override; virtual Optional property(PropertyID) const override; + virtual Optional custom_property(FlyString const& custom_property_name) const override { return m_custom_properties.get(custom_property_name); } virtual WebIDL::ExceptionOr set_property(StringView property_name, StringView css_text, StringView priority) override; virtual WebIDL::ExceptionOr remove_property(PropertyID) override; Vector const& properties() const { return m_properties; } HashMap const& custom_properties() const { return m_custom_properties; } - Optional custom_property(FlyString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); } + size_t custom_property_count() const { return m_custom_properties.size(); } virtual String serialized() const final override; diff --git a/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index ac95dc85adf..3f42a2e5360 100644 --- a/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -601,6 +601,12 @@ Optional ResolvedCSSStyleDeclaration::property(PropertyID propert }; } +Optional ResolvedCSSStyleDeclaration::custom_property(FlyString const&) const +{ + dbgln("FIXME: ResolvedCSSStyleDeclaration::custom_property is not implemented"); + return {}; +} + static WebIDL::ExceptionOr cannot_modify_computed_property_error(JS::Realm& realm) { return WebIDL::NoModificationAllowedError::create(realm, "Cannot modify properties in result of getComputedStyle()"_string); diff --git a/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h b/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h index 9a663016ed7..acc3c3b00bc 100644 --- a/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h +++ b/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h @@ -24,6 +24,7 @@ public: virtual String item(size_t index) const override; virtual Optional property(PropertyID) const override; + virtual Optional custom_property(FlyString const& custom_property_name) const override; virtual WebIDL::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority) override; virtual WebIDL::ExceptionOr set_property(StringView property_name, StringView css_text, StringView priority) override; virtual WebIDL::ExceptionOr remove_property(PropertyID) override; diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt b/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt index 1e04124a3a2..c8d606fffe0 100644 --- a/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt +++ b/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt @@ -1 +1,2 @@ +style.getPropertyValue(--redcolor)=red rgb(255, 0, 0) diff --git a/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-custom-properties.html b/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-custom-properties.html index 51ba809f88d..2c4942ddbd6 100644 --- a/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-custom-properties.html +++ b/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-custom-properties.html @@ -5,6 +5,7 @@ test(() => { const div = document.createElement('div'); div.style.setProperty("--redcolor", "red"); + println(`style.getPropertyValue(--redcolor)=${div.style.getPropertyValue("--redcolor")}`); const nested = document.createElement('div'); nested.style["backgroundColor"] = "var(--redcolor)";