diff --git a/Libraries/LibWeb/DOM/Range.cpp b/Libraries/LibWeb/DOM/Range.cpp index 7f5ebbb2983..850e0b48763 100644 --- a/Libraries/LibWeb/DOM/Range.cpp +++ b/Libraries/LibWeb/DOM/Range.cpp @@ -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 = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) { - if (is(*node) && contains_node(*node)) + for_each_contained([&](GC::Ref node) { + if (is(*node)) builder.append(static_cast(*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(*end_container())) { diff --git a/Libraries/LibWeb/DOM/Range.h b/Libraries/LibWeb/DOM/Range.h index cd59a5121b5..571405157ad 100644 --- a/Libraries/LibWeb/DOM/Range.h +++ b/Libraries/LibWeb/DOM/Range.h @@ -2,7 +2,7 @@ * Copyright (c) 2020, the SerenityOS developers. * Copyright (c) 2022, Luke Wilde * Copyright (c) 2022-2023, Andreas Kling - * Copyright (c) 2024, Jelle Raaijmakers + * Copyright (c) 2024-2025, Jelle Raaijmakers * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,6 +10,7 @@ #pragma once #include +#include #include #include @@ -98,6 +99,29 @@ public: WebIDL::ExceptionOr> create_contextual_fragment(String const& fragment); + template + void for_each_contained(Callback callback) const + { + return const_cast(this)->for_each_contained(move(callback)); + } + + template + void for_each_contained(Callback callback) + { + GC::Ptr end = m_end_container; + while (end && !end->next_sibling()) + end = end->parent(); + if (end) + end = end->next_sibling(); + + for (GC::Ptr 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 start_container, WebIDL::UnsignedLong start_offset, GC::Ref end_container, WebIDL::UnsignedLong end_offset); diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp index d5d6d3832c1..5720a22f0ed 100644 --- a/Libraries/LibWeb/Editing/Commands.cpp +++ b/Libraries/LibWeb/Editing/Commands.cpp @@ -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 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> contained_nodes; - auto common_ancestor = new_line_range->common_ancestor_container(); - common_ancestor->for_each_in_subtree([&](GC::Ref 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 node) { + contained_nodes.append(node); + return IterationDecision::Continue; }); // 27. Let frag be the result of calling extractContents() on new line range. diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp index 540506996c3..5ebebb41104 100644 --- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp @@ -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 node) { - if (!active_range()->contains_node(node)) - return TraversalDecision::SkipChildrenAndContinue; - + active_range()->for_each_contained([&node_list](GC::Ref 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(*node) && !is(*node) && !is(*node)) node_list.append(node); - return TraversalDecision::Continue; + return IterationDecision::Continue; }); // 25. For each node in node list: diff --git a/Libraries/LibWeb/HTML/Navigable.cpp b/Libraries/LibWeb/HTML/Navigable.cpp index 404ff4ce491..e710e0afab3 100644 --- a/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Libraries/LibWeb/HTML/Navigable.cpp @@ -2186,10 +2186,11 @@ static String visible_text_in_range(DOM::Range const& range) if (is(*range.start_container()) && range.start_container()->layout_node()) builder.append(static_cast(*range.start_container()).data().bytes_as_string_view().substring_view(range.start_offset())); - for (GC::Ptr node = range.start_container(); node != range.end_container()->next_sibling(); node = node->next_in_pre_order()) { - if (is(*node) && range.contains_node(*node) && node->layout_node()) + range.for_each_contained([&](GC::Ref node) { + if (is(*node) && node->layout_node()) builder.append(static_cast(*node).data()); - } + return IterationDecision::Continue; + }); if (is(*range.end_container()) && range.end_container()->layout_node()) builder.append(static_cast(*range.end_container()).data().bytes_as_string_view().substring_view(0, range.end_offset()));