Serenity Operating System
at master 129 lines 5.1 kB view raw
1/* 2 * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/DeprecatedString.h> 10#include <AK/Vector.h> 11#include <LibWeb/Bindings/PlatformObject.h> 12#include <LibWeb/CSS/StyleValue.h> 13 14namespace Web::CSS { 15 16enum class Important { 17 No, 18 Yes, 19}; 20 21struct StyleProperty { 22 Important important { Important::No }; 23 CSS::PropertyID property_id; 24 NonnullRefPtr<StyleValue const> value; 25 DeprecatedString custom_name {}; 26}; 27 28class CSSStyleDeclaration : public Bindings::PlatformObject { 29 WEB_PLATFORM_OBJECT(CSSStyleDeclaration, Bindings::PlatformObject); 30 31public: 32 virtual ~CSSStyleDeclaration() = default; 33 34 virtual size_t length() const = 0; 35 virtual DeprecatedString item(size_t index) const = 0; 36 37 virtual Optional<StyleProperty> property(PropertyID) const = 0; 38 39 virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority = ""sv) = 0; 40 virtual WebIDL::ExceptionOr<DeprecatedString> remove_property(PropertyID) = 0; 41 42 WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority); 43 WebIDL::ExceptionOr<DeprecatedString> remove_property(StringView property_name); 44 45 DeprecatedString get_property_value(StringView property) const; 46 DeprecatedString get_property_priority(StringView property) const; 47 48 DeprecatedString css_text() const; 49 virtual WebIDL::ExceptionOr<void> set_css_text(StringView) = 0; 50 51 virtual DeprecatedString serialized() const = 0; 52 53 virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyKey const& name) const override; 54 virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override; 55 virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override; 56 57protected: 58 explicit CSSStyleDeclaration(JS::Realm&); 59}; 60 61class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration { 62 WEB_PLATFORM_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration); 63 friend class ElementInlineCSSStyleDeclaration; 64 65public: 66 static WebIDL::ExceptionOr<JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration>> create(JS::Realm&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty> custom_properties); 67 68 virtual ~PropertyOwningCSSStyleDeclaration() override = default; 69 70 virtual size_t length() const override; 71 virtual DeprecatedString item(size_t index) const override; 72 73 virtual Optional<StyleProperty> property(PropertyID) const override; 74 75 virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority) override; 76 virtual WebIDL::ExceptionOr<DeprecatedString> remove_property(PropertyID) override; 77 78 Vector<StyleProperty> const& properties() const { return m_properties; } 79 HashMap<DeprecatedString, StyleProperty> const& custom_properties() const { return m_custom_properties; } 80 Optional<StyleProperty> custom_property(DeprecatedString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); } 81 size_t custom_property_count() const { return m_custom_properties.size(); } 82 83 virtual DeprecatedString serialized() const final override; 84 virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override; 85 86protected: 87 PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty>); 88 89 virtual void update_style_attribute() { } 90 91 void empty_the_declarations(); 92 void set_the_declarations(Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties); 93 94private: 95 bool set_a_css_declaration(PropertyID, NonnullRefPtr<StyleValue const>, Important); 96 97 Vector<StyleProperty> m_properties; 98 HashMap<DeprecatedString, StyleProperty> m_custom_properties; 99}; 100 101class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { 102 WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration); 103 104public: 105 static WebIDL::ExceptionOr<JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration>> create(DOM::Element&, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties); 106 107 virtual ~ElementInlineCSSStyleDeclaration() override = default; 108 109 DOM::Element* element() { return m_element.ptr(); } 110 const DOM::Element* element() const { return m_element.ptr(); } 111 112 bool is_updating() const { return m_updating; } 113 114 virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override; 115 116private: 117 ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties); 118 119 virtual void visit_edges(Cell::Visitor&) override; 120 121 virtual void update_style_attribute() override; 122 123 JS::GCPtr<DOM::Element> m_element; 124 125 // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag 126 bool m_updating { false }; 127}; 128 129}