/* * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include namespace Web::CSS { enum class Important { No, Yes, }; struct StyleProperty { Important important { Important::No }; CSS::PropertyID property_id; NonnullRefPtr value; DeprecatedString custom_name {}; }; class CSSStyleDeclaration : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(CSSStyleDeclaration, Bindings::PlatformObject); public: virtual ~CSSStyleDeclaration() = default; virtual size_t length() const = 0; virtual DeprecatedString item(size_t index) const = 0; virtual Optional property(PropertyID) const = 0; 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); DeprecatedString get_property_value(StringView property) const; DeprecatedString get_property_priority(StringView property) const; DeprecatedString css_text() const; virtual WebIDL::ExceptionOr set_css_text(StringView) = 0; virtual DeprecatedString serialized() const = 0; virtual JS::ThrowCompletionOr internal_has_property(JS::PropertyKey const& name) const override; virtual JS::ThrowCompletionOr internal_get(JS::PropertyKey const&, JS::Value receiver) const override; virtual JS::ThrowCompletionOr internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override; protected: explicit CSSStyleDeclaration(JS::Realm&); }; class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration { WEB_PLATFORM_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration); friend class ElementInlineCSSStyleDeclaration; public: static PropertyOwningCSSStyleDeclaration* create(JS::Realm&, Vector, HashMap custom_properties); virtual ~PropertyOwningCSSStyleDeclaration() override = default; virtual size_t length() const override; virtual DeprecatedString item(size_t index) const override; virtual Optional property(PropertyID) const override; virtual WebIDL::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority) override; virtual WebIDL::ExceptionOr remove_property(PropertyID) override; Vector const& properties() const { return m_properties; } HashMap const& custom_properties() const { return m_custom_properties; } Optional custom_property(DeprecatedString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); } size_t custom_property_count() const { return m_custom_properties.size(); } virtual DeprecatedString serialized() const final override; virtual WebIDL::ExceptionOr set_css_text(StringView) override; protected: PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector, HashMap); virtual void update_style_attribute() { } void empty_the_declarations(); void set_the_declarations(Vector properties, HashMap custom_properties); private: bool set_a_css_declaration(PropertyID, NonnullRefPtr, Important); Vector m_properties; HashMap m_custom_properties; }; class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration); public: static ElementInlineCSSStyleDeclaration* create(DOM::Element&, Vector properties, HashMap custom_properties); virtual ~ElementInlineCSSStyleDeclaration() override = default; DOM::Element* element() { return m_element.ptr(); } const DOM::Element* element() const { return m_element.ptr(); } bool is_updating() const { return m_updating; } virtual WebIDL::ExceptionOr set_css_text(StringView) override; private: ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); virtual void visit_edges(Cell::Visitor&) override; virtual void update_style_attribute() override; JS::GCPtr m_element; // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag bool m_updating { false }; }; }