CSSRule.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  12. #include <LibWeb/CSS/Selector.h>
  13. #include <LibWeb/WebIDL/Types.h>
  14. namespace Web::CSS {
  15. class CSSRule : public Bindings::PlatformObject {
  16. WEB_PLATFORM_OBJECT(CSSRule, Bindings::PlatformObject);
  17. public:
  18. virtual ~CSSRule() = default;
  19. // https://drafts.csswg.org/cssom/#dom-cssrule-type
  20. enum class Type : WebIDL::UnsignedShort {
  21. Style = 1,
  22. Import = 3,
  23. Media = 4,
  24. FontFace = 5,
  25. Keyframes = 7,
  26. Keyframe = 8,
  27. Namespace = 10,
  28. Supports = 12,
  29. // AD-HOC: These are not included in the spec, but we need them internally. So, their numbers are arbitrary.
  30. LayerBlock = 100,
  31. LayerStatement = 101,
  32. NestedDeclarations = 102,
  33. Property = 103,
  34. };
  35. Type type() const { return m_type; }
  36. WebIDL::UnsignedShort type_for_bindings() const;
  37. String css_text() const;
  38. void set_css_text(StringView);
  39. CSSRule* parent_rule() { return m_parent_rule.ptr(); }
  40. CSSRule const* parent_rule() const { return m_parent_rule.ptr(); }
  41. void set_parent_rule(CSSRule*);
  42. CSSStyleSheet* parent_style_sheet() { return m_parent_style_sheet.ptr(); }
  43. virtual void set_parent_style_sheet(CSSStyleSheet*);
  44. template<typename T>
  45. bool fast_is() const = delete;
  46. // https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
  47. virtual String serialized() const = 0;
  48. virtual void clear_caches();
  49. protected:
  50. explicit CSSRule(JS::Realm&, Type);
  51. virtual void visit_edges(Cell::Visitor&) override;
  52. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
  53. {
  54. if (!m_cached_layer_name.has_value())
  55. return parent_layer_internal_qualified_name_slow_case();
  56. return m_cached_layer_name.value();
  57. }
  58. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const;
  59. Type m_type;
  60. JS::GCPtr<CSSRule> m_parent_rule;
  61. JS::GCPtr<CSSStyleSheet> m_parent_style_sheet;
  62. mutable Optional<FlyString> m_cached_layer_name;
  63. };
  64. }