ShadowRoot.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/ShadowRootPrototype.h>
  7. #include <LibWeb/DOM/AdoptedStyleSheets.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. #include <LibWeb/DOMParsing/InnerHTML.h>
  12. #include <LibWeb/Layout/BlockContainer.h>
  13. namespace Web::DOM {
  14. JS_DEFINE_ALLOCATOR(ShadowRoot);
  15. ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
  16. : DocumentFragment(document)
  17. , m_mode(mode)
  18. {
  19. document.register_shadow_root({}, *this);
  20. set_host(&host);
  21. }
  22. void ShadowRoot::finalize()
  23. {
  24. Base::finalize();
  25. document().unregister_shadow_root({}, *this);
  26. }
  27. void ShadowRoot::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(ShadowRoot);
  31. }
  32. // https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
  33. EventTarget* ShadowRoot::get_parent(Event const& event)
  34. {
  35. if (!event.composed()) {
  36. auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
  37. if (&events_first_invocation_target.root() == this)
  38. return nullptr;
  39. }
  40. return host();
  41. }
  42. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  43. WebIDL::ExceptionOr<String> ShadowRoot::inner_html() const
  44. {
  45. return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
  46. }
  47. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  48. WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView markup)
  49. {
  50. TRY(DOMParsing::inner_html_setter(*this, markup));
  51. set_needs_style_update(true);
  52. return {};
  53. }
  54. CSS::StyleSheetList& ShadowRoot::style_sheets()
  55. {
  56. if (!m_style_sheets)
  57. m_style_sheets = CSS::StyleSheetList::create(document());
  58. return *m_style_sheets;
  59. }
  60. CSS::StyleSheetList const& ShadowRoot::style_sheets() const
  61. {
  62. return const_cast<ShadowRoot*>(this)->style_sheets();
  63. }
  64. void ShadowRoot::visit_edges(Visitor& visitor)
  65. {
  66. Base::visit_edges(visitor);
  67. visitor.visit(m_style_sheets);
  68. visitor.visit(m_adopted_style_sheets);
  69. }
  70. JS::NonnullGCPtr<WebIDL::ObservableArray> ShadowRoot::adopted_style_sheets() const
  71. {
  72. if (!m_adopted_style_sheets)
  73. m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
  74. return *m_adopted_style_sheets;
  75. }
  76. WebIDL::ExceptionOr<void> ShadowRoot::set_adopted_style_sheets(JS::Value new_value)
  77. {
  78. if (!m_adopted_style_sheets)
  79. m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
  80. m_adopted_style_sheets->clear();
  81. auto iterator_record = TRY(get_iterator(vm(), new_value, JS::IteratorHint::Sync));
  82. while (true) {
  83. auto next = TRY(iterator_step_value(vm(), iterator_record));
  84. if (!next.has_value())
  85. break;
  86. TRY(m_adopted_style_sheets->append(*next));
  87. }
  88. return {};
  89. }
  90. void ShadowRoot::for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const
  91. {
  92. for (auto& style_sheet : style_sheets().sheets())
  93. callback(*style_sheet);
  94. if (m_adopted_style_sheets) {
  95. m_adopted_style_sheets->for_each<CSS::CSSStyleSheet>([&](auto& style_sheet) {
  96. callback(style_sheet);
  97. });
  98. }
  99. }
  100. }