From ea68bdef26260762dec02413cce0d79caef6f4a4 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 4 Aug 2024 22:12:31 +0100 Subject: [PATCH] LibWeb: Return error on modification of a computed CSS style declaration 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. --- .../CSSStyleDeclaration-modify-computed.txt | 2 ++ .../CSSStyleDeclaration-modify-computed.html | 19 +++++++++++++ .../LibWeb/CSS/CSSStyleDeclaration.h | 4 +-- .../CSS/ResolvedCSSStyleDeclaration.cpp | 28 +++++++++++++++++-- .../LibWeb/CSS/ResolvedCSSStyleDeclaration.h | 2 ++ 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-modify-computed.txt create mode 100644 Tests/LibWeb/Text/input/css/CSSStyleDeclaration-modify-computed.html diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-modify-computed.txt b/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-modify-computed.txt new file mode 100644 index 00000000000..e64a185d0f3 --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/CSSStyleDeclaration-modify-computed.txt @@ -0,0 +1,2 @@ +Calling setProperty() on a computed CSSStyleDeclaration throws error of type: NoModificationAllowedError +Calling removeProperty() on a computed CSSStyleDeclaration throws error of type: NoModificationAllowedError diff --git a/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-modify-computed.html b/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-modify-computed.html new file mode 100644 index 00000000000..e0852f777d5 --- /dev/null +++ b/Tests/LibWeb/Text/input/css/CSSStyleDeclaration-modify-computed.html @@ -0,0 +1,19 @@ + + + diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 79a81877889..62be62dada8 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -30,8 +30,8 @@ public: virtual WebIDL::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority = ""sv) = 0; virtual WebIDL::ExceptionOr remove_property(PropertyID) = 0; - WebIDL::ExceptionOr set_property(StringView property_name, StringView css_text, StringView priority); - WebIDL::ExceptionOr remove_property(StringView property_name); + virtual WebIDL::ExceptionOr set_property(StringView property_name, StringView css_text, StringView priority); + virtual WebIDL::ExceptionOr remove_property(StringView property_name); String get_property_value(StringView property) const; StringView get_property_priority(StringView property) const; diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index 80e6c04a3f9..23a88763314 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -569,18 +569,42 @@ Optional ResolvedCSSStyleDeclaration::property(PropertyID propert }; } +static WebIDL::ExceptionOr cannot_modify_computed_property_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 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_property_error(realm()); +} + +// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty +WebIDL::ExceptionOr ResolvedCSSStyleDeclaration::set_property(StringView, StringView, StringView) +{ + // 1. If the computed flag is set, then throw a NoModificationAllowedError exception. + return cannot_modify_computed_property_error(realm()); +} + +static WebIDL::ExceptionOr cannot_remove_computed_property_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 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_property_error(realm()); +} + +// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty +WebIDL::ExceptionOr ResolvedCSSStyleDeclaration::remove_property(StringView) +{ + // 1. If the computed flag is set, then throw a NoModificationAllowedError exception. + return cannot_remove_computed_property_error(realm()); } String ResolvedCSSStyleDeclaration::serialized() const diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h index 422f82d1a3e..7dfd76bd989 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h @@ -24,7 +24,9 @@ public: virtual Optional property(PropertyID) const override; virtual WebIDL::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority) override; + 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; virtual String serialized() const override; virtual WebIDL::ExceptionOr set_css_text(StringView) override;