ShadowRoot.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. JS::ThrowCompletionOr<void> ShadowRoot::initialize(JS::Realm& realm)
  19. {
  20. MUST_OR_THROW_OOM(Base::initialize(realm));
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::ShadowRootPrototype>(realm, "ShadowRoot"));
  22. return {};
  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<DeprecatedString> 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(DeprecatedString const& markup)
  41. {
  42. TRY(DOMParsing::inner_html_setter(*this, markup));
  43. set_needs_style_update(true);
  44. return {};
  45. }
  46. }