TreeNode.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Assertions.h>
  8. #include <AK/TypeCasts.h>
  9. #include <LibJS/Heap/Cell.h>
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/TraversalDecision.h>
  13. namespace Web {
  14. template<typename T>
  15. class TreeNode {
  16. public:
  17. T* parent() { return m_parent; }
  18. T const* parent() const { return m_parent; }
  19. bool has_children() const { return m_first_child; }
  20. T* next_sibling() { return m_next_sibling; }
  21. T* previous_sibling() { return m_previous_sibling; }
  22. T* first_child() { return m_first_child; }
  23. T* last_child() { return m_last_child; }
  24. T const* next_sibling() const { return m_next_sibling; }
  25. T const* previous_sibling() const { return m_previous_sibling; }
  26. T const* first_child() const { return m_first_child; }
  27. T const* last_child() const { return m_last_child; }
  28. // https://dom.spec.whatwg.org/#concept-tree-index
  29. size_t index() const
  30. {
  31. // The index of an object is its number of preceding siblings, or 0 if it has none.
  32. size_t index = 0;
  33. for (auto* node = previous_sibling(); node; node = node->previous_sibling())
  34. ++index;
  35. return index;
  36. }
  37. template<typename ChildType>
  38. Optional<size_t> index_of_child(T const& search_child)
  39. {
  40. VERIFY(search_child.parent() == this);
  41. size_t index = 0;
  42. auto* child = first_child();
  43. VERIFY(child);
  44. do {
  45. if (!is<ChildType>(child))
  46. continue;
  47. if (child == &search_child)
  48. return index;
  49. index++;
  50. } while (child && (child = child->next_sibling()));
  51. return {};
  52. }
  53. bool is_ancestor_of(TreeNode const&) const;
  54. bool is_inclusive_ancestor_of(TreeNode const&) const;
  55. void append_child(JS::NonnullGCPtr<T> node);
  56. void prepend_child(JS::NonnullGCPtr<T> node);
  57. void insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child);
  58. void remove_child(JS::NonnullGCPtr<T> node);
  59. T* next_in_pre_order()
  60. {
  61. if (first_child())
  62. return first_child();
  63. T* node;
  64. if (!(node = next_sibling())) {
  65. node = parent();
  66. while (node && !node->next_sibling())
  67. node = node->parent();
  68. if (node)
  69. node = node->next_sibling();
  70. }
  71. return node;
  72. }
  73. T* next_in_pre_order(T const* stay_within)
  74. {
  75. if (first_child())
  76. return first_child();
  77. T* node = static_cast<T*>(this);
  78. T* next = nullptr;
  79. while (!(next = node->next_sibling())) {
  80. node = node->parent();
  81. if (!node || node == stay_within)
  82. return nullptr;
  83. }
  84. return next;
  85. }
  86. T const* next_in_pre_order() const
  87. {
  88. return const_cast<TreeNode*>(this)->next_in_pre_order();
  89. }
  90. T const* next_in_pre_order(T const* stay_within) const
  91. {
  92. return const_cast<TreeNode*>(this)->next_in_pre_order(stay_within);
  93. }
  94. T* previous_in_pre_order()
  95. {
  96. if (auto* node = previous_sibling()) {
  97. while (node->last_child())
  98. node = node->last_child();
  99. return node;
  100. }
  101. return parent();
  102. }
  103. T const* previous_in_pre_order() const
  104. {
  105. return const_cast<TreeNode*>(this)->previous_in_pre_order();
  106. }
  107. template<typename Callback>
  108. TraversalDecision for_each_in_inclusive_subtree(Callback callback) const
  109. {
  110. if (auto decision = callback(static_cast<T const&>(*this)); decision != TraversalDecision::Continue)
  111. return decision;
  112. for (auto* child = first_child(); child; child = child->next_sibling()) {
  113. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  114. return TraversalDecision::Break;
  115. }
  116. return TraversalDecision::Continue;
  117. }
  118. template<typename Callback>
  119. TraversalDecision for_each_in_inclusive_subtree(Callback callback)
  120. {
  121. if (auto decision = callback(static_cast<T&>(*this)); decision != TraversalDecision::Continue)
  122. return decision;
  123. for (auto* child = first_child(); child; child = child->next_sibling()) {
  124. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  125. return TraversalDecision::Break;
  126. }
  127. return TraversalDecision::Continue;
  128. }
  129. template<typename U, typename Callback>
  130. TraversalDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  131. {
  132. if (is<U>(static_cast<T const&>(*this))) {
  133. if (auto decision = callback(static_cast<U&>(*this)); decision != TraversalDecision::Continue)
  134. return decision;
  135. }
  136. for (auto* child = first_child(); child; child = child->next_sibling()) {
  137. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
  138. return TraversalDecision::Break;
  139. }
  140. return TraversalDecision::Continue;
  141. }
  142. template<typename U, typename Callback>
  143. TraversalDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  144. {
  145. if (is<U>(static_cast<T const&>(*this))) {
  146. if (auto decision = callback(static_cast<U const&>(*this)); decision != TraversalDecision::Continue)
  147. return decision;
  148. }
  149. for (auto* child = first_child(); child; child = child->next_sibling()) {
  150. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
  151. return TraversalDecision::Break;
  152. }
  153. return TraversalDecision::Continue;
  154. }
  155. template<typename Callback>
  156. TraversalDecision for_each_in_subtree(Callback callback) const
  157. {
  158. for (auto* child = first_child(); child; child = child->next_sibling()) {
  159. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  160. return TraversalDecision::Break;
  161. }
  162. return TraversalDecision::Continue;
  163. }
  164. template<typename Callback>
  165. TraversalDecision for_each_in_subtree(Callback callback)
  166. {
  167. for (auto* child = first_child(); child; child = child->next_sibling()) {
  168. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  169. return TraversalDecision::Break;
  170. }
  171. return TraversalDecision::Continue;
  172. }
  173. template<typename U, typename Callback>
  174. TraversalDecision for_each_in_subtree_of_type(Callback callback)
  175. {
  176. for (auto* child = first_child(); child; child = child->next_sibling()) {
  177. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
  178. return TraversalDecision::Break;
  179. }
  180. return TraversalDecision::Continue;
  181. }
  182. template<typename U, typename Callback>
  183. TraversalDecision for_each_in_subtree_of_type(Callback callback) const
  184. {
  185. for (auto* child = first_child(); child; child = child->next_sibling()) {
  186. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == TraversalDecision::Break)
  187. return TraversalDecision::Break;
  188. }
  189. return TraversalDecision::Continue;
  190. }
  191. template<typename Callback>
  192. void for_each_child(Callback callback) const
  193. {
  194. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  195. }
  196. template<typename Callback>
  197. void for_each_child(Callback callback)
  198. {
  199. for (auto* node = first_child(); node; node = node->next_sibling()) {
  200. if (callback(*node) == IterationDecision::Break)
  201. return;
  202. }
  203. }
  204. template<typename U, typename Callback>
  205. void for_each_child_of_type(Callback callback)
  206. {
  207. for (auto* node = first_child(); node; node = node->next_sibling()) {
  208. if (is<U>(node)) {
  209. if (callback(verify_cast<U>(*node)) == IterationDecision::Break)
  210. return;
  211. }
  212. }
  213. }
  214. template<typename U, typename Callback>
  215. void for_each_child_of_type(Callback callback) const
  216. {
  217. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  218. }
  219. template<typename U>
  220. U const* next_sibling_of_type() const
  221. {
  222. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  223. }
  224. template<typename U>
  225. inline U* next_sibling_of_type()
  226. {
  227. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  228. if (is<U>(*sibling))
  229. return &verify_cast<U>(*sibling);
  230. }
  231. return nullptr;
  232. }
  233. template<typename U>
  234. U const* previous_sibling_of_type() const
  235. {
  236. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  237. }
  238. template<typename U>
  239. U* previous_sibling_of_type()
  240. {
  241. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  242. if (is<U>(*sibling))
  243. return &verify_cast<U>(*sibling);
  244. }
  245. return nullptr;
  246. }
  247. template<typename U>
  248. U const* first_child_of_type() const
  249. {
  250. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  251. }
  252. template<typename U>
  253. U const* last_child_of_type() const
  254. {
  255. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  256. }
  257. template<typename U>
  258. U* first_child_of_type()
  259. {
  260. for (auto* child = first_child(); child; child = child->next_sibling()) {
  261. if (is<U>(*child))
  262. return &verify_cast<U>(*child);
  263. }
  264. return nullptr;
  265. }
  266. template<typename U>
  267. U* last_child_of_type()
  268. {
  269. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  270. if (is<U>(*child))
  271. return &verify_cast<U>(*child);
  272. }
  273. return nullptr;
  274. }
  275. template<typename U>
  276. U const* first_ancestor_of_type() const
  277. {
  278. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  279. }
  280. template<typename U>
  281. U* first_ancestor_of_type()
  282. {
  283. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  284. if (is<U>(*ancestor))
  285. return &verify_cast<U>(*ancestor);
  286. }
  287. return nullptr;
  288. }
  289. ~TreeNode() = default;
  290. protected:
  291. TreeNode() = default;
  292. void visit_edges(JS::Cell::Visitor& visitor)
  293. {
  294. visitor.visit(m_parent);
  295. visitor.visit(m_first_child);
  296. visitor.visit(m_last_child);
  297. visitor.visit(m_next_sibling);
  298. visitor.visit(m_previous_sibling);
  299. }
  300. private:
  301. T* m_parent { nullptr };
  302. T* m_first_child { nullptr };
  303. T* m_last_child { nullptr };
  304. T* m_next_sibling { nullptr };
  305. T* m_previous_sibling { nullptr };
  306. };
  307. template<typename T>
  308. inline void TreeNode<T>::remove_child(JS::NonnullGCPtr<T> node)
  309. {
  310. VERIFY(node->m_parent == this);
  311. if (m_first_child == node)
  312. m_first_child = node->m_next_sibling;
  313. if (m_last_child == node)
  314. m_last_child = node->m_previous_sibling;
  315. if (node->m_next_sibling)
  316. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  317. if (node->m_previous_sibling)
  318. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  319. node->m_next_sibling = nullptr;
  320. node->m_previous_sibling = nullptr;
  321. node->m_parent = nullptr;
  322. }
  323. template<typename T>
  324. inline void TreeNode<T>::append_child(JS::NonnullGCPtr<T> node)
  325. {
  326. VERIFY(!node->m_parent);
  327. if (m_last_child)
  328. m_last_child->m_next_sibling = node.ptr();
  329. node->m_previous_sibling = m_last_child;
  330. node->m_parent = static_cast<T*>(this);
  331. m_last_child = node.ptr();
  332. if (!m_first_child)
  333. m_first_child = m_last_child;
  334. }
  335. template<typename T>
  336. inline void TreeNode<T>::insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child)
  337. {
  338. if (!child)
  339. return append_child(move(node));
  340. VERIFY(!node->m_parent);
  341. VERIFY(child->parent() == this);
  342. node->m_previous_sibling = child->m_previous_sibling;
  343. node->m_next_sibling = child;
  344. if (child->m_previous_sibling)
  345. child->m_previous_sibling->m_next_sibling = node;
  346. if (m_first_child == child)
  347. m_first_child = node;
  348. child->m_previous_sibling = node;
  349. node->m_parent = static_cast<T*>(this);
  350. }
  351. template<typename T>
  352. inline void TreeNode<T>::prepend_child(JS::NonnullGCPtr<T> node)
  353. {
  354. VERIFY(!node->m_parent);
  355. if (m_first_child)
  356. m_first_child->m_previous_sibling = node.ptr();
  357. node->m_next_sibling = m_first_child;
  358. node->m_parent = static_cast<T*>(this);
  359. m_first_child = node.ptr();
  360. if (!m_last_child)
  361. m_last_child = m_first_child;
  362. node->inserted_into(static_cast<T&>(*this));
  363. static_cast<T*>(this)->children_changed();
  364. }
  365. template<typename T>
  366. inline bool TreeNode<T>::is_ancestor_of(TreeNode<T> const& other) const
  367. {
  368. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  369. if (ancestor == this)
  370. return true;
  371. }
  372. return false;
  373. }
  374. template<typename T>
  375. inline bool TreeNode<T>::is_inclusive_ancestor_of(TreeNode<T> const& other) const
  376. {
  377. return &other == this || is_ancestor_of(other);
  378. }
  379. }