From ac5699c8fc36c90b33b9fe0b3080500b3cc15434 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 20 Nov 2024 17:48:35 +0100 Subject: [PATCH] LibWeb: Allow custom properties in CSSStyleDeclaration.removeProperty() --- Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp | 25 +++++++++++-------- Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 7 +++--- .../CSSStyleDeclaration-custom-properties.txt | 1 + ...CSSStyleDeclaration-custom-properties.html | 3 +++ 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index 1fe76a2751d..82dbd566eae 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -119,7 +119,7 @@ WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::set_property(String // 3. If value is the empty string, invoke removeProperty() with property as argument and return. if (value.is_empty()) { - MUST(remove_property(property_id)); + MUST(remove_property(property_name)); return {}; } @@ -177,8 +177,12 @@ WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::set_property(String } // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty -WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::remove_property(PropertyID property_id) +WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::remove_property(StringView property_name) { + auto property_id = property_id_from_string(property_name); + if (!property_id.has_value()) + return String {}; + // 1. If the computed flag is set, then throw a NoModificationAllowedError exception. // NOTE: This is handled by the virtual override in ResolvedCSSStyleDeclaration. @@ -186,8 +190,7 @@ WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::remove_property(P // NOTE: We've already converted it to a PropertyID enum value. // 3. Let value be the return value of invoking getPropertyValue() with property as argument. - // FIXME: The trip through string_from_property_id() here is silly. - auto value = get_property_value(string_from_property_id(property_id)); + auto value = get_property_value(property_name); // 4. Let removed be false. bool removed = false; @@ -197,7 +200,12 @@ WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::remove_property(P // 2. Remove that CSS declaration and let removed be true. // 6. Otherwise, if property is a case-sensitive match for a property name of a CSS declaration in the declarations, remove that CSS declaration and let removed be true. - removed = m_properties.remove_first_matching([&](auto& entry) { return entry.property_id == property_id; }); + if (property_id == PropertyID::Custom) { + auto custom_name = FlyString::from_utf8_without_validation(property_name.bytes()); + removed = m_custom_properties.remove(custom_name); + } else { + removed = m_properties.remove_first_matching([&](auto& entry) { return entry.property_id == property_id; }); + } // 7. If removed is true, Update style attribute for the CSS declaration block. if (removed) @@ -321,12 +329,9 @@ WebIDL::ExceptionOr CSSStyleDeclaration::set_property(PropertyID property_ return set_property(string_from_property_id(property_id), css_text, priority); } -WebIDL::ExceptionOr CSSStyleDeclaration::remove_property(StringView property_name) +WebIDL::ExceptionOr CSSStyleDeclaration::remove_property(PropertyID property_name) { - auto property_id = property_id_from_string(property_name); - if (!property_id.has_value()) - return String {}; - return remove_property(property_id.value()); + return remove_property(string_from_property_id(property_name)); } // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index f94660dcdf5..32a898d50dc 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -32,10 +32,10 @@ public: 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; + virtual WebIDL::ExceptionOr remove_property(PropertyID); virtual WebIDL::ExceptionOr set_property(StringView property_name, StringView css_text, StringView priority) = 0; - virtual WebIDL::ExceptionOr remove_property(StringView property_name); + virtual WebIDL::ExceptionOr remove_property(StringView property_name) = 0; String get_property_value(StringView property) const; StringView get_property_priority(StringView property) const; @@ -79,8 +79,7 @@ public: 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; - + virtual WebIDL::ExceptionOr remove_property(StringView property_name) override; Vector const& properties() const { return m_properties; } HashMap const& custom_properties() const { return m_custom_properties; } diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt b/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt index c8d606fffe0..dde24163aa6 100644 --- a/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt +++ b/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-custom-properties.txt @@ -1,2 +1,3 @@ style.getPropertyValue(--redcolor)=red rgb(255, 0, 0) +rgba(0, 0, 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 2c4942ddbd6..929250da074 100644 --- a/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-custom-properties.html +++ b/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-custom-properties.html @@ -16,5 +16,8 @@ document.body.appendChild(div); println(getComputedStyle(nested).backgroundColor); + + div.style.removeProperty("--redcolor"); + println(getComputedStyle(nested).backgroundColor); }); \ No newline at end of file