CSSStyleDeclaration.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/String.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. JS_DECLARE_ALLOCATOR(CSSStyleDeclaration);
  16. public:
  17. virtual ~CSSStyleDeclaration() = default;
  18. virtual void initialize(JS::Realm&) override;
  19. virtual size_t length() const = 0;
  20. virtual String item(size_t index) const = 0;
  21. virtual Optional<StyleProperty> property(PropertyID) const = 0;
  22. virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority = ""sv) = 0;
  23. virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) = 0;
  24. WebIDL::ExceptionOr<void> set_property(StringView property_name, StringView css_text, StringView priority);
  25. WebIDL::ExceptionOr<String> remove_property(StringView property_name);
  26. String get_property_value(StringView property) const;
  27. StringView get_property_priority(StringView property) const;
  28. String css_text() const;
  29. virtual WebIDL::ExceptionOr<void> set_css_text(StringView) = 0;
  30. virtual String serialized() const = 0;
  31. virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyKey const& name) const override;
  32. virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver, JS::CacheablePropertyMetadata*, PropertyLookupPhase) const override;
  33. virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata*) override;
  34. protected:
  35. explicit CSSStyleDeclaration(JS::Realm&);
  36. };
  37. class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration {
  38. WEB_PLATFORM_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration);
  39. JS_DECLARE_ALLOCATOR(PropertyOwningCSSStyleDeclaration);
  40. friend class ElementInlineCSSStyleDeclaration;
  41. public:
  42. [[nodiscard]] static JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration>
  43. create(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties);
  44. virtual ~PropertyOwningCSSStyleDeclaration() override = default;
  45. virtual size_t length() const override;
  46. virtual String item(size_t index) const override;
  47. virtual Optional<StyleProperty> property(PropertyID) const override;
  48. virtual WebIDL::ExceptionOr<void> set_property(PropertyID, StringView css_text, StringView priority) override;
  49. virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) override;
  50. Vector<StyleProperty> const& properties() const { return m_properties; }
  51. HashMap<FlyString, StyleProperty> const& custom_properties() const { return m_custom_properties; }
  52. Optional<StyleProperty> custom_property(FlyString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); }
  53. size_t custom_property_count() const { return m_custom_properties.size(); }
  54. virtual String serialized() const final override;
  55. virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
  56. protected:
  57. PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty>);
  58. virtual void update_style_attribute() { }
  59. void empty_the_declarations();
  60. void set_the_declarations(Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties);
  61. private:
  62. bool set_a_css_declaration(PropertyID, NonnullRefPtr<StyleValue const>, Important);
  63. virtual void visit_edges(Cell::Visitor&) override;
  64. Vector<StyleProperty> m_properties;
  65. HashMap<FlyString, StyleProperty> m_custom_properties;
  66. };
  67. class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration {
  68. WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration);
  69. JS_DECLARE_ALLOCATOR(ElementInlineCSSStyleDeclaration);
  70. public:
  71. [[nodiscard]] static JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties);
  72. virtual ~ElementInlineCSSStyleDeclaration() override = default;
  73. DOM::Element* element() { return m_element.ptr(); }
  74. const DOM::Element* element() const { return m_element.ptr(); }
  75. bool is_updating() const { return m_updating; }
  76. virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
  77. private:
  78. ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties);
  79. virtual void visit_edges(Cell::Visitor&) override;
  80. virtual void update_style_attribute() override;
  81. JS::GCPtr<DOM::Element> m_element;
  82. // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag
  83. bool m_updating { false };
  84. };
  85. }