1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:18:12 +00:00

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)).
This commit is contained in:
Andreas Kling 2021-09-26 19:06:17 +02:00
parent e0e41116a4
commit 0ab31d8c84
4 changed files with 19 additions and 3 deletions

View file

@ -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);
}
}