/src/serenity/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
Line | Count | Source (jump to first uncovered line) |
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/String.h> |
10 | | #include <AK/Vector.h> |
11 | | #include <LibWeb/Bindings/PlatformObject.h> |
12 | | #include <LibWeb/CSS/CSSStyleValue.h> |
13 | | #include <LibWeb/CSS/GeneratedCSSStyleProperties.h> |
14 | | #include <LibWeb/CSS/StyleProperty.h> |
15 | | |
16 | | namespace Web::CSS { |
17 | | |
18 | | class CSSStyleDeclaration |
19 | | : public Bindings::PlatformObject |
20 | | , public Bindings::GeneratedCSSStyleProperties { |
21 | | WEB_PLATFORM_OBJECT(CSSStyleDeclaration, Bindings::PlatformObject); |
22 | | JS_DECLARE_ALLOCATOR(CSSStyleDeclaration); |
23 | | |
24 | | public: |
25 | 0 | virtual ~CSSStyleDeclaration() = default; |
26 | | virtual void initialize(JS::Realm&) override; |
27 | | |
28 | | virtual size_t length() const = 0; |
29 | | virtual String item(size_t index) const = 0; |
30 | | |
31 | | virtual Optional<StyleProperty> property(PropertyID) const = 0; |
32 | | |
33 | | virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority = ""sv) = 0; |
34 | | virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) = 0; |
35 | | |
36 | | virtual WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority); |
37 | | virtual WebIDL::ExceptionOr<String> remove_property(StringView property_name); |
38 | | |
39 | | String get_property_value(StringView property) const; |
40 | | StringView get_property_priority(StringView property) const; |
41 | | |
42 | | String css_text() const; |
43 | | virtual WebIDL::ExceptionOr<void> set_css_text(StringView) = 0; |
44 | | |
45 | | String css_float() const; |
46 | | WebIDL::ExceptionOr<void> set_css_float(StringView); |
47 | | |
48 | | virtual String serialized() const = 0; |
49 | | |
50 | | virtual JS::GCPtr<CSSRule> parent_rule() const; |
51 | | |
52 | | protected: |
53 | | explicit CSSStyleDeclaration(JS::Realm&); |
54 | | |
55 | 0 | virtual CSSStyleDeclaration& generated_style_properties_to_css_style_declaration() override { return *this; } |
56 | | |
57 | | private: |
58 | | // ^PlatformObject |
59 | | virtual Optional<JS::Value> item_value(size_t index) const override; |
60 | | }; |
61 | | |
62 | | class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration { |
63 | | WEB_PLATFORM_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration); |
64 | | JS_DECLARE_ALLOCATOR(PropertyOwningCSSStyleDeclaration); |
65 | | |
66 | | friend class ElementInlineCSSStyleDeclaration; |
67 | | |
68 | | public: |
69 | | [[nodiscard]] static JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> |
70 | | create(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties); |
71 | | |
72 | 0 | virtual ~PropertyOwningCSSStyleDeclaration() override = default; |
73 | | |
74 | | virtual size_t length() const override; |
75 | | virtual String item(size_t index) const override; |
76 | | |
77 | | virtual Optional<StyleProperty> property(PropertyID) const override; |
78 | | |
79 | | virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority) override; |
80 | | virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) override; |
81 | | |
82 | 0 | Vector<StyleProperty> const& properties() const { return m_properties; } |
83 | 0 | HashMap<FlyString, StyleProperty> const& custom_properties() const { return m_custom_properties; } |
84 | 0 | Optional<StyleProperty> custom_property(FlyString const& custom_property_name) const { return m_custom_properties.get(custom_property_name).copy(); } |
85 | 0 | size_t custom_property_count() const { return m_custom_properties.size(); } |
86 | | |
87 | | virtual String serialized() const final override; |
88 | | virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override; |
89 | | |
90 | | virtual JS::GCPtr<CSSRule> parent_rule() const override; |
91 | | void set_parent_rule(JS::NonnullGCPtr<CSSRule>); |
92 | | |
93 | | protected: |
94 | | PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty>); |
95 | | |
96 | 0 | virtual void update_style_attribute() { } |
97 | | |
98 | | void empty_the_declarations(); |
99 | | void set_the_declarations(Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties); |
100 | | |
101 | | private: |
102 | | bool set_a_css_declaration(PropertyID, NonnullRefPtr<CSSStyleValue const>, Important); |
103 | | |
104 | | virtual void visit_edges(Cell::Visitor&) override; |
105 | | |
106 | | JS::GCPtr<CSSRule> m_parent_rule; |
107 | | Vector<StyleProperty> m_properties; |
108 | | HashMap<FlyString, StyleProperty> m_custom_properties; |
109 | | }; |
110 | | |
111 | | class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { |
112 | | WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration); |
113 | | JS_DECLARE_ALLOCATOR(ElementInlineCSSStyleDeclaration); |
114 | | |
115 | | public: |
116 | | [[nodiscard]] static JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties); |
117 | | |
118 | | virtual ~ElementInlineCSSStyleDeclaration() override = default; |
119 | | |
120 | 0 | DOM::Element* element() { return m_element.ptr(); } |
121 | 0 | const DOM::Element* element() const { return m_element.ptr(); } |
122 | | |
123 | 0 | bool is_updating() const { return m_updating; } |
124 | | |
125 | | virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override; |
126 | | |
127 | | private: |
128 | | ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties); |
129 | | |
130 | | virtual void visit_edges(Cell::Visitor&) override; |
131 | | |
132 | | virtual void update_style_attribute() override; |
133 | | |
134 | | JS::GCPtr<DOM::Element> m_element; |
135 | | |
136 | | // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag |
137 | | bool m_updating { false }; |
138 | | }; |
139 | | |
140 | | } |