2022-03-09 14:34:32 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2022-03-09 14:34:32 +01:00
|
|
|
|
#include <LibWeb/DOM/Node.h>
|
|
|
|
|
#include <LibWeb/DOM/NodeIterator.h>
|
2022-09-24 16:14:37 +01:00
|
|
|
|
#include <LibWeb/WebIDL/AbstractOperations.h>
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
|
|
|
NodeIterator::NodeIterator(Node& root)
|
2023-01-10 06:56:59 -05:00
|
|
|
|
: PlatformObject(root.realm())
|
2022-08-08 22:56:19 +02:00
|
|
|
|
, m_root(root)
|
2022-03-23 00:12:12 +01:00
|
|
|
|
, m_reference({ root })
|
2022-03-09 14:34:32 +01:00
|
|
|
|
{
|
2022-03-09 16:38:44 +01:00
|
|
|
|
root.document().register_node_iterator({}, *this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:46:39 +02:00
|
|
|
|
NodeIterator::~NodeIterator() = default;
|
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
|
JS::ThrowCompletionOr<void> NodeIterator::initialize(JS::Realm& realm)
|
2023-01-10 06:56:59 -05:00
|
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:56:59 -05:00
|
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::NodeIteratorPrototype>(realm, "NodeIterator"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
|
|
return {};
|
2023-01-10 06:56:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:46:39 +02:00
|
|
|
|
void NodeIterator::finalize()
|
2022-03-09 16:38:44 +01:00
|
|
|
|
{
|
2022-10-20 19:46:39 +02:00
|
|
|
|
Base::finalize();
|
2022-03-09 16:38:44 +01:00
|
|
|
|
m_root->document().unregister_node_iterator({}, *this);
|
2022-03-09 14:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 22:56:19 +02:00
|
|
|
|
void NodeIterator::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_filter.ptr());
|
2022-08-28 13:42:07 +02:00
|
|
|
|
visitor.visit(m_root.ptr());
|
|
|
|
|
visitor.visit(m_reference.node.ptr());
|
|
|
|
|
|
|
|
|
|
if (m_traversal_pointer.has_value())
|
|
|
|
|
visitor.visit(m_traversal_pointer->node.ptr());
|
2022-08-08 22:56:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 14:34:32 +01:00
|
|
|
|
// https://dom.spec.whatwg.org/#dom-document-createnodeiterator
|
2022-08-08 22:56:19 +02:00
|
|
|
|
JS::NonnullGCPtr<NodeIterator> NodeIterator::create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter> filter)
|
2022-03-09 14:34:32 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. Let iterator be a new NodeIterator object.
|
|
|
|
|
// 2. Set iterator’s root and iterator’s reference to root.
|
|
|
|
|
// 3. Set iterator’s pointer before reference to true.
|
2022-09-30 17:16:16 -06:00
|
|
|
|
auto& realm = root.realm();
|
2023-01-28 13:39:44 -05:00
|
|
|
|
auto iterator = realm.heap().allocate<NodeIterator>(realm, root).release_allocated_value_but_fixme_should_propagate_errors();
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 4. Set iterator’s whatToShow to whatToShow.
|
|
|
|
|
iterator->m_what_to_show = what_to_show;
|
|
|
|
|
|
|
|
|
|
// 5. Set iterator’s filter to filter.
|
2022-08-08 22:56:19 +02:00
|
|
|
|
iterator->m_filter = filter;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 6. Return iterator.
|
2022-12-14 17:40:33 +00:00
|
|
|
|
return iterator;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#dom-nodeiterator-detach
|
|
|
|
|
void NodeIterator::detach()
|
|
|
|
|
{
|
|
|
|
|
// The detach() method steps are to do nothing.
|
|
|
|
|
// Its functionality (disabling a NodeIterator object) was removed, but the method itself is preserved for compatibility.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-nodeiterator-traverse
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::ThrowCompletionOr<JS::GCPtr<Node>> NodeIterator::traverse(Direction direction)
|
2022-03-09 14:34:32 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. Let node be iterator’s reference.
|
|
|
|
|
// 2. Let beforeNode be iterator’s pointer before reference.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
m_traversal_pointer = m_reference;
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<Node> candidate;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 3. While true:
|
|
|
|
|
while (true) {
|
|
|
|
|
// 4. Branch on direction:
|
|
|
|
|
if (direction == Direction::Next) {
|
|
|
|
|
// next
|
|
|
|
|
// If beforeNode is false, then set node to the first node following node in iterator’s iterator collection.
|
|
|
|
|
// If there is no such node, then return null.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (!m_traversal_pointer->is_before_node) {
|
|
|
|
|
auto* next_node = m_traversal_pointer->node->next_in_pre_order(m_root.ptr());
|
2022-03-09 14:34:32 +01:00
|
|
|
|
if (!next_node)
|
2022-03-22 21:10:59 +01:00
|
|
|
|
return nullptr;
|
2022-12-14 13:43:57 +01:00
|
|
|
|
m_traversal_pointer->node = *next_node;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
} else {
|
|
|
|
|
// If beforeNode is true, then set it to false.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
m_traversal_pointer->is_before_node = false;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// previous
|
|
|
|
|
// If beforeNode is true, then set node to the first node preceding node in iterator’s iterator collection.
|
|
|
|
|
// If there is no such node, then return null.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (m_traversal_pointer->is_before_node) {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
if (m_traversal_pointer->node.ptr() == m_root.ptr())
|
2022-03-09 14:34:32 +01:00
|
|
|
|
return nullptr;
|
2022-03-23 00:12:12 +01:00
|
|
|
|
auto* previous_node = m_traversal_pointer->node->previous_in_pre_order();
|
2022-03-09 14:34:32 +01:00
|
|
|
|
if (!previous_node)
|
2022-03-22 21:10:59 +01:00
|
|
|
|
return nullptr;
|
2022-12-14 13:43:57 +01:00
|
|
|
|
m_traversal_pointer->node = *previous_node;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
} else {
|
|
|
|
|
// If beforeNode is false, then set it to true.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
m_traversal_pointer->is_before_node = true;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 00:12:12 +01:00
|
|
|
|
// NOTE: If the NodeFilter deletes the iterator's current traversal pointer,
|
|
|
|
|
// we will automatically retarget it. However, in that case, we're expected
|
|
|
|
|
// to return the node passed to the filter, not the adjusted traversal pointer's
|
|
|
|
|
// node after the filter returns!
|
|
|
|
|
candidate = m_traversal_pointer->node;
|
|
|
|
|
|
2022-03-09 14:34:32 +01:00
|
|
|
|
// 2. Let result be the result of filtering node within iterator.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
auto result = TRY(filter(*m_traversal_pointer->node));
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 3. If result is FILTER_ACCEPT, then break.
|
2023-01-27 13:22:36 +00:00
|
|
|
|
if (result == NodeFilter::Result::FILTER_ACCEPT)
|
2022-03-09 14:34:32 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Set iterator’s reference to node.
|
|
|
|
|
// 5. Set iterator’s pointer before reference to beforeNode.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
m_reference = m_traversal_pointer.release_value();
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 6. Return node.
|
2022-03-23 00:12:12 +01:00
|
|
|
|
return candidate;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-node-filter
|
|
|
|
|
JS::ThrowCompletionOr<NodeFilter::Result> NodeIterator::filter(Node& node)
|
|
|
|
|
{
|
|
|
|
|
// 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException.
|
|
|
|
|
if (m_active)
|
2022-09-25 16:15:49 -06:00
|
|
|
|
return throw_completion(WebIDL::InvalidStateError::create(realm(), "NodeIterator is already active"));
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 2. Let n be node’s nodeType attribute value − 1.
|
|
|
|
|
auto n = node.node_type() - 1;
|
|
|
|
|
|
|
|
|
|
// 3. If the nth bit (where 0 is the least significant bit) of traverser’s whatToShow is not set, then return FILTER_SKIP.
|
|
|
|
|
if (!(m_what_to_show & (1u << n)))
|
2023-01-27 13:22:36 +00:00
|
|
|
|
return NodeFilter::Result::FILTER_SKIP;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 4. If traverser’s filter is null, then return FILTER_ACCEPT.
|
2022-08-08 22:56:19 +02:00
|
|
|
|
if (!m_filter)
|
2023-01-27 13:22:36 +00:00
|
|
|
|
return NodeFilter::Result::FILTER_ACCEPT;
|
2022-03-09 14:34:32 +01:00
|
|
|
|
|
|
|
|
|
// 5. Set traverser’s active flag.
|
|
|
|
|
m_active = true;
|
|
|
|
|
|
|
|
|
|
// 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ».
|
|
|
|
|
// If this throws an exception, then unset traverser’s active flag and rethrow the exception.
|
2022-09-24 16:14:37 +01:00
|
|
|
|
auto result = WebIDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, &node);
|
2022-03-09 14:34:32 +01:00
|
|
|
|
if (result.is_abrupt()) {
|
|
|
|
|
m_active = false;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7. Unset traverser’s active flag.
|
|
|
|
|
m_active = false;
|
|
|
|
|
|
|
|
|
|
// 8. Return result.
|
2022-08-08 22:56:19 +02:00
|
|
|
|
auto result_value = TRY(result.value()->to_i32(vm()));
|
2022-03-09 14:34:32 +01:00
|
|
|
|
return static_cast<NodeFilter::Result>(result_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#dom-nodeiterator-nextnode
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::ThrowCompletionOr<JS::GCPtr<Node>> NodeIterator::next_node()
|
2022-03-09 14:34:32 +01:00
|
|
|
|
{
|
|
|
|
|
return traverse(Direction::Next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#dom-nodeiterator-previousnode
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::ThrowCompletionOr<JS::GCPtr<Node>> NodeIterator::previous_node()
|
2022-03-09 14:34:32 +01:00
|
|
|
|
{
|
|
|
|
|
return traverse(Direction::Previous);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 00:12:12 +01:00
|
|
|
|
void NodeIterator::run_pre_removing_steps_with_node_pointer(Node& to_be_removed_node, NodePointer& pointer)
|
2022-03-09 16:38:44 +01:00
|
|
|
|
{
|
2022-03-23 00:12:12 +01:00
|
|
|
|
// NOTE: This function tries to match the behavior of other engines, but not the DOM specification
|
|
|
|
|
// as it's a known issue that the spec doesn't match how major browsers behave.
|
|
|
|
|
// Spec bug: https://github.com/whatwg/dom/issues/907
|
2022-03-09 16:38:44 +01:00
|
|
|
|
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (!to_be_removed_node.is_descendant_of(root()))
|
2022-03-09 16:38:44 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (!to_be_removed_node.is_inclusive_ancestor_of(pointer.node))
|
|
|
|
|
return;
|
2022-03-09 16:38:44 +01:00
|
|
|
|
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (pointer.is_before_node) {
|
|
|
|
|
if (auto* node = to_be_removed_node.next_in_pre_order(root())) {
|
|
|
|
|
while (node && node->is_descendant_of(to_be_removed_node))
|
|
|
|
|
node = node->next_in_pre_order(root());
|
|
|
|
|
if (node)
|
2022-12-14 13:43:57 +01:00
|
|
|
|
pointer.node = *node;
|
2022-03-09 16:38:44 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (auto* node = to_be_removed_node.previous_in_pre_order()) {
|
|
|
|
|
if (to_be_removed_node.is_ancestor_of(pointer.node)) {
|
|
|
|
|
while (node && node->is_descendant_of(to_be_removed_node))
|
|
|
|
|
node = node->previous_in_pre_order();
|
|
|
|
|
}
|
|
|
|
|
if (node) {
|
|
|
|
|
pointer = {
|
|
|
|
|
.node = *node,
|
|
|
|
|
.is_before_node = false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
2022-03-09 16:38:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (auto* node = to_be_removed_node.previous_in_pre_order()) {
|
|
|
|
|
if (to_be_removed_node.is_ancestor_of(pointer.node)) {
|
|
|
|
|
while (node && node->is_descendant_of(to_be_removed_node))
|
|
|
|
|
node = node->previous_in_pre_order();
|
|
|
|
|
}
|
|
|
|
|
if (node)
|
2022-12-14 13:43:57 +01:00
|
|
|
|
pointer.node = *node;
|
2022-03-23 00:12:12 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto* node = to_be_removed_node.next_in_pre_order(root());
|
|
|
|
|
if (to_be_removed_node.is_ancestor_of(pointer.node)) {
|
|
|
|
|
while (node && node->is_descendant_of(to_be_removed_node))
|
|
|
|
|
node = node->previous_in_pre_order();
|
2022-03-09 16:38:44 +01:00
|
|
|
|
}
|
2022-03-23 00:12:12 +01:00
|
|
|
|
if (node)
|
2022-12-14 13:43:57 +01:00
|
|
|
|
pointer.node = *node;
|
2022-03-23 00:12:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#nodeiterator-pre-removing-steps
|
|
|
|
|
void NodeIterator::run_pre_removing_steps(Node& to_be_removed_node)
|
|
|
|
|
{
|
|
|
|
|
// NOTE: If we're in the middle of traversal, we have to adjust the traversal pointer in response to node removal.
|
|
|
|
|
if (m_traversal_pointer.has_value())
|
|
|
|
|
run_pre_removing_steps_with_node_pointer(to_be_removed_node, *m_traversal_pointer);
|
|
|
|
|
|
|
|
|
|
run_pre_removing_steps_with_node_pointer(to_be_removed_node, m_reference);
|
2022-03-09 16:38:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 14:34:32 +01:00
|
|
|
|
}
|