mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-18 08:20:44 +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
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue