CSSRule.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. namespace Web::CSS {
  14. class CSSRule : public Bindings::PlatformObject {
  15. WEB_PLATFORM_OBJECT(CSSRule, Bindings::PlatformObject);
  16. public:
  17. virtual ~CSSRule() = default;
  18. // https://drafts.csswg.org/cssom/#dom-cssrule-type
  19. enum class Type : u16 {
  20. Style = 1,
  21. Import = 3,
  22. Media = 4,
  23. FontFace = 5,
  24. Keyframes = 7,
  25. Keyframe = 8,
  26. Namespace = 10,
  27. Supports = 12,
  28. // AD-HOC: These are not included in the spec, but we need them internally. So, their numbers are arbitrary.
  29. LayerBlock = 100,
  30. LayerStatement = 101,
  31. NestedDeclarations = 102,
  32. Property = 103, // FIXME: This should return `0` as a type, but type is used for a lot of dispatching
  33. };
  34. Type type() const { return m_type; }
  35. String css_text() const;
  36. void set_css_text(StringView);
  37. CSSRule* parent_rule() { return m_parent_rule.ptr(); }
  38. CSSRule const* parent_rule() const { return m_parent_rule.ptr(); }
  39. void set_parent_rule(CSSRule*);
  40. CSSStyleSheet* parent_style_sheet() { return m_parent_style_sheet.ptr(); }
  41. virtual void set_parent_style_sheet(CSSStyleSheet*);
  42. template<typename T>
  43. bool fast_is() const = delete;
  44. // https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
  45. virtual String serialized() const = 0;
  46. protected:
  47. explicit CSSRule(JS::Realm&, Type);
  48. virtual void visit_edges(Cell::Visitor&) override;
  49. virtual void clear_caches();
  50. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
  51. {
  52. if (!m_cached_layer_name.has_value())
  53. return parent_layer_internal_qualified_name_slow_case();
  54. return m_cached_layer_name.value();
  55. }
  56. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const;
  57. Type m_type;
  58. JS::GCPtr<CSSRule> m_parent_rule;
  59. JS::GCPtr<CSSStyleSheet> m_parent_style_sheet;
  60. mutable Optional<FlyString> m_cached_layer_name;
  61. };
  62. }