diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-26 19:06:17 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-26 19:07:03 +0200 |
commit | 0ab31d8c84c57039a50eb0e8e0a3ed668ab422fe (patch) | |
tree | a734cfcff853f4b2a7fc9c493dc1c60696677ff9 /Userland/Libraries | |
parent | e0e41116a4d3414018b41fa476242f31bee3482d (diff) | |
download | serenity-0ab31d8c84c57039a50eb0e8e0a3ed668ab422fe.zip |
LibWeb: Support simplest form of CSSStyleDeclaration.setProperty()
This patch adds the setProperty(name, value) API to CSSStyleDeclaration.
Setting an invalid or empty value will cause the property to be removed
from the declaration.
Note that this only works on mutable declarations (i.e element.style)
and not on resolved declarations (i.e window.getComputedStyle(element)).
Diffstat (limited to 'Userland/Libraries')
4 files changed, 19 insertions, 3 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp index d5e572fcf3..66bd50f446 100644 --- a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp +++ b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp @@ -44,7 +44,8 @@ bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS:: if (vm().exception()) return false; - return impl().set_property(property_id, css_text); + impl().set_property(property_id, css_text); + return true; } } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index dae3a69a59..0635a4c2e3 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -64,9 +64,12 @@ Optional<StyleProperty> PropertyOwningCSSStyleDeclaration::property(PropertyID p bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, StringView css_text) { auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id); - // FIXME: What are we supposed to do if we can't parse it? - if (!new_value) + if (!new_value) { + m_properties.remove_all_matching([&](auto& entry) { + return entry.property_id == property_id; + }); return false; + } ScopeGuard style_invalidation_guard = [&] { auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(*this); @@ -102,4 +105,12 @@ String CSSStyleDeclaration::get_property_value(StringView property_name) const return maybe_property->value->to_string(); } +void CSSStyleDeclaration::set_property(StringView property_name, StringView css_text) +{ + auto property_id = property_id_from_string(property_name); + if (property_id == CSS::PropertyID::Invalid) + return; + set_property(property_id, css_text); +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 17bfcb26c1..37a32c9f6a 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -34,6 +34,8 @@ public: virtual Optional<StyleProperty> property(PropertyID) const = 0; virtual bool set_property(PropertyID, StringView css_text) = 0; + void set_property(StringView property_name, StringView css_text); + String get_property_value(StringView property) const; protected: diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl index 743f2151ad..bce8af29f3 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl @@ -6,4 +6,6 @@ interface CSSStyleDeclaration { CSSOMString getPropertyValue(CSSOMString property); + [CEReactions] undefined setProperty(CSSOMString property, [LegacyNullToEmptyString] CSSOMString value); + }; |