Skip to content

Commit 0ab31d8

Browse files
committed
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)).
1 parent e0e4111 commit 0ab31d8

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::
4444
if (vm().exception())
4545
return false;
4646

47-
return impl().set_property(property_id, css_text);
47+
impl().set_property(property_id, css_text);
48+
return true;
4849
}
4950

5051
}

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ Optional<StyleProperty> PropertyOwningCSSStyleDeclaration::property(PropertyID p
6464
bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, StringView css_text)
6565
{
6666
auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
67-
// FIXME: What are we supposed to do if we can't parse it?
68-
if (!new_value)
67+
if (!new_value) {
68+
m_properties.remove_all_matching([&](auto& entry) {
69+
return entry.property_id == property_id;
70+
});
6971
return false;
72+
}
7073

7174
ScopeGuard style_invalidation_guard = [&] {
7275
auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(*this);
@@ -102,4 +105,12 @@ String CSSStyleDeclaration::get_property_value(StringView property_name) const
102105
return maybe_property->value->to_string();
103106
}
104107

108+
void CSSStyleDeclaration::set_property(StringView property_name, StringView css_text)
109+
{
110+
auto property_id = property_id_from_string(property_name);
111+
if (property_id == CSS::PropertyID::Invalid)
112+
return;
113+
set_property(property_id, css_text);
114+
}
115+
105116
}

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class CSSStyleDeclaration
3434
virtual Optional<StyleProperty> property(PropertyID) const = 0;
3535
virtual bool set_property(PropertyID, StringView css_text) = 0;
3636

37+
void set_property(StringView property_name, StringView css_text);
38+
3739
String get_property_value(StringView property) const;
3840

3941
protected:

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ interface CSSStyleDeclaration {
66

77
CSSOMString getPropertyValue(CSSOMString property);
88

9+
[CEReactions] undefined setProperty(CSSOMString property, [LegacyNullToEmptyString] CSSOMString value);
10+
911
};

0 commit comments

Comments
 (0)