ShadowRoot.cpp 3.2 KB

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