2020-08-01 03:07:00 +01:00
|
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2023-09-05 14:49:30 -04:00
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:07:00 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/HTMLSlotElementPrototype.h>
|
2022-09-30 17:16:16 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2023-09-05 15:05:46 -04:00
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
|
|
|
|
#include <LibWeb/DOM/Text.h>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLSlotElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(HTMLSlotElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
|
HTMLSlotElement::HTMLSlotElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:07:00 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
HTMLSlotElement::~HTMLSlotElement() = default;
|
2020-08-01 03:07:00 +01:00
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void HTMLSlotElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLSlotElement);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 14:49:30 -04:00
|
|
|
|
void HTMLSlotElement::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
Slot::visit_edges(visitor);
|
2023-09-05 15:05:46 -04:00
|
|
|
|
|
|
|
|
|
for (auto const& node : m_manually_assigned_nodes)
|
|
|
|
|
node.visit([&](auto const& slottable) { visitor.visit(slottable); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignednodes
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Root<DOM::Node>> HTMLSlotElement::assigned_nodes(AssignedNodesOptions options) const
|
2023-09-05 15:05:46 -04:00
|
|
|
|
{
|
2023-09-05 15:10:31 -04:00
|
|
|
|
// 1. If options["flatten"] is false, then return this's assigned nodes.
|
|
|
|
|
if (!options.flatten) {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Root<DOM::Node>> assigned_nodes;
|
2023-09-05 15:10:31 -04:00
|
|
|
|
assigned_nodes.ensure_capacity(assigned_nodes_internal().size());
|
|
|
|
|
|
|
|
|
|
for (auto const& node : assigned_nodes_internal()) {
|
|
|
|
|
node.visit([&](auto const& node) {
|
|
|
|
|
assigned_nodes.unchecked_append(*node);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return assigned_nodes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 15:05:46 -04:00
|
|
|
|
// FIXME: 2. Return the result of finding flattened slottables with this.
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignedelements
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Root<DOM::Element>> HTMLSlotElement::assigned_elements(AssignedNodesOptions options) const
|
2023-09-05 15:05:46 -04:00
|
|
|
|
{
|
2023-09-05 15:10:31 -04:00
|
|
|
|
// 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
|
|
|
|
|
if (!options.flatten) {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Root<DOM::Element>> assigned_nodes;
|
2023-09-05 15:10:31 -04:00
|
|
|
|
|
|
|
|
|
for (auto const& node : assigned_nodes_internal()) {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
if (auto const* element = node.get_pointer<GC::Ref<DOM::Element>>())
|
2023-09-05 15:10:31 -04:00
|
|
|
|
assigned_nodes.append(*element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return assigned_nodes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 15:05:46 -04:00
|
|
|
|
// FIXME: 2. Return the result of finding flattened slottables with this, filtered to contain only Element nodes.
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assign
|
|
|
|
|
void HTMLSlotElement::assign(Vector<SlottableHandle> nodes)
|
|
|
|
|
{
|
|
|
|
|
// 1. For each node of this's manually assigned nodes, set node's manual slot assignment to null.
|
|
|
|
|
for (auto& node : m_manually_assigned_nodes) {
|
|
|
|
|
node.visit([&](auto& node) {
|
|
|
|
|
node->set_manual_slot_assignment(nullptr);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Let nodesSet be a new ordered set.
|
|
|
|
|
Vector<DOM::Slottable> nodes_set;
|
|
|
|
|
|
|
|
|
|
// 3. For each node of nodes:
|
|
|
|
|
for (auto& node_handle : nodes) {
|
|
|
|
|
auto& node = node_handle.visit([](auto& node) -> DOM::SlottableMixin& { return *node; });
|
|
|
|
|
auto slottable = node_handle.visit([](auto& node) { return node->as_slottable(); });
|
|
|
|
|
|
|
|
|
|
// 1. If node's manual slot assignment refers to a slot, then remove node from that slot's manually assigned nodes.
|
|
|
|
|
if (node.manual_slot_assignment() != nullptr) {
|
|
|
|
|
m_manually_assigned_nodes.remove_all_matching([&](auto const& manually_assigned_node) {
|
|
|
|
|
return slottable == manually_assigned_node;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Set node's manual slot assignment to this.
|
|
|
|
|
node.set_manual_slot_assignment(this);
|
|
|
|
|
|
|
|
|
|
// 3. Append node to nodesSet.
|
|
|
|
|
nodes_set.append(slottable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Set this's manually assigned nodes to nodesSet.
|
|
|
|
|
m_manually_assigned_nodes = move(nodes_set);
|
|
|
|
|
|
|
|
|
|
// 5. Run assign slottables for a tree for this's root.
|
|
|
|
|
assign_slottables_for_a_tree(root());
|
2023-09-05 14:49:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 21:28:18 +01:00
|
|
|
|
// https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-change-ext
|
2024-11-14 08:14:16 -05:00
|
|
|
|
void HTMLSlotElement::attribute_changed(FlyString const& local_name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2023-11-19 21:28:18 +01:00
|
|
|
|
{
|
2024-11-14 08:14:16 -05:00
|
|
|
|
Base::attribute_changed(local_name, old_value, value, namespace_);
|
2023-11-19 21:28:18 +01:00
|
|
|
|
|
|
|
|
|
// 1. If element is a slot, localName is name, and namespace is null, then:
|
|
|
|
|
if (local_name == AttributeNames::name && !namespace_.has_value()) {
|
|
|
|
|
// 1. If value is oldValue, then return.
|
|
|
|
|
if (value == old_value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 2. If value is null and oldValue is the empty string, then return.
|
|
|
|
|
if (!value.has_value() && old_value == String {})
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 3. If value is the empty string and oldValue is null, then return.
|
|
|
|
|
if (value == String {} && !old_value.has_value())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 4. If value is null or the empty string, then set element’s name to the empty string.
|
|
|
|
|
if (!value.has_value())
|
|
|
|
|
set_slot_name({});
|
|
|
|
|
// 5. Otherwise, set element’s name to value.
|
|
|
|
|
else
|
|
|
|
|
set_slot_name(*value);
|
|
|
|
|
|
|
|
|
|
// 6. Run assign slottables for a tree with element’s root.
|
|
|
|
|
DOM::assign_slottables_for_a_tree(root());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 03:07:00 +01:00
|
|
|
|
}
|