CSSStyleDeclaration.h 4.8 KB

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