CSSStyleDeclaration.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/Vector.h>
  9. #include <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/CSS/StyleProperty.h>
  11. #include <LibWeb/CSS/StyleValue.h>
  12. namespace Web::CSS {
  13. class CSSStyleDeclaration : public Bindings::PlatformObject {
  14. WEB_PLATFORM_OBJECT(CSSStyleDeclaration, Bindings::PlatformObject);
  15. public:
  16. virtual ~CSSStyleDeclaration() = default;
  17. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  18. virtual size_t length() const = 0;
  19. virtual DeprecatedString item(size_t index) const = 0;
  20. virtual Optional<StyleProperty> property(PropertyID) const = 0;
  21. virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority = ""sv) = 0;
  22. virtual WebIDL::ExceptionOr<DeprecatedString> remove_property(PropertyID) = 0;
  23. WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority);
  24. WebIDL::ExceptionOr<DeprecatedString> remove_property(StringView property_name);
  25. DeprecatedString get_property_value(StringView property) const;
  26. DeprecatedString get_property_priority(StringView property) const;
  27. DeprecatedString css_text() const;
  28. virtual WebIDL::ExceptionOr<void> set_css_text(StringView) = 0;
  29. virtual DeprecatedString serialized() const = 0;
  30. virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyKey const& name) const override;
  31. virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
  32. virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
  33. protected:
  34. explicit CSSStyleDeclaration(JS::Realm&);
  35. };
  36. class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration {
  37. WEB_PLATFORM_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration);
  38. friend class ElementInlineCSSStyleDeclaration;
  39. public:
  40. static WebIDL::ExceptionOr<JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration>> create(JS::Realm&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty> custom_properties);
  41. virtual ~PropertyOwningCSSStyleDeclaration() override = default;
  42. virtual size_t length() const override;
  43. virtual DeprecatedString item(size_t index) const override;
  44. virtual Optional<StyleProperty> property(PropertyID) const override;
  45. virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority) override;
  46. virtual WebIDL::ExceptionOr<DeprecatedString> remove_property(PropertyID) override;
  47. Vector<StyleProperty> const& properties() const { return m_properties; }
  48. HashMap<DeprecatedString, StyleProperty> const& custom_properties() const { return m_custom_properties; }
  49. Optional<StyleProperty> custom_property(DeprecatedString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); }
  50. size_t custom_property_count() const { return m_custom_properties.size(); }
  51. virtual DeprecatedString serialized() const final override;
  52. virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
  53. protected:
  54. PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty>);
  55. virtual void update_style_attribute() { }
  56. void empty_the_declarations();
  57. void set_the_declarations(Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties);
  58. private:
  59. bool set_a_css_declaration(PropertyID, NonnullRefPtr<StyleValue const>, Important);
  60. Vector<StyleProperty> m_properties;
  61. HashMap<DeprecatedString, StyleProperty> m_custom_properties;
  62. };
  63. class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration {
  64. WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration);
  65. public:
  66. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration>> create(DOM::Element&, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties);
  67. virtual ~ElementInlineCSSStyleDeclaration() override = default;
  68. DOM::Element* element() { return m_element.ptr(); }
  69. const DOM::Element* element() const { return m_element.ptr(); }
  70. bool is_updating() const { return m_updating; }
  71. virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
  72. private:
  73. ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties);
  74. virtual void visit_edges(Cell::Visitor&) override;
  75. virtual void update_style_attribute() override;
  76. JS::GCPtr<DOM::Element> m_element;
  77. // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag
  78. bool m_updating { false };
  79. };
  80. }