/* * Copyright (c) 2018-2021, 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; String custom_name {}; }; class CSSStyleDeclaration : public RefCounted , public Bindings::Wrappable { public: using WrapperType = Bindings::CSSStyleDeclarationWrapper; virtual ~CSSStyleDeclaration() = default; virtual size_t length() const = 0; virtual String item(size_t index) const = 0; virtual Optional property(PropertyID) const = 0; virtual DOM::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority = "") = 0; virtual DOM::ExceptionOr remove_property(PropertyID) = 0; DOM::ExceptionOr set_property(StringView property_name, StringView css_text, StringView priority); DOM::ExceptionOr remove_property(StringView property_name); String get_property_value(StringView property) const; String get_property_priority(StringView property) const; String css_text() const; void set_css_text(StringView); virtual String serialized() const = 0; protected: CSSStyleDeclaration() = default; }; class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration { friend class ElementInlineCSSStyleDeclaration; public: static NonnullRefPtr create(Vector properties, HashMap custom_properties) { return adopt_ref(*new PropertyOwningCSSStyleDeclaration(move(properties), move(custom_properties))); } virtual ~PropertyOwningCSSStyleDeclaration() override = default; virtual size_t length() const override; virtual String item(size_t index) const override; virtual Optional property(PropertyID) const override; virtual DOM::ExceptionOr set_property(PropertyID, StringView css_text, StringView priority) override; virtual DOM::ExceptionOr remove_property(PropertyID) override; Vector const& properties() const { return m_properties; } HashMap const& custom_properties() const { return m_custom_properties; } Optional custom_property(String 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 String serialized() const final override; protected: explicit PropertyOwningCSSStyleDeclaration(Vector, HashMap); virtual void update_style_attribute() { } private: bool set_a_css_declaration(PropertyID, NonnullRefPtr, Important); Vector m_properties; HashMap m_custom_properties; }; class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { public: static NonnullRefPtr create(DOM::Element& element, Vector properties, HashMap custom_properties) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element, move(properties), move(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; } private: explicit ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); virtual void update_style_attribute() override; WeakPtr m_element; // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag bool m_updating { false }; }; } namespace Web::Bindings { CSSStyleDeclarationWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleDeclaration&); }