ShadowRoot.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/HTML/HTMLTemplateElement.h>
  12. #include <LibWeb/HTML/Parser/HTMLParser.h>
  13. #include <LibWeb/Layout/BlockContainer.h>
  14. namespace Web::DOM {
  15. JS_DEFINE_ALLOCATOR(ShadowRoot);
  16. ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
  17. : DocumentFragment(document)
  18. , m_mode(mode)
  19. {
  20. document.register_shadow_root({}, *this);
  21. set_host(&host);
  22. }
  23. void ShadowRoot::finalize()
  24. {
  25. Base::finalize();
  26. document().unregister_shadow_root({}, *this);
  27. }
  28. void ShadowRoot::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. WEB_SET_PROTOTYPE_FOR_INTERFACE(ShadowRoot);
  32. }
  33. // https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
  34. void ShadowRoot::set_onslotchange(WebIDL::CallbackType* event_handler)
  35. {
  36. set_event_handler_attribute(HTML::EventNames::slotchange, event_handler);
  37. }
  38. // https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
  39. WebIDL::CallbackType* ShadowRoot::onslotchange()
  40. {
  41. return event_handler_attribute(HTML::EventNames::slotchange);
  42. }
  43. // https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
  44. EventTarget* ShadowRoot::get_parent(Event const& event)
  45. {
  46. if (!event.composed()) {
  47. auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
  48. if (&events_first_invocation_target.root() == this)
  49. return nullptr;
  50. }
  51. return host();
  52. }
  53. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
  54. WebIDL::ExceptionOr<String> ShadowRoot::inner_html() const
  55. {
  56. return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
  57. }
  58. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
  59. WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView value)
  60. {
  61. // FIXME: 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, the given value, "ShadowRoot innerHTML", and "script".
  62. // 2. Let context be this's host.
  63. auto context = this->host();
  64. VERIFY(context);
  65. // 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString. FIXME: Use compliantString instead of markup.
  66. auto fragment = TRY(context->parse_fragment(value));
  67. // 4. Replace all with fragment within this.
  68. this->replace_all(fragment);
  69. // NOTE: We don't invalidate style & layout for <template> elements since they don't affect rendering.
  70. if (!is<HTML::HTMLTemplateElement>(*this)) {
  71. this->set_needs_style_update(true);
  72. if (this->is_connected()) {
  73. // NOTE: Since the DOM has changed, we have to rebuild the layout tree.
  74. this->document().invalidate_layout();
  75. }
  76. }
  77. set_needs_style_update(true);
  78. return {};
  79. }
  80. // https://html.spec.whatwg.org/#dom-element-gethtml
  81. WebIDL::ExceptionOr<String> ShadowRoot::get_html(GetHTMLOptions const& options) const
  82. {
  83. // ShadowRoot's getHTML(options) method steps are to return the result
  84. // of HTML fragment serialization algorithm with this,
  85. // options["serializableShadowRoots"], and options["shadowRoots"].
  86. return HTML::HTMLParser::serialize_html_fragment(
  87. *this,
  88. options.serializable_shadow_roots ? HTML::HTMLParser::SerializableShadowRoots::Yes : HTML::HTMLParser::SerializableShadowRoots::No,
  89. options.shadow_roots);
  90. }
  91. // https://html.spec.whatwg.org/#dom-shadowroot-sethtmlunsafe
  92. WebIDL::ExceptionOr<void> ShadowRoot::set_html_unsafe(StringView html)
  93. {
  94. // FIXME: 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, html, "ShadowRoot setHTMLUnsafe", and "script".
  95. // 3. Unsafe set HTML given this, this's shadow host, and compliantHTML. FIXME: Use compliantHTML.
  96. TRY(unsafely_set_html(*this->host(), html));
  97. return {};
  98. }
  99. CSS::StyleSheetList& ShadowRoot::style_sheets()
  100. {
  101. if (!m_style_sheets)
  102. m_style_sheets = CSS::StyleSheetList::create(*this);
  103. return *m_style_sheets;
  104. }
  105. CSS::StyleSheetList const& ShadowRoot::style_sheets() const
  106. {
  107. return const_cast<ShadowRoot*>(this)->style_sheets();
  108. }
  109. void ShadowRoot::visit_edges(Visitor& visitor)
  110. {
  111. Base::visit_edges(visitor);
  112. visitor.visit(m_style_sheets);
  113. visitor.visit(m_adopted_style_sheets);
  114. }
  115. JS::NonnullGCPtr<WebIDL::ObservableArray> ShadowRoot::adopted_style_sheets() const
  116. {
  117. if (!m_adopted_style_sheets)
  118. m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
  119. return *m_adopted_style_sheets;
  120. }
  121. WebIDL::ExceptionOr<void> ShadowRoot::set_adopted_style_sheets(JS::Value new_value)
  122. {
  123. if (!m_adopted_style_sheets)
  124. m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
  125. m_adopted_style_sheets->clear();
  126. auto iterator_record = TRY(get_iterator(vm(), new_value, JS::IteratorHint::Sync));
  127. while (true) {
  128. auto next = TRY(iterator_step_value(vm(), iterator_record));
  129. if (!next.has_value())
  130. break;
  131. TRY(m_adopted_style_sheets->append(*next));
  132. }
  133. return {};
  134. }
  135. void ShadowRoot::for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const
  136. {
  137. for (auto& style_sheet : style_sheets().sheets())
  138. callback(*style_sheet);
  139. if (m_adopted_style_sheets) {
  140. m_adopted_style_sheets->for_each<CSS::CSSStyleSheet>([&](auto& style_sheet) {
  141. callback(style_sheet);
  142. });
  143. }
  144. }
  145. Vector<JS::NonnullGCPtr<Animations::Animation>> ShadowRoot::get_animations()
  146. {
  147. Vector<JS::NonnullGCPtr<Animations::Animation>> relevant_animations;
  148. for_each_child_of_type<Element>([&](auto& child) {
  149. relevant_animations.extend(child.get_animations({ .subtree = true }));
  150. return IterationDecision::Continue;
  151. });
  152. return relevant_animations;
  153. }
  154. }