2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-20 23:25:25 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-11-21 10:39:54 +13:00
|
|
|
#include <AK/String.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Vector.h>
|
2022-08-07 16:21:26 +02:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2024-08-14 11:10:54 +01:00
|
|
|
#include <LibWeb/CSS/CSSStyleValue.h>
|
2024-11-14 16:57:14 +00:00
|
|
|
#include <LibWeb/CSS/GeneratedCSSStyleProperties.h>
|
2023-05-08 06:57:55 +02:00
|
|
|
#include <LibWeb/CSS/StyleProperty.h>
|
2019-06-20 23:25:25 +02:00
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
namespace Web::CSS {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2024-11-14 16:57:14 +00:00
|
|
|
class CSSStyleDeclaration
|
|
|
|
: public Bindings::PlatformObject
|
|
|
|
, public Bindings::GeneratedCSSStyleProperties {
|
2022-08-28 13:42:07 +02:00
|
|
|
WEB_PLATFORM_OBJECT(CSSStyleDeclaration, Bindings::PlatformObject);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(CSSStyleDeclaration);
|
2021-03-13 22:39:55 +01:00
|
|
|
|
2022-08-07 16:21:26 +02:00
|
|
|
public:
|
2022-03-14 13:21:51 -06:00
|
|
|
virtual ~CSSStyleDeclaration() = default;
|
2023-08-07 08:41:28 +02:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2021-09-12 19:24:01 +02:00
|
|
|
|
|
|
|
virtual size_t length() const = 0;
|
2023-09-06 19:07:24 +12:00
|
|
|
virtual String item(size_t index) const = 0;
|
2021-09-12 19:24:01 +02:00
|
|
|
|
|
|
|
virtual Optional<StyleProperty> property(PropertyID) const = 0;
|
2024-11-25 13:23:31 +01:00
|
|
|
virtual Optional<StyleProperty const&> custom_property(FlyString const& custom_property_name) const = 0;
|
2021-09-12 19:24:01 +02:00
|
|
|
|
2024-11-20 15:48:04 +01:00
|
|
|
virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority = ""sv);
|
2024-11-20 17:48:35 +01:00
|
|
|
virtual WebIDL::ExceptionOr<String> remove_property(PropertyID);
|
2022-04-11 16:10:55 +02:00
|
|
|
|
2024-11-20 15:48:04 +01:00
|
|
|
virtual WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority) = 0;
|
2024-11-20 17:48:35 +01:00
|
|
|
virtual WebIDL::ExceptionOr<String> remove_property(StringView property_name) = 0;
|
2021-09-26 19:06:17 +02:00
|
|
|
|
2023-09-06 19:07:24 +12:00
|
|
|
String get_property_value(StringView property) const;
|
|
|
|
StringView get_property_priority(StringView property) const;
|
2021-09-12 20:44:17 +02:00
|
|
|
|
2023-11-21 10:39:54 +13:00
|
|
|
String css_text() const;
|
2022-11-05 04:51:05 +00:00
|
|
|
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) = 0;
|
2021-10-01 19:57:45 +02:00
|
|
|
|
2024-11-14 16:57:14 +00:00
|
|
|
String css_float() const;
|
|
|
|
WebIDL::ExceptionOr<void> set_css_float(StringView);
|
2021-10-01 19:57:45 +02:00
|
|
|
|
2024-11-14 16:57:14 +00:00
|
|
|
virtual String serialized() const = 0;
|
2022-08-07 16:21:26 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual GC::Ptr<CSSRule> parent_rule() const;
|
2024-06-14 16:38:23 +02:00
|
|
|
|
2024-12-07 01:10:11 +01:00
|
|
|
// https://drafts.csswg.org/cssom/#cssstyledeclaration-computed-flag
|
|
|
|
[[nodiscard]] virtual bool computed_flag() const { return false; }
|
|
|
|
|
2021-09-12 19:24:01 +02:00
|
|
|
protected:
|
2022-09-24 16:34:04 -06:00
|
|
|
explicit CSSStyleDeclaration(JS::Realm&);
|
2024-11-14 16:57:14 +00:00
|
|
|
|
|
|
|
virtual CSSStyleDeclaration& generated_style_properties_to_css_style_declaration() override { return *this; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// ^PlatformObject
|
|
|
|
virtual Optional<JS::Value> item_value(size_t index) const override;
|
2025-02-06 11:10:36 +00:00
|
|
|
Optional<StyleProperty> get_property_internal(PropertyID) const;
|
2021-09-12 19:24:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration {
|
2022-08-28 13:42:07 +02:00
|
|
|
WEB_PLATFORM_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(PropertyOwningCSSStyleDeclaration);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2021-09-12 19:24:01 +02:00
|
|
|
friend class ElementInlineCSSStyleDeclaration;
|
|
|
|
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] static GC::Ref<PropertyOwningCSSStyleDeclaration>
|
2023-11-05 16:19:16 +13:00
|
|
|
create(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties);
|
2019-06-21 19:19:49 +02:00
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
virtual ~PropertyOwningCSSStyleDeclaration() override = default;
|
2019-06-20 23:25:25 +02:00
|
|
|
|
2021-09-12 19:24:01 +02:00
|
|
|
virtual size_t length() const override;
|
2023-09-06 19:07:24 +12:00
|
|
|
virtual String item(size_t index) const override;
|
2019-06-20 23:25:25 +02:00
|
|
|
|
2021-09-12 19:24:01 +02:00
|
|
|
virtual Optional<StyleProperty> property(PropertyID) const override;
|
2024-11-25 13:23:31 +01:00
|
|
|
virtual Optional<StyleProperty const&> custom_property(FlyString const& custom_property_name) const override { return m_custom_properties.get(custom_property_name); }
|
2022-04-11 16:10:55 +02:00
|
|
|
|
2024-11-20 15:48:04 +01:00
|
|
|
virtual WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority) override;
|
2024-11-20 17:48:35 +01:00
|
|
|
virtual WebIDL::ExceptionOr<String> remove_property(StringView property_name) override;
|
2022-04-01 20:58:27 +03:00
|
|
|
Vector<StyleProperty> const& properties() const { return m_properties; }
|
2023-11-05 16:19:16 +13:00
|
|
|
HashMap<FlyString, StyleProperty> const& custom_properties() const { return m_custom_properties; }
|
2024-11-20 17:21:22 +01:00
|
|
|
|
2021-09-12 19:24:01 +02:00
|
|
|
size_t custom_property_count() const { return m_custom_properties.size(); }
|
2021-03-13 22:39:55 +01:00
|
|
|
|
2023-11-21 10:39:54 +13:00
|
|
|
virtual String serialized() const final override;
|
2022-11-05 04:51:05 +00:00
|
|
|
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
|
2021-10-01 19:57:45 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual GC::Ptr<CSSRule> parent_rule() const override;
|
|
|
|
void set_parent_rule(GC::Ref<CSSRule>);
|
2024-06-14 16:38:23 +02:00
|
|
|
|
2021-03-16 18:55:53 +01:00
|
|
|
protected:
|
2023-11-05 16:19:16 +13:00
|
|
|
PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty>);
|
2022-08-28 13:42:07 +02:00
|
|
|
|
2022-04-11 16:10:55 +02:00
|
|
|
virtual void update_style_attribute() { }
|
|
|
|
|
2022-11-05 04:51:05 +00:00
|
|
|
void empty_the_declarations();
|
2023-11-05 16:19:16 +13:00
|
|
|
void set_the_declarations(Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties);
|
2022-11-05 04:51:05 +00:00
|
|
|
|
2020-05-19 17:28:16 +01:00
|
|
|
private:
|
2024-08-14 11:10:54 +01:00
|
|
|
bool set_a_css_declaration(PropertyID, NonnullRefPtr<CSSStyleValue const>, Important);
|
2022-04-11 16:10:55 +02:00
|
|
|
|
2023-09-24 18:13:39 +02:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<CSSRule> m_parent_rule;
|
2019-09-30 20:06:17 +02:00
|
|
|
Vector<StyleProperty> m_properties;
|
2023-11-05 16:19:16 +13:00
|
|
|
HashMap<FlyString, StyleProperty> m_custom_properties;
|
2019-06-20 23:25:25 +02:00
|
|
|
};
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2021-09-12 19:24:01 +02:00
|
|
|
class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration {
|
2022-08-28 13:42:07 +02:00
|
|
|
WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(ElementInlineCSSStyleDeclaration);
|
2022-08-07 16:21:26 +02:00
|
|
|
|
2021-03-16 18:55:53 +01:00
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] static GC::Ref<ElementInlineCSSStyleDeclaration> create(DOM::Element&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties);
|
2022-08-07 16:21:26 +02:00
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
virtual ~ElementInlineCSSStyleDeclaration() override = default;
|
2021-03-16 18:55:53 +01:00
|
|
|
|
|
|
|
DOM::Element* element() { return m_element.ptr(); }
|
|
|
|
const DOM::Element* element() const { return m_element.ptr(); }
|
|
|
|
|
2022-04-11 16:56:52 +02:00
|
|
|
bool is_updating() const { return m_updating; }
|
|
|
|
|
2024-11-25 14:35:56 +01:00
|
|
|
void set_declarations_from_text(StringView);
|
|
|
|
|
2022-11-05 04:51:05 +00:00
|
|
|
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
|
|
|
|
|
2021-03-16 18:55:53 +01:00
|
|
|
private:
|
2023-11-05 16:19:16 +13:00
|
|
|
ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties);
|
2022-08-28 13:42:07 +02:00
|
|
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2022-04-11 16:10:55 +02:00
|
|
|
virtual void update_style_attribute() override;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<DOM::Element> m_element;
|
2022-04-11 16:56:52 +02:00
|
|
|
|
|
|
|
// https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag
|
|
|
|
bool m_updating { false };
|
2021-03-16 18:55:53 +01:00
|
|
|
};
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|