CSSRule.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.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. };
  32. virtual Type type() const = 0;
  33. String css_text() const;
  34. void set_css_text(StringView);
  35. CSSRule* parent_rule() { return m_parent_rule.ptr(); }
  36. CSSRule const* parent_rule() const { return m_parent_rule.ptr(); }
  37. void set_parent_rule(CSSRule*);
  38. CSSStyleSheet* parent_style_sheet() { return m_parent_style_sheet.ptr(); }
  39. virtual void set_parent_style_sheet(CSSStyleSheet*);
  40. template<typename T>
  41. bool fast_is() const = delete;
  42. protected:
  43. explicit CSSRule(JS::Realm&);
  44. virtual String serialized() const = 0;
  45. virtual void visit_edges(Cell::Visitor&) override;
  46. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
  47. {
  48. if (!m_cached_layer_name.has_value())
  49. return parent_layer_internal_qualified_name_slow_case();
  50. return m_cached_layer_name.value();
  51. }
  52. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const;
  53. JS::GCPtr<CSSRule> m_parent_rule;
  54. JS::GCPtr<CSSStyleSheet> m_parent_style_sheet;
  55. mutable Optional<FlyString> m_cached_layer_name;
  56. };
  57. }