Skip to content

Commit

Permalink
LibWeb: Return error on modification of a computed CSS style declaration
Browse files Browse the repository at this point in the history
Previously, calling `setProperty` or `removeProperty` from JS on a
CSSStyleDeclaration returned from `getComputedStyle()` would return
null. We now return a NoModificationAllowedError instead, which aligns
our implementation with the specification.
  • Loading branch information
tcl3 committed Aug 4, 2024
1 parent a034d1e commit ad3a199
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class CSSStyleDeclaration : public Bindings::PlatformObject {
virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority = ""sv) = 0;
virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) = 0;

WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority);
WebIDL::ExceptionOr<String> remove_property(StringView property_name);
virtual WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority);
virtual WebIDL::ExceptionOr<String> remove_property(StringView property_name);

String get_property_value(StringView property) const;
StringView get_property_priority(StringView property) const;
Expand Down
28 changes: 26 additions & 2 deletions Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,18 +569,42 @@ Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID propert
};
}

static WebIDL::ExceptionOr<void> cannot_modify_computed_properties_error(JS::Realm& realm)
{
return WebIDL::NoModificationAllowedError::create(realm, "Cannot modify properties in result of getComputedStyle()"_fly_string);
}

// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
{
// 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()"_fly_string);
return cannot_modify_computed_properties_error(realm());
}

// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(StringView, StringView, StringView)
{
// 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
return cannot_modify_computed_properties_error(realm());
}

static WebIDL::ExceptionOr<String> cannot_remove_computed_properties_error(JS::Realm& realm)
{
return WebIDL::NoModificationAllowedError::create(realm, "Cannot remove properties from result of getComputedStyle()"_fly_string);
}

// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
WebIDL::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
{
// 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
return WebIDL::NoModificationAllowedError::create(realm(), "Cannot remove properties from result of getComputedStyle()"_fly_string);
return cannot_remove_computed_properties_error(realm());
}

// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
WebIDL::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(StringView)
{
// 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
return cannot_remove_computed_properties_error(realm());
}

String ResolvedCSSStyleDeclaration::serialized() const
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class ResolvedCSSStyleDeclaration final : public CSSStyleDeclaration {

virtual Optional<StyleProperty> property(PropertyID) const override;
virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority) override;
virtual WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority) override;
virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) override;
virtual WebIDL::ExceptionOr<String> remove_property(StringView property_name) override;

virtual String serialized() const override;
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
Expand Down

0 comments on commit ad3a199

Please sign in to comment.