2020-11-21 15:32:39 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-21 15:32:39 -03:00
|
|
|
*/
|
|
|
|
|
|
2024-04-26 21:09:58 -03:00
|
|
|
#include <LibWeb/Bindings/ShadowRootPrototype.h>
|
2024-03-08 15:27:24 -03:00
|
|
|
#include <LibWeb/DOM/AdoptedStyleSheets.h>
|
2021-09-13 18:42:57 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-11-21 15:32:39 -03:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2021-09-13 18:42:57 -03:00
|
|
|
#include <LibWeb/DOMParsing/InnerHTML.h>
|
2021-10-06 15:02:41 -03:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2020-11-21 15:32:39 -03:00
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
2023-11-19 15:47:52 -03:00
|
|
|
JS_DEFINE_ALLOCATOR(ShadowRoot);
|
|
|
|
|
|
2023-01-28 16:36:58 -03:00
|
|
|
ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
|
2020-11-21 15:32:39 -03:00
|
|
|
: DocumentFragment(document)
|
2023-01-28 16:36:58 -03:00
|
|
|
, m_mode(mode)
|
2020-11-21 15:32:39 -03:00
|
|
|
{
|
2024-03-08 20:19:05 -03:00
|
|
|
document.register_shadow_root({}, *this);
|
2022-03-15 20:26:40 -03:00
|
|
|
set_host(&host);
|
2020-11-21 15:32:39 -03:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 20:19:05 -03:00
|
|
|
void ShadowRoot::finalize()
|
|
|
|
|
{
|
|
|
|
|
Base::finalize();
|
|
|
|
|
document().unregister_shadow_root({}, *this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void ShadowRoot::initialize(JS::Realm& realm)
|
2023-01-28 16:26:01 -03:00
|
|
|
{
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm);
|
2024-03-16 09:13:08 -03:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(ShadowRoot);
|
2023-01-28 16:26:01 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-02 15:27:42 -03:00
|
|
|
// https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
|
2022-04-01 14:58:27 -03:00
|
|
|
EventTarget* ShadowRoot::get_parent(Event const& event)
|
2020-11-21 15:32:39 -03:00
|
|
|
{
|
|
|
|
|
if (!event.composed()) {
|
2021-06-24 14:53:42 -03:00
|
|
|
auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
|
2021-09-02 15:27:42 -03:00
|
|
|
if (&events_first_invocation_target.root() == this)
|
2020-11-21 15:32:39 -03:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return host();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 18:42:57 -03:00
|
|
|
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
2023-11-20 05:32:29 -03:00
|
|
|
WebIDL::ExceptionOr<String> ShadowRoot::inner_html() const
|
2021-09-13 18:42:57 -03:00
|
|
|
{
|
2022-11-02 20:14:27 -03:00
|
|
|
return serialize_fragment(DOMParsing::RequireWellFormed::Yes);
|
2021-09-13 18:42:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
2023-09-05 23:52:53 -03:00
|
|
|
WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView markup)
|
2021-09-13 18:42:57 -03:00
|
|
|
{
|
2022-03-22 09:40:10 -03:00
|
|
|
TRY(DOMParsing::inner_html_setter(*this, markup));
|
2021-09-13 18:42:57 -03:00
|
|
|
|
|
|
|
|
set_needs_style_update(true);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 15:27:24 -03:00
|
|
|
CSS::StyleSheetList& ShadowRoot::style_sheets()
|
|
|
|
|
{
|
|
|
|
|
if (!m_style_sheets)
|
|
|
|
|
m_style_sheets = CSS::StyleSheetList::create(document());
|
|
|
|
|
return *m_style_sheets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSS::StyleSheetList const& ShadowRoot::style_sheets() const
|
|
|
|
|
{
|
|
|
|
|
return const_cast<ShadowRoot*>(this)->style_sheets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShadowRoot::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_style_sheets);
|
|
|
|
|
visitor.visit(m_adopted_style_sheets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS::NonnullGCPtr<WebIDL::ObservableArray> ShadowRoot::adopted_style_sheets() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_adopted_style_sheets)
|
|
|
|
|
m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
|
|
|
|
|
return *m_adopted_style_sheets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebIDL::ExceptionOr<void> ShadowRoot::set_adopted_style_sheets(JS::Value new_value)
|
|
|
|
|
{
|
|
|
|
|
if (!m_adopted_style_sheets)
|
|
|
|
|
m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<Document&>(document()));
|
|
|
|
|
|
|
|
|
|
m_adopted_style_sheets->clear();
|
|
|
|
|
auto iterator_record = TRY(get_iterator(vm(), new_value, JS::IteratorHint::Sync));
|
|
|
|
|
while (true) {
|
|
|
|
|
auto next = TRY(iterator_step_value(vm(), iterator_record));
|
|
|
|
|
if (!next.has_value())
|
|
|
|
|
break;
|
|
|
|
|
TRY(m_adopted_style_sheets->append(*next));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 13:01:26 -03:00
|
|
|
void ShadowRoot::for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const
|
|
|
|
|
{
|
|
|
|
|
for (auto& style_sheet : style_sheets().sheets())
|
|
|
|
|
callback(*style_sheet);
|
|
|
|
|
|
|
|
|
|
if (m_adopted_style_sheets) {
|
|
|
|
|
m_adopted_style_sheets->for_each<CSS::CSSStyleSheet>([&](auto& style_sheet) {
|
|
|
|
|
callback(style_sheet);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 15:32:39 -03:00
|
|
|
}
|