ShadowRoot.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
  13. : DocumentFragment(document)
  14. , m_mode(mode)
  15. {
  16. set_host(&host);
  17. }
  18. void ShadowRoot::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::ShadowRootPrototype>(realm, "ShadowRoot"));
  22. }
  23. // https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
  24. EventTarget* ShadowRoot::get_parent(Event const& event)
  25. {
  26. if (!event.composed()) {
  27. auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
  28. if (&events_first_invocation_target.root() == this)
  29. return nullptr;
  30. }
  31. return host();
  32. }
  33. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  34. WebIDL::ExceptionOr<DeprecatedString> ShadowRoot::inner_html() const
  35. {
  36. return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
  37. }
  38. // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
  39. WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView markup)
  40. {
  41. TRY(DOMParsing::inner_html_setter(*this, markup));
  42. set_needs_style_update(true);
  43. return {};
  44. }
  45. }