2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
2020-07-26 18:16:18 +03:00
|
|
|
#include <AK/TypeCasts.h>
|
2019-10-19 10:42:20 +03:00
|
|
|
#include <AK/Weakable.h>
|
2020-10-11 22:52:59 +03:00
|
|
|
#include <LibWeb/Forward.h>
|
2019-06-25 20:46:01 +03:00
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
namespace Web {
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
template<typename T>
|
2019-10-19 10:42:20 +03:00
|
|
|
class TreeNode : public Weakable<T> {
|
2019-06-25 20:46:01 +03:00
|
|
|
public:
|
|
|
|
void ref()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_in_removed_last_ref);
|
2021-10-03 17:03:45 +03:00
|
|
|
if constexpr (!IsBaseOf<DOM::Node, T>) {
|
|
|
|
// NOTE: DOM::Document is allowed to survive with 0 ref count, if one of its descendant nodes are alive.
|
|
|
|
VERIFY(m_ref_count);
|
|
|
|
}
|
2019-06-25 20:46:01 +03:00
|
|
|
++m_ref_count;
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:14:21 +03:00
|
|
|
void unref()
|
2019-06-25 20:46:01 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_in_removed_last_ref);
|
|
|
|
VERIFY(m_ref_count);
|
2019-10-09 22:58:38 +03:00
|
|
|
if (!--m_ref_count) {
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsBaseOf<DOM::Node, T>) {
|
2020-10-23 00:38:14 +03:00
|
|
|
m_in_removed_last_ref = true;
|
2020-10-11 22:52:59 +03:00
|
|
|
static_cast<T*>(this)->removed_last_ref();
|
2020-10-23 00:38:14 +03:00
|
|
|
} else {
|
2020-10-11 22:52:59 +03:00
|
|
|
delete static_cast<T*>(this);
|
2020-10-23 00:38:14 +03:00
|
|
|
}
|
2020-10-11 22:52:59 +03:00
|
|
|
return;
|
2019-10-09 22:58:38 +03:00
|
|
|
}
|
2019-06-25 20:46:01 +03:00
|
|
|
}
|
|
|
|
int ref_count() const { return m_ref_count; }
|
|
|
|
|
|
|
|
T* parent() { return m_parent; }
|
|
|
|
const T* parent() const { return m_parent; }
|
|
|
|
|
|
|
|
bool has_children() const { return m_first_child; }
|
|
|
|
T* next_sibling() { return m_next_sibling; }
|
|
|
|
T* previous_sibling() { return m_previous_sibling; }
|
|
|
|
T* first_child() { return m_first_child; }
|
|
|
|
T* last_child() { return m_last_child; }
|
|
|
|
const T* next_sibling() const { return m_next_sibling; }
|
|
|
|
const T* previous_sibling() const { return m_previous_sibling; }
|
|
|
|
const T* first_child() const { return m_first_child; }
|
|
|
|
const T* last_child() const { return m_last_child; }
|
|
|
|
|
2019-11-09 13:31:03 +03:00
|
|
|
int child_count() const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling())
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* child_at_index(int index)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (count == index)
|
|
|
|
return child;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* child_at_index(int index) const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->child_at_index(index);
|
|
|
|
}
|
|
|
|
|
2021-04-18 00:05:56 +03:00
|
|
|
Optional<size_t> index_of_child(const T& search_child)
|
|
|
|
{
|
|
|
|
VERIFY(search_child.parent() == this);
|
|
|
|
size_t index = 0;
|
|
|
|
auto* child = first_child();
|
|
|
|
VERIFY(child);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (child == &search_child)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
} while (child && (child = child->next_sibling()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ChildType>
|
|
|
|
Optional<size_t> index_of_child(const T& search_child)
|
|
|
|
{
|
|
|
|
VERIFY(search_child.parent() == this);
|
|
|
|
size_t index = 0;
|
|
|
|
auto* child = first_child();
|
|
|
|
VERIFY(child);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!is<ChildType>(child))
|
|
|
|
continue;
|
|
|
|
if (child == &search_child)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
} while (child && (child = child->next_sibling()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-10-09 22:33:34 +03:00
|
|
|
bool is_ancestor_of(const TreeNode&) const;
|
2021-04-06 21:34:49 +03:00
|
|
|
bool is_inclusive_ancestor_of(const TreeNode&) const;
|
|
|
|
bool is_descendant_of(const TreeNode&) const;
|
|
|
|
bool is_inclusive_descendant_of(const TreeNode&) const;
|
2019-10-09 22:33:34 +03:00
|
|
|
|
2021-04-06 20:29:12 +03:00
|
|
|
void append_child(NonnullRefPtr<T> node);
|
2020-04-04 00:06:09 +03:00
|
|
|
void prepend_child(NonnullRefPtr<T> node);
|
2021-04-06 20:29:12 +03:00
|
|
|
void insert_before(NonnullRefPtr<T> node, RefPtr<T> child);
|
|
|
|
void remove_child(NonnullRefPtr<T> node);
|
2019-06-25 20:46:01 +03:00
|
|
|
|
2019-10-13 00:26:47 +03:00
|
|
|
bool is_child_allowed(const T&) const { return true; }
|
|
|
|
|
2019-11-05 20:36:06 +03:00
|
|
|
T* next_in_pre_order()
|
|
|
|
{
|
|
|
|
if (first_child())
|
|
|
|
return first_child();
|
|
|
|
T* node;
|
|
|
|
if (!(node = next_sibling())) {
|
|
|
|
node = parent();
|
|
|
|
while (node && !node->next_sibling())
|
|
|
|
node = node->parent();
|
|
|
|
if (node)
|
|
|
|
node = node->next_sibling();
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:15:34 +03:00
|
|
|
T const* next_in_pre_order() const
|
2019-11-05 20:36:06 +03:00
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->next_in_pre_order();
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:15:34 +03:00
|
|
|
T* previous_in_pre_order()
|
|
|
|
{
|
|
|
|
if (auto* node = previous_sibling()) {
|
|
|
|
while (node->last_child())
|
|
|
|
node = node->last_child();
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
T const* previous_in_pre_order() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->previous_in_pre_order();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_before(T const& other) const
|
2020-06-29 01:24:35 +03:00
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return false;
|
|
|
|
for (auto* node = this; node; node = node->next_in_pre_order()) {
|
|
|
|
if (node == &other)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:15:34 +03:00
|
|
|
// https://dom.spec.whatwg.org/#concept-tree-preceding (Object A is 'typename U' and Object B is 'this')
|
|
|
|
template<typename U>
|
|
|
|
bool has_preceding_node_of_type_in_tree_order() const
|
|
|
|
{
|
|
|
|
for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
|
|
|
|
if (is<U>(node))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-tree-following (Object A is 'typename U' and Object B is 'this')
|
|
|
|
template<typename U>
|
|
|
|
bool has_following_node_of_type_in_tree_order() const
|
|
|
|
{
|
|
|
|
for (auto* node = next_in_pre_order(); node; node = node->next_in_pre_order()) {
|
|
|
|
if (is<U>(node))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-19 19:14:54 +03:00
|
|
|
template<typename Callback>
|
2021-04-06 20:38:10 +03:00
|
|
|
IterationDecision for_each_in_inclusive_subtree(Callback callback) const
|
2019-10-19 19:14:54 +03:00
|
|
|
{
|
2019-10-21 13:01:30 +03:00
|
|
|
if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
2019-10-19 19:14:54 +03:00
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2021-04-06 20:38:10 +03:00
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
2019-10-21 13:01:30 +03:00
|
|
|
return IterationDecision::Break;
|
2019-10-19 19:14:54 +03:00
|
|
|
}
|
2019-10-21 13:01:30 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2021-04-06 20:38:10 +03:00
|
|
|
IterationDecision for_each_in_inclusive_subtree(Callback callback)
|
2019-10-21 13:01:30 +03:00
|
|
|
{
|
|
|
|
if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2021-04-06 20:38:10 +03:00
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
2019-10-21 13:01:30 +03:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
2019-10-19 19:14:54 +03:00
|
|
|
}
|
|
|
|
|
2019-12-18 23:34:03 +03:00
|
|
|
template<typename U, typename Callback>
|
2021-04-06 20:38:10 +03:00
|
|
|
IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
|
2019-12-18 23:34:03 +03:00
|
|
|
{
|
|
|
|
if (is<U>(static_cast<const T&>(*this))) {
|
|
|
|
if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2021-04-06 20:38:10 +03:00
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
2019-12-18 23:34:03 +03:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
2021-04-06 20:38:10 +03:00
|
|
|
IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
|
2019-12-18 23:34:03 +03:00
|
|
|
{
|
|
|
|
if (is<U>(static_cast<const T&>(*this))) {
|
|
|
|
if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
2021-04-06 20:38:10 +03:00
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
2019-12-18 23:34:03 +03:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:49:02 +03:00
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:52:49 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback) const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling())
|
|
|
|
callback(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (is<U>(node))
|
2021-06-24 20:53:42 +03:00
|
|
|
callback(verify_cast<U>(*node));
|
2020-08-10 14:52:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
const U* next_sibling_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
inline U* next_sibling_of_type()
|
|
|
|
{
|
|
|
|
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
|
|
|
|
if (is<U>(*sibling))
|
2021-06-24 20:53:42 +03:00
|
|
|
return &verify_cast<U>(*sibling);
|
2020-08-10 14:52:49 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
const U* previous_sibling_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* previous_sibling_of_type()
|
|
|
|
{
|
|
|
|
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
|
|
|
|
if (is<U>(*sibling))
|
2021-06-24 20:53:42 +03:00
|
|
|
return &verify_cast<U>(*sibling);
|
2020-08-10 14:52:49 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
const U* first_child_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
|
|
|
|
}
|
|
|
|
|
2020-11-21 21:49:09 +03:00
|
|
|
template<typename U>
|
|
|
|
const U* last_child_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:52:49 +03:00
|
|
|
template<typename U>
|
|
|
|
U* first_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (is<U>(*child))
|
2021-06-24 20:53:42 +03:00
|
|
|
return &verify_cast<U>(*child);
|
2020-08-10 14:52:49 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-21 21:49:09 +03:00
|
|
|
template<typename U>
|
|
|
|
U* last_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = last_child(); child; child = child->previous_sibling()) {
|
|
|
|
if (is<U>(*child))
|
2021-06-24 20:53:42 +03:00
|
|
|
return &verify_cast<U>(*child);
|
2020-11-21 21:49:09 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:34:49 +03:00
|
|
|
template<typename U>
|
|
|
|
bool has_child_of_type() const
|
|
|
|
{
|
|
|
|
return first_child_of_type<U>() != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:52:49 +03:00
|
|
|
template<typename U>
|
|
|
|
const U* first_ancestor_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* first_ancestor_of_type()
|
|
|
|
{
|
|
|
|
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (is<U>(*ancestor))
|
2021-06-24 20:53:42 +03:00
|
|
|
return &verify_cast<U>(*ancestor);
|
2020-08-10 14:52:49 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-14 01:01:17 +03:00
|
|
|
bool is_parent_of(T const& other) const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (&other == child)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:50:47 +03:00
|
|
|
~TreeNode()
|
|
|
|
{
|
|
|
|
VERIFY(!m_parent);
|
|
|
|
T* next_child = nullptr;
|
|
|
|
for (auto* child = m_first_child; child; child = next_child) {
|
|
|
|
next_child = child->m_next_sibling;
|
|
|
|
child->m_parent = nullptr;
|
|
|
|
child->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
protected:
|
2021-09-16 09:24:47 +03:00
|
|
|
TreeNode() = default;
|
2019-06-25 20:46:01 +03:00
|
|
|
|
2020-10-23 00:38:14 +03:00
|
|
|
bool m_deletion_has_begun { false };
|
|
|
|
bool m_in_removed_last_ref { false };
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
private:
|
|
|
|
int m_ref_count { 1 };
|
|
|
|
T* m_parent { nullptr };
|
|
|
|
T* m_first_child { nullptr };
|
|
|
|
T* m_last_child { nullptr };
|
|
|
|
T* m_next_sibling { nullptr };
|
|
|
|
T* m_previous_sibling { nullptr };
|
|
|
|
};
|
|
|
|
|
2019-11-06 22:25:40 +03:00
|
|
|
template<typename T>
|
2021-04-06 20:29:12 +03:00
|
|
|
inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
|
2019-11-06 22:25:40 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(node->m_parent == this);
|
2019-11-06 22:25:40 +03:00
|
|
|
|
|
|
|
if (m_first_child == node)
|
|
|
|
m_first_child = node->m_next_sibling;
|
|
|
|
|
|
|
|
if (m_last_child == node)
|
|
|
|
m_last_child = node->m_previous_sibling;
|
|
|
|
|
2019-11-08 00:42:55 +03:00
|
|
|
if (node->m_next_sibling)
|
|
|
|
node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
|
|
|
|
|
|
|
|
if (node->m_previous_sibling)
|
|
|
|
node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
|
|
|
|
|
2019-11-06 22:25:40 +03:00
|
|
|
node->m_next_sibling = nullptr;
|
|
|
|
node->m_previous_sibling = nullptr;
|
|
|
|
node->m_parent = nullptr;
|
|
|
|
|
2020-01-23 17:14:21 +03:00
|
|
|
node->unref();
|
2019-11-06 22:25:40 +03:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
template<typename T>
|
2021-04-06 20:29:12 +03:00
|
|
|
inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
|
2019-06-25 20:46:01 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!node->m_parent);
|
2019-10-13 00:26:47 +03:00
|
|
|
|
|
|
|
if (!static_cast<T*>(this)->is_child_allowed(*node))
|
|
|
|
return;
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
if (m_last_child)
|
|
|
|
m_last_child->m_next_sibling = node.ptr();
|
|
|
|
node->m_previous_sibling = m_last_child;
|
|
|
|
node->m_parent = static_cast<T*>(this);
|
2019-09-29 18:40:39 +03:00
|
|
|
m_last_child = node.ptr();
|
2019-06-25 20:46:01 +03:00
|
|
|
if (!m_first_child)
|
|
|
|
m_first_child = m_last_child;
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto& rc = node.leak_ref();
|
2019-06-25 20:46:01 +03:00
|
|
|
}
|
2019-09-25 12:26:26 +03:00
|
|
|
|
2020-06-21 17:45:21 +03:00
|
|
|
template<typename T>
|
2021-04-06 20:29:12 +03:00
|
|
|
inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
|
2020-06-21 17:45:21 +03:00
|
|
|
{
|
|
|
|
if (!child)
|
2021-04-06 20:29:12 +03:00
|
|
|
return append_child(move(node));
|
2020-06-21 17:45:21 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!node->m_parent);
|
|
|
|
VERIFY(child->parent() == this);
|
2020-06-21 17:45:21 +03:00
|
|
|
|
|
|
|
node->m_previous_sibling = child->m_previous_sibling;
|
|
|
|
node->m_next_sibling = child;
|
|
|
|
|
2020-11-28 01:00:53 +03:00
|
|
|
if (child->m_previous_sibling)
|
|
|
|
child->m_previous_sibling->m_next_sibling = node;
|
|
|
|
|
|
|
|
if (m_first_child == child)
|
|
|
|
m_first_child = node;
|
|
|
|
|
2020-11-27 23:06:15 +03:00
|
|
|
child->m_previous_sibling = node;
|
|
|
|
|
2020-06-21 17:45:21 +03:00
|
|
|
node->m_parent = static_cast<T*>(this);
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto& rc = node.leak_ref();
|
2020-06-21 17:45:21 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 21:17:01 +03:00
|
|
|
template<typename T>
|
2020-04-04 00:06:09 +03:00
|
|
|
inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
|
2019-10-09 21:17:01 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!node->m_parent);
|
2019-10-13 00:26:47 +03:00
|
|
|
|
|
|
|
if (!static_cast<T*>(this)->is_child_allowed(*node))
|
|
|
|
return;
|
|
|
|
|
2019-10-09 21:17:01 +03:00
|
|
|
if (m_first_child)
|
|
|
|
m_first_child->m_previous_sibling = node.ptr();
|
|
|
|
node->m_next_sibling = m_first_child;
|
|
|
|
node->m_parent = static_cast<T*>(this);
|
|
|
|
m_first_child = node.ptr();
|
|
|
|
if (!m_last_child)
|
|
|
|
m_last_child = m_first_child;
|
2020-04-04 00:06:09 +03:00
|
|
|
node->inserted_into(static_cast<T&>(*this));
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto& rc = node.leak_ref();
|
2020-04-04 00:06:09 +03:00
|
|
|
|
|
|
|
static_cast<T*>(this)->children_changed();
|
2019-10-09 21:17:01 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 22:33:34 +03:00
|
|
|
template<typename T>
|
|
|
|
inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
|
|
|
|
{
|
|
|
|
for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (ancestor == this)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2021-04-06 21:34:49 +03:00
|
|
|
template<typename T>
|
|
|
|
inline bool TreeNode<T>::is_inclusive_ancestor_of(const TreeNode<T>& other) const
|
|
|
|
{
|
|
|
|
return &other == this || is_ancestor_of(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool TreeNode<T>::is_descendant_of(const TreeNode<T>& other) const
|
|
|
|
{
|
|
|
|
return other.is_ancestor_of(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool TreeNode<T>::is_inclusive_descendant_of(const TreeNode<T>& other) const
|
|
|
|
{
|
|
|
|
return other.is_inclusive_ancestor_of(*this);
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
}
|