CSSStyleDeclaration.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2018-2021, 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/Wrappable.h>
  10. #include <LibWeb/CSS/StyleValue.h>
  11. namespace Web::CSS {
  12. struct StyleProperty {
  13. CSS::PropertyID property_id;
  14. NonnullRefPtr<StyleValue> value;
  15. String custom_name {};
  16. bool important { false };
  17. };
  18. class CSSStyleDeclaration
  19. : public RefCounted<CSSStyleDeclaration>
  20. , public Bindings::Wrappable {
  21. public:
  22. using WrapperType = Bindings::CSSStyleDeclarationWrapper;
  23. static NonnullRefPtr<CSSStyleDeclaration> create(Vector<StyleProperty>&& properties, HashMap<String, StyleProperty>&& custom_properties)
  24. {
  25. return adopt_ref(*new CSSStyleDeclaration(move(properties), move(custom_properties)));
  26. }
  27. virtual ~CSSStyleDeclaration();
  28. const Vector<StyleProperty>& properties() const { return m_properties; }
  29. const Optional<StyleProperty> custom_property(const String& custom_property_name) const { return m_custom_properties.get(custom_property_name); }
  30. size_t custom_property_count() const { return m_custom_properties.size(); };
  31. size_t length() const { return m_properties.size(); }
  32. String item(size_t index) const;
  33. protected:
  34. explicit CSSStyleDeclaration(Vector<StyleProperty>&&, HashMap<String, StyleProperty>&&);
  35. private:
  36. friend class Bindings::CSSStyleDeclarationWrapper;
  37. Vector<StyleProperty> m_properties;
  38. HashMap<String, StyleProperty> m_custom_properties;
  39. };
  40. class ElementInlineCSSStyleDeclaration final : public CSSStyleDeclaration {
  41. public:
  42. static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element& element) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element)); }
  43. virtual ~ElementInlineCSSStyleDeclaration() override;
  44. DOM::Element* element() { return m_element.ptr(); }
  45. const DOM::Element* element() const { return m_element.ptr(); }
  46. private:
  47. explicit ElementInlineCSSStyleDeclaration(DOM::Element&);
  48. WeakPtr<DOM::Element> m_element;
  49. };
  50. }
  51. namespace Web::Bindings {
  52. CSSStyleDeclarationWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleDeclaration&);
  53. }