Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
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
13namespace Web::DOM {
14
15ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
16 : DocumentFragment(document)
17 , m_mode(mode)
18{
19 set_host(&host);
20}
21
22JS::ThrowCompletionOr<void> ShadowRoot::initialize(JS::Realm& realm)
23{
24 MUST_OR_THROW_OOM(Base::initialize(realm));
25 set_prototype(&Bindings::ensure_web_prototype<Bindings::ShadowRootPrototype>(realm, "ShadowRoot"));
26 return {};
27}
28
29// https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
30EventTarget* ShadowRoot::get_parent(Event const& event)
31{
32 if (!event.composed()) {
33 auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
34 if (&events_first_invocation_target.root() == this)
35 return nullptr;
36 }
37
38 return host();
39}
40
41// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
42WebIDL::ExceptionOr<DeprecatedString> ShadowRoot::inner_html() const
43{
44 return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
45}
46
47// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
48WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(DeprecatedString const& markup)
49{
50 TRY(DOMParsing::inner_html_setter(*this, markup));
51
52 set_needs_style_update(true);
53 return {};
54}
55
56}