CSSNestedDeclarations.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSNestedDeclarations.h"
  7. #include <LibWeb/Bindings/CSSNestedDeclarationsPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/CSSStyleRule.h>
  10. namespace Web::CSS {
  11. JS_DEFINE_ALLOCATOR(CSSNestedDeclarations);
  12. JS::NonnullGCPtr<CSSNestedDeclarations> CSSNestedDeclarations::create(JS::Realm& realm, PropertyOwningCSSStyleDeclaration& declaration)
  13. {
  14. return realm.create<CSSNestedDeclarations>(realm, declaration);
  15. }
  16. CSSNestedDeclarations::CSSNestedDeclarations(JS::Realm& realm, PropertyOwningCSSStyleDeclaration& declaration)
  17. : CSSRule(realm, Type::NestedDeclarations)
  18. , m_declaration(declaration)
  19. {
  20. m_declaration->set_parent_rule(*this);
  21. }
  22. void CSSNestedDeclarations::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSNestedDeclarations);
  26. }
  27. void CSSNestedDeclarations::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_declaration);
  31. visitor.visit(m_parent_style_rule);
  32. }
  33. CSSStyleDeclaration* CSSNestedDeclarations::style()
  34. {
  35. return m_declaration;
  36. }
  37. CSSStyleRule const& CSSNestedDeclarations::parent_style_rule() const
  38. {
  39. if (m_parent_style_rule)
  40. return *m_parent_style_rule;
  41. for (auto* parent = parent_rule(); parent; parent = parent->parent_rule()) {
  42. if (is<CSSStyleRule>(parent)) {
  43. m_parent_style_rule = static_cast<CSSStyleRule const*>(parent);
  44. return *m_parent_style_rule;
  45. }
  46. }
  47. dbgln("CSSNestedDeclarations has no parent style rule!");
  48. VERIFY_NOT_REACHED();
  49. }
  50. String CSSNestedDeclarations::serialized() const
  51. {
  52. // NOTE: There's no proper spec for this yet, only this note:
  53. // "The CSSNestedDeclarations rule serializes as if its declaration block had been serialized directly."
  54. // - https://drafts.csswg.org/css-nesting-1/#ref-for-cssnesteddeclarations%E2%91%A1
  55. // So, we'll do the simple thing and hope it's good.
  56. return m_declaration->serialized();
  57. }
  58. void CSSNestedDeclarations::clear_caches()
  59. {
  60. Base::clear_caches();
  61. m_parent_style_rule = nullptr;
  62. }
  63. }