mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 12:49:19 +00:00
LibWeb: Add Range::for_each_contained()
This centralizes the logic for iterating over a Range's contained nodes.
This commit is contained in:
parent
2466021e55
commit
4323669939
Notes:
github-actions[bot]
2025-01-10 22:39:13 +00:00
Author: https://github.com/gmta
Commit: 4323669939
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3216
5 changed files with 44 additions and 26 deletions
|
@ -578,10 +578,11 @@ String Range::to_string() const
|
|||
}
|
||||
|
||||
// 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s.
|
||||
for (GC::Ptr<Node> node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) {
|
||||
if (is<Text>(*node) && contains_node(*node))
|
||||
for_each_contained([&](GC::Ref<DOM::Node> node) {
|
||||
if (is<Text>(*node))
|
||||
builder.append(static_cast<Text const&>(*node).data());
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
// 5. If this’s end node is a Text node, then append the substring of that node’s data from its start until this’s end offset to s.
|
||||
if (is<Text>(*end_container())) {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
||||
* Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
|
||||
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
|
||||
* Copyright (c) 2024-2025, Jelle Raaijmakers <jelle@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -10,6 +10,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibWeb/DOM/AbstractRange.h>
|
||||
#include <LibWeb/DOM/Node.h>
|
||||
#include <LibWeb/Selection/Selection.h>
|
||||
#include <LibWeb/WebIDL/Types.h>
|
||||
|
||||
|
@ -98,6 +99,29 @@ public:
|
|||
|
||||
WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> create_contextual_fragment(String const& fragment);
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_contained(Callback callback) const
|
||||
{
|
||||
return const_cast<Range*>(this)->for_each_contained(move(callback));
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_contained(Callback callback)
|
||||
{
|
||||
GC::Ptr<Node> end = m_end_container;
|
||||
while (end && !end->next_sibling())
|
||||
end = end->parent();
|
||||
if (end)
|
||||
end = end->next_sibling();
|
||||
|
||||
for (GC::Ptr<Node> node = m_start_container; node && node != end; node = node->next_in_pre_order()) {
|
||||
if (contains_node(*node)) {
|
||||
if (callback(*node) == IterationDecision::Break)
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
explicit Range(Document&);
|
||||
Range(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset);
|
||||
|
|
|
@ -551,12 +551,12 @@ bool command_insert_paragraph_action(DOM::Document& document, String const&)
|
|||
|
||||
// 4. Append to node list the first node in tree order that is contained in new range and is an allowed child of
|
||||
// "p", if any.
|
||||
new_range->start_container()->for_each_in_inclusive_subtree([&](DOM::Node& node) {
|
||||
if (is_allowed_child_of_node(GC::Ref { node }, HTML::TagNames::p) && new_range->contains_node(node)) {
|
||||
new_range->for_each_contained([&node_list](GC::Ref<DOM::Node> node) {
|
||||
if (is_allowed_child_of_node(node, HTML::TagNames::p)) {
|
||||
node_list.append(node);
|
||||
return TraversalDecision::Break;
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
return TraversalDecision::Continue;
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
// 5. If node list is empty:
|
||||
|
@ -725,14 +725,10 @@ bool command_insert_paragraph_action(DOM::Document& document, String const&)
|
|||
container->parent()->insert_before(*new_container, container->next_sibling());
|
||||
|
||||
// 26. Let contained nodes be all nodes contained in new line range.
|
||||
// FIXME: this is probably wildly inefficient
|
||||
Vector<GC::Ref<DOM::Node>> contained_nodes;
|
||||
auto common_ancestor = new_line_range->common_ancestor_container();
|
||||
common_ancestor->for_each_in_subtree([&](GC::Ref<DOM::Node> child_node) {
|
||||
if (!new_line_range->contains_node(child_node))
|
||||
return TraversalDecision::SkipChildrenAndContinue;
|
||||
contained_nodes.append(child_node);
|
||||
return TraversalDecision::Continue;
|
||||
new_line_range->for_each_contained([&contained_nodes](GC::Ref<DOM::Node> node) {
|
||||
contained_nodes.append(node);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
// 27. Let frag be the result of calling extractContents() on new line range.
|
||||
|
|
|
@ -590,21 +590,17 @@ void delete_the_selection(Selection& selection, bool block_merging, bool strip_w
|
|||
|
||||
// 24. For each node contained in the active range, append node to node list if the last member of node list (if
|
||||
// any) is not an ancestor of node; node is editable; and node is not a thead, tbody, tfoot, tr, th, or td.
|
||||
auto common_ancestor = active_range()->common_ancestor_container();
|
||||
common_ancestor->for_each_in_subtree([&](GC::Ref<DOM::Node> node) {
|
||||
if (!active_range()->contains_node(node))
|
||||
return TraversalDecision::SkipChildrenAndContinue;
|
||||
|
||||
active_range()->for_each_contained([&node_list](GC::Ref<DOM::Node> node) {
|
||||
if (!node_list.is_empty() && node_list.last()->is_ancestor_of(node))
|
||||
return TraversalDecision::SkipChildrenAndContinue;
|
||||
return IterationDecision::Continue;
|
||||
|
||||
if (!node->is_editable())
|
||||
return TraversalDecision::Continue;
|
||||
return IterationDecision::Continue;
|
||||
|
||||
if (!is<HTML::HTMLTableSectionElement>(*node) && !is<HTML::HTMLTableRowElement>(*node) && !is<HTML::HTMLTableCellElement>(*node))
|
||||
node_list.append(node);
|
||||
|
||||
return TraversalDecision::Continue;
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
// 25. For each node in node list:
|
||||
|
|
|
@ -2186,10 +2186,11 @@ static String visible_text_in_range(DOM::Range const& range)
|
|||
if (is<DOM::Text>(*range.start_container()) && range.start_container()->layout_node())
|
||||
builder.append(static_cast<DOM::Text const&>(*range.start_container()).data().bytes_as_string_view().substring_view(range.start_offset()));
|
||||
|
||||
for (GC::Ptr<DOM::Node> node = range.start_container(); node != range.end_container()->next_sibling(); node = node->next_in_pre_order()) {
|
||||
if (is<DOM::Text>(*node) && range.contains_node(*node) && node->layout_node())
|
||||
range.for_each_contained([&](GC::Ref<DOM::Node> node) {
|
||||
if (is<DOM::Text>(*node) && node->layout_node())
|
||||
builder.append(static_cast<DOM::Text const&>(*node).data());
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
if (is<DOM::Text>(*range.end_container()) && range.end_container()->layout_node())
|
||||
builder.append(static_cast<DOM::Text const&>(*range.end_container()).data().bytes_as_string_view().substring_view(0, range.end_offset()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue