Pārlūkot izejas kodu

LibWeb: Implement CSSStyleDeclaration.cssText

Luke Wilde 2 gadi atpakaļ
vecāks
revīzija
c247fefee7

+ 48 - 6
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp

@@ -232,15 +232,14 @@ WebIDL::ExceptionOr<String> CSSStyleDeclaration::remove_property(StringView prop
     return remove_property(property_id);
 }
 
+// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
 String CSSStyleDeclaration::css_text() const
 {
-    TODO();
-    return "";
-}
+    // 1. If the computed flag is set, then return the empty string.
+    // NOTE: See ResolvedCSSStyleDeclaration::serialized()
 
-void CSSStyleDeclaration::set_css_text(StringView)
-{
-    TODO();
+    // 2. Return the result of serializing the declarations.
+    return serialized();
 }
 
 // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
@@ -360,4 +359,47 @@ JS::ThrowCompletionOr<bool> CSSStyleDeclaration::internal_set(JS::PropertyKey co
     return true;
 }
 
+WebIDL::ExceptionOr<void> PropertyOwningCSSStyleDeclaration::set_css_text(StringView css_text)
+{
+    dbgln("(STUBBED) PropertyOwningCSSStyleDeclaration::set_css_text(css_text='{}')", css_text);
+    return {};
+}
+
+void PropertyOwningCSSStyleDeclaration::empty_the_declarations()
+{
+    m_properties.clear();
+    m_custom_properties.clear();
+}
+
+void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties)
+{
+    m_properties = move(properties);
+    m_custom_properties = move(custom_properties);
+}
+
+// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
+WebIDL::ExceptionOr<void> ElementInlineCSSStyleDeclaration::set_css_text(StringView css_text)
+{
+    // FIXME: What do we do if the element is null?
+    if (!m_element) {
+        dbgln("FIXME: Returning from ElementInlineCSSStyleDeclaration::set_css_text as m_element is null.");
+        return {};
+    }
+
+    // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
+    // NOTE: See ResolvedCSSStyleDeclaration.
+
+    // 2. Empty the declarations.
+    empty_the_declarations();
+
+    // 3. Parse the given value and, if the return value is not the empty list, insert the items in the list into the declarations, in specified order.
+    auto style = parse_css_style_attribute(CSS::Parser::ParsingContext(m_element->document()), css_text, *m_element.ptr());
+    set_the_declarations(style->properties(), style->custom_properties());
+
+    // 4. Update style attribute for the CSS declaration block.
+    update_style_attribute();
+
+    return {};
+}
+
 }

+ 7 - 1
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h

@@ -46,7 +46,7 @@ public:
     String get_property_priority(StringView property) const;
 
     String css_text() const;
-    void set_css_text(StringView);
+    virtual WebIDL::ExceptionOr<void> set_css_text(StringView) = 0;
 
     virtual String serialized() const = 0;
 
@@ -81,12 +81,16 @@ public:
     size_t custom_property_count() const { return m_custom_properties.size(); }
 
     virtual String serialized() const final override;
+    virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
 
 protected:
     PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<String, StyleProperty>);
 
     virtual void update_style_attribute() { }
 
+    void empty_the_declarations();
+    void set_the_declarations(Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties);
+
 private:
     bool set_a_css_declaration(PropertyID, NonnullRefPtr<StyleValue>, Important);
 
@@ -107,6 +111,8 @@ public:
 
     bool is_updating() const { return m_updating; }
 
+    virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
+
 private:
     ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties);
 

+ 2 - 0
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl

@@ -2,6 +2,8 @@
 [Exposed=Window]
 interface CSSStyleDeclaration {
 
+    [CEReactions] attribute CSSOMString cssText;
+
     readonly attribute unsigned long length;
     CSSOMString item(unsigned long index);
 

+ 7 - 0
Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp

@@ -570,4 +570,11 @@ String ResolvedCSSStyleDeclaration::serialized() const
     return String::empty();
 }
 
+// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
+WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_css_text(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()");
+}
+
 }

+ 1 - 0
Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h

@@ -25,6 +25,7 @@ public:
     virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) override;
 
     virtual String serialized() const override;
+    virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
 
 private:
     explicit ResolvedCSSStyleDeclaration(DOM::Element&);