CSSStyleDeclaration.h 5.0 KB

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