2020-11-21 18:32:39 +00:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-21 18:32:39 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/ShadowRootPrototype.h>
|
2026-02-11 07:33:58 +01:00
|
|
|
|
#include <LibWeb/CSS/CSSStyleSheet.h>
|
|
|
|
|
|
#include <LibWeb/CSS/StyleSheetList.h>
|
2024-03-08 19:27:24 +01:00
|
|
|
|
#include <LibWeb/DOM/AdoptedStyleSheets.h>
|
2021-09-13 22:42:57 +01:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2025-02-02 22:04:01 -08:00
|
|
|
|
#include <LibWeb/DOM/DocumentOrShadowRoot.h>
|
2020-11-21 18:32:39 +00:00
|
|
|
|
#include <LibWeb/DOM/Event.h>
|
|
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2026-01-28 13:12:46 +00:00
|
|
|
|
#include <LibWeb/DOM/SlotRegistry.h>
|
2025-03-31 19:38:15 +02:00
|
|
|
|
#include <LibWeb/DOM/Utils.h>
|
2026-02-12 09:00:25 +00:00
|
|
|
|
#include <LibWeb/HTML/AttributeNames.h>
|
2025-04-23 13:02:04 +01:00
|
|
|
|
#include <LibWeb/HTML/CustomElements/CustomElementRegistry.h>
|
2026-01-28 13:12:46 +00:00
|
|
|
|
#include <LibWeb/HTML/HTMLSlotElement.h>
|
2024-06-25 19:51:18 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLTemplateElement.h>
|
2024-06-25 14:52:06 +02:00
|
|
|
|
#include <LibWeb/HTML/Parser/HTMLParser.h>
|
2026-02-11 09:20:13 +01:00
|
|
|
|
#include <LibWeb/HTML/XMLSerializer.h>
|
2021-10-06 20:02:41 +02:00
|
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2025-08-11 00:01:41 +02:00
|
|
|
|
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
|
|
|
|
|
|
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
|
2020-11-21 18:32:39 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(ShadowRoot);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2023-01-28 20:36:58 +01:00
|
|
|
|
ShadowRoot::ShadowRoot(Document& document, Element& host, Bindings::ShadowRootMode mode)
|
2020-11-21 18:32:39 +00:00
|
|
|
|
: DocumentFragment(document)
|
2023-01-28 20:36:58 +01:00
|
|
|
|
, m_mode(mode)
|
LibWeb: Add StyleScope to keep style caches per Document/ShadowRoot
Before this change, we've been maintaining various StyleComputer caches
at the document level.
This made sense for old-school documents without shadow trees, since
all the style information was document-wide anyway. However, documents
with many shadow trees ended up suffering since any time you mutated
a style sheet inside a shadow tree, *all* style caches for the entire
document would get invalidated.
This was particularly expensive on Reddit, which has tons of shadow
trees with their own style elements. Every time we'd create one of their
custom elements, we'd invalidate the document-level "rule cache" and
have to rebuild it, taking about ~60ms each time (ouch).
This commit introduces a new object called StyleScope.
Every Document and ShadowRoot has its own StyleScope. Rule caches etc
are moved from StyleComputer to StyleScope.
Rule cache invalidation now happens at StyleScope level. As an example,
rule cache rebuilds now take ~1ms on Reddit instead of ~60ms.
This is largely a mechanical change, moving things around, but there's
one key detail to be aware of: due to the :host selector, which works
across the shadow DOM boundary and reaches from inside a shadow tree out
into the light tree, there are various places where we have to check
both the shadow tree's StyleScope *and* the document-level StyleScope
in order to get all rules that may apply.
2025-11-13 19:08:08 +01:00
|
|
|
|
, m_style_scope(*this)
|
2020-11-21 18:32:39 +00:00
|
|
|
|
{
|
2024-03-09 00:19:05 +01:00
|
|
|
|
document.register_shadow_root({}, *this);
|
2022-03-16 00:26:40 +01:00
|
|
|
|
set_host(&host);
|
2020-11-21 18:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-09 00:19:05 +01:00
|
|
|
|
void ShadowRoot::finalize()
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::finalize();
|
|
|
|
|
|
document().unregister_shadow_root({}, *this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-31 19:38:15 +02:00
|
|
|
|
// https://fullscreen.spec.whatwg.org/#dom-document-fullscreenelement
|
|
|
|
|
|
GC::Ptr<Element> ShadowRoot::fullscreen_element_for_bindings() const
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. If this is a shadow root and its host is not connected, then return null.
|
|
|
|
|
|
if (!host() || !host()->is_connected())
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Let candidate be the result of retargeting fullscreen element against this.
|
2026-02-28 12:06:27 -05:00
|
|
|
|
// NB: ShadowRoot does not have it's own top layer. But the algorithm says to get the fullscreen element from the
|
|
|
|
|
|
// top layer, so it's grabbed from this' document.
|
|
|
|
|
|
auto* candidate = retarget(document().fullscreen_element(), const_cast<ShadowRoot*>(this));
|
2025-03-31 19:38:15 +02:00
|
|
|
|
if (!candidate)
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
// 3. If candidate and this are in the same tree, then return candidate.
|
|
|
|
|
|
if (auto* retargeted_element = as<Element>(candidate); &retargeted_element->root() == &root())
|
|
|
|
|
|
return retargeted_element;
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Return null.
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void ShadowRoot::initialize(JS::Realm& realm)
|
2023-01-28 20:26:01 +01:00
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(ShadowRoot);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-01-28 20:26:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-02 00:01:22 +01:00
|
|
|
|
// https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
|
|
|
|
|
|
void ShadowRoot::set_onslotchange(WebIDL::CallbackType* event_handler)
|
|
|
|
|
|
{
|
|
|
|
|
|
set_event_handler_attribute(HTML::EventNames::slotchange, event_handler);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
|
|
|
|
|
|
WebIDL::CallbackType* ShadowRoot::onslotchange()
|
|
|
|
|
|
{
|
|
|
|
|
|
return event_handler_attribute(HTML::EventNames::slotchange);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-02 19:27:42 +01:00
|
|
|
|
// https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
|
2022-04-01 20:58:27 +03:00
|
|
|
|
EventTarget* ShadowRoot::get_parent(Event const& event)
|
2020-11-21 18:32:39 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (!event.composed()) {
|
2025-01-21 09:12:05 -05:00
|
|
|
|
auto& events_first_invocation_target = as<Node>(*event.path().first().invocation_target);
|
2021-09-02 19:27:42 +01:00
|
|
|
|
if (&events_first_invocation_target.root() == this)
|
2020-11-21 18:32:39 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return host();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-25 19:51:18 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
|
2025-08-11 00:01:41 +02:00
|
|
|
|
WebIDL::ExceptionOr<TrustedTypes::TrustedHTMLOrString> ShadowRoot::inner_html() const
|
2021-09-13 22:42:57 +01:00
|
|
|
|
{
|
2025-08-11 00:01:41 +02:00
|
|
|
|
return TRY(serialize_fragment(HTML::RequireWellFormed::Yes));
|
2021-09-13 22:42:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-25 19:51:18 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
|
2025-08-11 00:01:41 +02:00
|
|
|
|
WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(TrustedTypes::TrustedHTMLOrString const& value)
|
2021-09-13 22:42:57 +01:00
|
|
|
|
{
|
2025-08-11 00:01:41 +02:00
|
|
|
|
// 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
|
|
|
|
|
|
// TrustedHTML, this's relevant global object, the given value, "ShadowRoot innerHTML", and "script".
|
|
|
|
|
|
auto const compliant_string = TRY(TrustedTypes::get_trusted_type_compliant_string(
|
|
|
|
|
|
TrustedTypes::TrustedTypeName::TrustedHTML,
|
|
|
|
|
|
HTML::relevant_global_object(*this),
|
|
|
|
|
|
value,
|
2025-11-04 15:27:46 +00:00
|
|
|
|
TrustedTypes::InjectionSink::ShadowRoot_innerHTML,
|
2025-08-11 00:01:41 +02:00
|
|
|
|
TrustedTypes::Script.to_string()));
|
2024-06-25 19:51:18 +01:00
|
|
|
|
|
|
|
|
|
|
// 2. Let context be this's host.
|
|
|
|
|
|
auto context = this->host();
|
2024-08-08 14:19:14 +01:00
|
|
|
|
VERIFY(context);
|
2024-06-25 19:51:18 +01:00
|
|
|
|
|
2025-08-11 00:01:41 +02:00
|
|
|
|
// 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString.
|
|
|
|
|
|
auto fragment = TRY(context->parse_fragment(compliant_string.to_utf8_but_should_be_ported_to_utf16()));
|
2024-06-25 19:51:18 +01:00
|
|
|
|
|
|
|
|
|
|
// 4. Replace all with fragment within this.
|
|
|
|
|
|
this->replace_all(fragment);
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: We don't invalidate style & layout for <template> elements since they don't affect rendering.
|
|
|
|
|
|
if (!is<HTML::HTMLTemplateElement>(*this)) {
|
|
|
|
|
|
this->set_needs_style_update(true);
|
|
|
|
|
|
|
|
|
|
|
|
if (this->is_connected()) {
|
|
|
|
|
|
// NOTE: Since the DOM has changed, we have to rebuild the layout tree.
|
2025-03-05 21:04:20 +01:00
|
|
|
|
this->document().invalidate_layout_tree(InvalidateLayoutTreeReason::ShadowRootSetInnerHTML);
|
2024-06-25 19:51:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-13 22:42:57 +01:00
|
|
|
|
|
|
|
|
|
|
set_needs_style_update(true);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-04 13:01:46 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-gethtml
|
2024-06-25 14:52:06 +02:00
|
|
|
|
WebIDL::ExceptionOr<String> ShadowRoot::get_html(GetHTMLOptions const& options) const
|
|
|
|
|
|
{
|
|
|
|
|
|
// ShadowRoot's getHTML(options) method steps are to return the result
|
|
|
|
|
|
// of HTML fragment serialization algorithm with this,
|
|
|
|
|
|
// options["serializableShadowRoots"], and options["shadowRoots"].
|
|
|
|
|
|
return HTML::HTMLParser::serialize_html_fragment(
|
|
|
|
|
|
*this,
|
|
|
|
|
|
options.serializable_shadow_roots ? HTML::HTMLParser::SerializableShadowRoots::Yes : HTML::HTMLParser::SerializableShadowRoots::No,
|
|
|
|
|
|
options.shadow_roots);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-04 13:01:46 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-sethtmlunsafe
|
2025-08-11 00:01:41 +02:00
|
|
|
|
WebIDL::ExceptionOr<void> ShadowRoot::set_html_unsafe(TrustedTypes::TrustedHTMLOrString const& html)
|
2024-06-25 20:55:58 +01:00
|
|
|
|
{
|
2025-08-11 00:01:41 +02:00
|
|
|
|
// 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with
|
|
|
|
|
|
// TrustedHTML, this's relevant global object, html, "ShadowRoot setHTMLUnsafe", and "script".
|
|
|
|
|
|
auto const compliant_html = TRY(TrustedTypes::get_trusted_type_compliant_string(
|
|
|
|
|
|
TrustedTypes::TrustedTypeName::TrustedHTML,
|
|
|
|
|
|
HTML::relevant_global_object(*this),
|
|
|
|
|
|
html,
|
2025-11-04 15:27:46 +00:00
|
|
|
|
TrustedTypes::InjectionSink::ShadowRoot_setHTMLUnsafe,
|
2025-08-11 00:01:41 +02:00
|
|
|
|
TrustedTypes::Script.to_string()));
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Unsafely set HTML given this, this's shadow host, and compliantHTML.
|
|
|
|
|
|
TRY(unsafely_set_html(*this->host(), compliant_html.to_utf8_but_should_be_ported_to_utf16()));
|
2024-06-25 20:55:58 +01:00
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-02 22:04:01 -08:00
|
|
|
|
GC::Ptr<Element> ShadowRoot::active_element()
|
|
|
|
|
|
{
|
|
|
|
|
|
return calculate_active_element(*this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-08 19:27:24 +01:00
|
|
|
|
CSS::StyleSheetList& ShadowRoot::style_sheets()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_style_sheets)
|
2024-08-20 14:55:28 +02:00
|
|
|
|
m_style_sheets = CSS::StyleSheetList::create(*this);
|
2024-03-08 19:27:24 +01:00
|
|
|
|
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);
|
LibWeb: Add StyleScope to keep style caches per Document/ShadowRoot
Before this change, we've been maintaining various StyleComputer caches
at the document level.
This made sense for old-school documents without shadow trees, since
all the style information was document-wide anyway. However, documents
with many shadow trees ended up suffering since any time you mutated
a style sheet inside a shadow tree, *all* style caches for the entire
document would get invalidated.
This was particularly expensive on Reddit, which has tons of shadow
trees with their own style elements. Every time we'd create one of their
custom elements, we'd invalidate the document-level "rule cache" and
have to rebuild it, taking about ~60ms each time (ouch).
This commit introduces a new object called StyleScope.
Every Document and ShadowRoot has its own StyleScope. Rule caches etc
are moved from StyleComputer to StyleScope.
Rule cache invalidation now happens at StyleScope level. As an example,
rule cache rebuilds now take ~1ms on Reddit instead of ~60ms.
This is largely a mechanical change, moving things around, but there's
one key detail to be aware of: due to the :host selector, which works
across the shadow DOM boundary and reaches from inside a shadow tree out
into the light tree, there are various places where we have to check
both the shadow tree's StyleScope *and* the document-level StyleScope
in order to get all rules that may apply.
2025-11-13 19:08:08 +01:00
|
|
|
|
m_style_scope.visit_edges(visitor);
|
2024-03-08 19:27:24 +01:00
|
|
|
|
visitor.visit(m_style_sheets);
|
|
|
|
|
|
visitor.visit(m_adopted_style_sheets);
|
2026-03-30 17:15:58 +02:00
|
|
|
|
m_anchor_name_map.visit_edges(visitor);
|
2025-12-08 15:41:02 +00:00
|
|
|
|
for (auto const& [key, elements] : m_part_element_map) {
|
|
|
|
|
|
for (auto const& element : elements)
|
|
|
|
|
|
element.visit(visitor);
|
|
|
|
|
|
}
|
2025-04-23 13:02:04 +01:00
|
|
|
|
visitor.visit(m_custom_element_registry);
|
2024-03-08 19:27:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<WebIDL::ObservableArray> ShadowRoot::adopted_style_sheets() const
|
2024-03-08 19:27:24 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (!m_adopted_style_sheets)
|
2025-01-10 20:00:43 +03:00
|
|
|
|
m_adopted_style_sheets = create_adopted_style_sheets_list(const_cast<ShadowRoot&>(*this));
|
2024-03-08 19:27:24 +01:00
|
|
|
|
return *m_adopted_style_sheets;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WebIDL::ExceptionOr<void> ShadowRoot::set_adopted_style_sheets(JS::Value new_value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_adopted_style_sheets)
|
2025-01-10 20:00:43 +03:00
|
|
|
|
m_adopted_style_sheets = create_adopted_style_sheets_list(*this);
|
2024-03-08 19:27:24 +01:00
|
|
|
|
|
|
|
|
|
|
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 17:01:26 +01: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);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 10:20:32 +13:00
|
|
|
|
void ShadowRoot::for_each_active_css_style_sheet(Function<void(CSS::CSSStyleSheet&)> const& callback) const
|
LibWeb: Add StyleScope to keep style caches per Document/ShadowRoot
Before this change, we've been maintaining various StyleComputer caches
at the document level.
This made sense for old-school documents without shadow trees, since
all the style information was document-wide anyway. However, documents
with many shadow trees ended up suffering since any time you mutated
a style sheet inside a shadow tree, *all* style caches for the entire
document would get invalidated.
This was particularly expensive on Reddit, which has tons of shadow
trees with their own style elements. Every time we'd create one of their
custom elements, we'd invalidate the document-level "rule cache" and
have to rebuild it, taking about ~60ms each time (ouch).
This commit introduces a new object called StyleScope.
Every Document and ShadowRoot has its own StyleScope. Rule caches etc
are moved from StyleComputer to StyleScope.
Rule cache invalidation now happens at StyleScope level. As an example,
rule cache rebuilds now take ~1ms on Reddit instead of ~60ms.
This is largely a mechanical change, moving things around, but there's
one key detail to be aware of: due to the :host selector, which works
across the shadow DOM boundary and reaches from inside a shadow tree out
into the light tree, there are various places where we have to check
both the shadow tree's StyleScope *and* the document-level StyleScope
in order to get all rules that may apply.
2025-11-13 19:08:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
for (auto& style_sheet : style_sheets().sheets()) {
|
|
|
|
|
|
if (!style_sheet->disabled())
|
|
|
|
|
|
callback(*style_sheet);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (m_adopted_style_sheets) {
|
|
|
|
|
|
m_adopted_style_sheets->for_each<CSS::CSSStyleSheet>([&](auto& style_sheet) {
|
|
|
|
|
|
if (!style_sheet.disabled())
|
|
|
|
|
|
callback(style_sheet);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
WebIDL::ExceptionOr<Vector<GC::Ref<Animations::Animation>>> ShadowRoot::get_animations()
|
2024-06-02 07:26:36 -07:00
|
|
|
|
{
|
2025-11-20 16:18:15 +01:00
|
|
|
|
document().update_style();
|
2025-11-10 16:27:00 +01:00
|
|
|
|
return calculate_get_animations(*this);
|
2024-06-02 07:26:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-25 17:30:52 +00:00
|
|
|
|
ElementByIdMap& ShadowRoot::element_by_id() const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_element_by_id)
|
|
|
|
|
|
m_element_by_id = make<ElementByIdMap>();
|
|
|
|
|
|
return *m_element_by_id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 13:12:46 +00:00
|
|
|
|
void ShadowRoot::register_slot(HTML::HTMLSlotElement& slot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_slot_registry)
|
|
|
|
|
|
m_slot_registry = make<SlotRegistry>();
|
|
|
|
|
|
m_slot_registry->add(slot);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ShadowRoot::unregister_slot(HTML::HTMLSlotElement& slot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_slot_registry)
|
|
|
|
|
|
m_slot_registry->remove(slot);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GC::Ptr<HTML::HTMLSlotElement> ShadowRoot::first_slot_with_name(FlyString const& name) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_slot_registry)
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
return m_slot_registry->first_slot_with_name(name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-13 10:06:54 +00:00
|
|
|
|
// https://drafts.csswg.org/css-shadow-1/#shadow-root-part-element-map
|
2025-12-08 15:41:02 +00:00
|
|
|
|
ShadowRoot::PartElementMap const& ShadowRoot::part_element_map() const
|
|
|
|
|
|
{
|
|
|
|
|
|
// FIXME: dom_tree_version() is crude and invalidates more than necessary.
|
|
|
|
|
|
// Come up with a smarter way of invalidating this if it turns out to be slow.
|
|
|
|
|
|
if (m_dom_tree_version_when_calculated_part_element_map < document().dom_tree_version()) {
|
|
|
|
|
|
const_cast<ShadowRoot*>(this)->calculate_part_element_map();
|
|
|
|
|
|
m_dom_tree_version_when_calculated_part_element_map = document().dom_tree_version();
|
|
|
|
|
|
}
|
|
|
|
|
|
return m_part_element_map;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 09:00:25 +00:00
|
|
|
|
// https://drafts.csswg.org/css-shadow-1/#exportparts
|
|
|
|
|
|
// Parse the exportparts attribute into a list of (inner_name, outer_name) pairs.
|
|
|
|
|
|
struct ExportedPart {
|
|
|
|
|
|
FlyString inner_name;
|
|
|
|
|
|
FlyString outer_name;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Vector<ExportedPart> parse_exportparts_attribute(Element const& element)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector<ExportedPart> result;
|
|
|
|
|
|
auto exportparts = element.get_attribute(HTML::AttributeNames::exportparts);
|
|
|
|
|
|
if (!exportparts.has_value())
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
exportparts->code_points().for_each_split_view([](u32 c) { return c == ','; }, SplitBehavior::Nothing, [&](Utf8View mapping) {
|
|
|
|
|
|
auto trimmed = mapping.as_string().trim_whitespace();
|
|
|
|
|
|
if (trimmed.is_empty())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
auto parts = trimmed.split_view(':', SplitBehavior::KeepEmpty);
|
|
|
|
|
|
if (parts.size() == 1) {
|
|
|
|
|
|
auto name = MUST(FlyString::from_utf8(parts[0].trim_whitespace()));
|
|
|
|
|
|
result.append({ name, name });
|
|
|
|
|
|
} else if (parts.size() == 2) {
|
|
|
|
|
|
auto inner_name = MUST(FlyString::from_utf8(parts[0].trim_whitespace()));
|
|
|
|
|
|
auto outer_name = MUST(FlyString::from_utf8(parts[1].trim_whitespace()));
|
|
|
|
|
|
result.append({ inner_name, outer_name });
|
|
|
|
|
|
} });
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-13 10:06:54 +00:00
|
|
|
|
// https://drafts.csswg.org/css-shadow-1/#calculate-the-part-element-map
|
2025-12-08 15:41:02 +00:00
|
|
|
|
void ShadowRoot::calculate_part_element_map()
|
|
|
|
|
|
{
|
|
|
|
|
|
// To calculate the part element map of a shadow root, outerRoot:
|
|
|
|
|
|
|
|
|
|
|
|
m_part_element_map.clear();
|
|
|
|
|
|
|
|
|
|
|
|
// 1. For each descendant el within outerRoot:
|
|
|
|
|
|
for_each_in_subtree_of_type<Element>([this](Element const& element) {
|
|
|
|
|
|
// 1. For each name in el’s part name list, append el to outerRoot’s part element map[name].
|
|
|
|
|
|
for (auto const& name : element.part_names())
|
|
|
|
|
|
m_part_element_map.ensure(name).set({ const_cast<Element&>(element), {} });
|
|
|
|
|
|
|
|
|
|
|
|
// 2. If el is a shadow host itself then let innerRoot be its shadow root.
|
2026-02-12 09:00:25 +00:00
|
|
|
|
if (element.is_shadow_host()) {
|
|
|
|
|
|
auto inner_root = element.shadow_root();
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Calculate innerRoot’s part element map.
|
|
|
|
|
|
auto const& inner_map = inner_root->part_element_map();
|
|
|
|
|
|
|
|
|
|
|
|
// 4. For each innerName/outerName in el’s forwarded part name list:
|
|
|
|
|
|
for (auto const& [inner_name, outer_name] : parse_exportparts_attribute(element)) {
|
|
|
|
|
|
// 1. If innerName is an ident:
|
|
|
|
|
|
if (auto it = inner_map.find(inner_name); it != inner_map.end()) {
|
|
|
|
|
|
// 1. Let innerParts be innerRoot’s part element map[innerName]
|
|
|
|
|
|
// 2. Append the elements in innerParts to outerRoot’s part element map[outerName]
|
|
|
|
|
|
for (auto const& abstract_el : it->value)
|
|
|
|
|
|
m_part_element_map.ensure(outer_name).set(abstract_el);
|
|
|
|
|
|
}
|
|
|
|
|
|
// FIXME: 2. If innerName is a pseudo-element name, append innerRoot’s
|
|
|
|
|
|
// pseudo-element(s) with that name to outerRoot’s part element map[outerName].
|
2025-12-08 15:41:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-12 09:00:25 +00:00
|
|
|
|
|
2025-12-08 15:41:02 +00:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-23 13:02:04 +01:00
|
|
|
|
// https://dom.spec.whatwg.org/#dom-documentorshadowroot-customelementregistry
|
|
|
|
|
|
GC::Ptr<HTML::CustomElementRegistry> ShadowRoot::custom_element_registry() const
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. If this is a document, then return this’s custom element registry.
|
|
|
|
|
|
// NB: Impossible.
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Assert: this is a ShadowRoot node.
|
|
|
|
|
|
// 3. Return this’s custom element registry.
|
|
|
|
|
|
return m_custom_element_registry;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-21 18:32:39 +00:00
|
|
|
|
}
|