ShadowRoot.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/#dom-shadowroot-onslotchange
  33. void ShadowRoot::set_onslotchange(WebIDL::CallbackType* event_handler)
  34. {
  35. set_event_handler_attribute(HTML::EventNames::slotchange, event_handler);
  36. }
  37. // https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
  38. WebIDL::CallbackType* ShadowRoot::onslotchange()
  39. {
  40. return event_handler_attribute(HTML::EventNames::slotchange);
  41. }
  42. // https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
  43. EventTarget* ShadowRoot::get_parent(Event const& event)
  44. {
  45. if (!event.composed()) {
  46. auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
  47. if (&events_first_invocation_target.root() == this)
  48. return nullptr;
  49. }
  50. return host();
  51. }
  52. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  53. WebIDL::ExceptionOr<String> ShadowRoot::inner_html() const
  54. {
  55. return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
  56. }
  57. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  58. WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView markup)
  59. {
  60. TRY(DOMParsing::inner_html_setter(*this, markup));
  61. set_needs_style_update(true);
  62. return {};
  63. }
  64. CSS::StyleSheetList& ShadowRoot::style_sheets()
  65. {
  66. if (!m_style_sheets)
  67. m_style_sheets = CSS::StyleSheetList::create(document());
  68. return *m_style_sheets;
  69. }
  70. CSS::StyleSheetList const& ShadowRoot::style_sheets() const
  71. {
  72. return const_cast<ShadowRoot*>(this)->style_sheets();
  73. }
  74. void ShadowRoot::visit_edges(Visitor& visitor)
  75. {
  76. Base::visit_edges(visitor);
  77. visitor.visit(m_style_sheets);
  78. visitor.visit(m_adopted_style_sheets);
  79. }
  80. JS::NonnullGCPtr<WebIDL::ObservableArray> ShadowRoot::adopted_style_sheets() const
  81. {
  82. if (!m_adopted_style_sheets)
  83. m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
  84. return *m_adopted_style_sheets;
  85. }
  86. WebIDL::ExceptionOr<void> ShadowRoot::set_adopted_style_sheets(JS::Value new_value)
  87. {
  88. if (!m_adopted_style_sheets)
  89. m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
  90. m_adopted_style_sheets->clear();
  91. auto iterator_record = TRY(get_iterator(vm(), new_value, JS::IteratorHint::Sync));
  92. while (true) {
  93. auto next = TRY(iterator_step_value(vm(), iterator_record));
  94. if (!next.has_value())
  95. break;
  96. TRY(m_adopted_style_sheets->append(*next));
  97. }
  98. return {};
  99. }
  100. void ShadowRoot::for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const
  101. {
  102. for (auto& style_sheet : style_sheets().sheets())
  103. callback(*style_sheet);
  104. if (m_adopted_style_sheets) {
  105. m_adopted_style_sheets->for_each<CSS::CSSStyleSheet>([&](auto& style_sheet) {
  106. callback(style_sheet);
  107. });
  108. }
  109. }
  110. }