CSSRule.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. };
  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. // https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
  43. virtual String serialized() const = 0;
  44. protected:
  45. explicit CSSRule(JS::Realm&);
  46. virtual void visit_edges(Cell::Visitor&) override;
  47. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
  48. {
  49. if (!m_cached_layer_name.has_value())
  50. return parent_layer_internal_qualified_name_slow_case();
  51. return m_cached_layer_name.value();
  52. }
  53. [[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const;
  54. JS::GCPtr<CSSRule> m_parent_rule;
  55. JS::GCPtr<CSSStyleSheet> m_parent_style_sheet;
  56. mutable Optional<FlyString> m_cached_layer_name;
  57. };
  58. }