ShadowRoot.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/Event.h>
  8. #include <LibWeb/DOM/ShadowRoot.h>
  9. #include <LibWeb/DOMParsing/InnerHTML.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. namespace Web::DOM {
  12. JS_DEFINE_ALLOCATOR(ShadowRoot);
  13. ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
  14. : DocumentFragment(document)
  15. , m_mode(mode)
  16. {
  17. set_host(&host);
  18. }
  19. void ShadowRoot::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::ShadowRootPrototype>(realm, "ShadowRoot"_fly_string));
  23. }
  24. // https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
  25. EventTarget* ShadowRoot::get_parent(Event const& event)
  26. {
  27. if (!event.composed()) {
  28. auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
  29. if (&events_first_invocation_target.root() == this)
  30. return nullptr;
  31. }
  32. return host();
  33. }
  34. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  35. WebIDL::ExceptionOr<String> ShadowRoot::inner_html() const
  36. {
  37. return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
  38. }
  39. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  40. WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView markup)
  41. {
  42. TRY(DOMParsing::inner_html_setter(*this, markup));
  43. set_needs_style_update(true);
  44. return {};
  45. }
  46. }