TreeNode.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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/GCPtr.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web {
  12. template<typename T>
  13. class TreeNode {
  14. public:
  15. T* parent() { return m_parent; }
  16. T const* parent() const { return m_parent; }
  17. bool has_children() const { return m_first_child; }
  18. T* next_sibling() { return m_next_sibling; }
  19. T* previous_sibling() { return m_previous_sibling; }
  20. T* first_child() { return m_first_child; }
  21. T* last_child() { return m_last_child; }
  22. T const* next_sibling() const { return m_next_sibling; }
  23. T const* previous_sibling() const { return m_previous_sibling; }
  24. T const* first_child() const { return m_first_child; }
  25. T const* last_child() const { return m_last_child; }
  26. size_t child_count() const
  27. {
  28. size_t count = 0;
  29. for (auto* child = first_child(); child; child = child->next_sibling())
  30. ++count;
  31. return count;
  32. }
  33. T* child_at_index(int index)
  34. {
  35. int count = 0;
  36. for (auto* child = first_child(); child; child = child->next_sibling()) {
  37. if (count == index)
  38. return child;
  39. ++count;
  40. }
  41. return nullptr;
  42. }
  43. T const* child_at_index(int index) const
  44. {
  45. return const_cast<TreeNode*>(this)->child_at_index(index);
  46. }
  47. // https://dom.spec.whatwg.org/#concept-tree-index
  48. size_t index() const
  49. {
  50. // The index of an object is its number of preceding siblings, or 0 if it has none.
  51. size_t index = 0;
  52. for (auto* node = previous_sibling(); node; node = node->previous_sibling())
  53. ++index;
  54. return index;
  55. }
  56. Optional<size_t> index_of_child(T const& search_child)
  57. {
  58. VERIFY(search_child.parent() == this);
  59. size_t index = 0;
  60. auto* child = first_child();
  61. VERIFY(child);
  62. do {
  63. if (child == &search_child)
  64. return index;
  65. index++;
  66. } while (child && (child = child->next_sibling()));
  67. return {};
  68. }
  69. template<typename ChildType>
  70. Optional<size_t> index_of_child(T const& search_child)
  71. {
  72. VERIFY(search_child.parent() == this);
  73. size_t index = 0;
  74. auto* child = first_child();
  75. VERIFY(child);
  76. do {
  77. if (!is<ChildType>(child))
  78. continue;
  79. if (child == &search_child)
  80. return index;
  81. index++;
  82. } while (child && (child = child->next_sibling()));
  83. return {};
  84. }
  85. bool is_ancestor_of(TreeNode const&) const;
  86. bool is_inclusive_ancestor_of(TreeNode const&) const;
  87. bool is_descendant_of(TreeNode const&) const;
  88. bool is_inclusive_descendant_of(TreeNode const&) const;
  89. bool is_following(TreeNode const&) const;
  90. void append_child(JS::NonnullGCPtr<T> node);
  91. void prepend_child(JS::NonnullGCPtr<T> node);
  92. void insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child);
  93. void remove_child(JS::NonnullGCPtr<T> node);
  94. bool is_child_allowed(T const&) const { return true; }
  95. T* next_in_pre_order()
  96. {
  97. if (first_child())
  98. return first_child();
  99. T* node;
  100. if (!(node = next_sibling())) {
  101. node = parent();
  102. while (node && !node->next_sibling())
  103. node = node->parent();
  104. if (node)
  105. node = node->next_sibling();
  106. }
  107. return node;
  108. }
  109. T* next_in_pre_order(T const* stay_within)
  110. {
  111. if (first_child())
  112. return first_child();
  113. T* node = static_cast<T*>(this);
  114. T* next = nullptr;
  115. while (!(next = node->next_sibling())) {
  116. node = node->parent();
  117. if (!node || node == stay_within)
  118. return nullptr;
  119. }
  120. return next;
  121. }
  122. T const* next_in_pre_order() const
  123. {
  124. return const_cast<TreeNode*>(this)->next_in_pre_order();
  125. }
  126. T const* next_in_pre_order(T const* stay_within) const
  127. {
  128. return const_cast<TreeNode*>(this)->next_in_pre_order(stay_within);
  129. }
  130. T* previous_in_pre_order()
  131. {
  132. if (auto* node = previous_sibling()) {
  133. while (node->last_child())
  134. node = node->last_child();
  135. return node;
  136. }
  137. return parent();
  138. }
  139. T const* previous_in_pre_order() const
  140. {
  141. return const_cast<TreeNode*>(this)->previous_in_pre_order();
  142. }
  143. bool is_before(T const& other) const
  144. {
  145. if (this == &other)
  146. return false;
  147. for (auto* node = this; node; node = node->next_in_pre_order()) {
  148. if (node == &other)
  149. return true;
  150. }
  151. return false;
  152. }
  153. // https://dom.spec.whatwg.org/#concept-tree-preceding (Object A is 'typename U' and Object B is 'this')
  154. template<typename U>
  155. bool has_preceding_node_of_type_in_tree_order() const
  156. {
  157. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  158. if (is<U>(node))
  159. return true;
  160. }
  161. return false;
  162. }
  163. // https://dom.spec.whatwg.org/#concept-tree-following (Object A is 'typename U' and Object B is 'this')
  164. template<typename U>
  165. bool has_following_node_of_type_in_tree_order() const
  166. {
  167. for (auto* node = next_in_pre_order(); node; node = node->next_in_pre_order()) {
  168. if (is<U>(node))
  169. return true;
  170. }
  171. return false;
  172. }
  173. template<typename Callback>
  174. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  175. {
  176. if (callback(static_cast<T const&>(*this)) == IterationDecision::Break)
  177. return IterationDecision::Break;
  178. for (auto* child = first_child(); child; child = child->next_sibling()) {
  179. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  180. return IterationDecision::Break;
  181. }
  182. return IterationDecision::Continue;
  183. }
  184. template<typename Callback>
  185. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  186. {
  187. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  188. return IterationDecision::Break;
  189. for (auto* child = first_child(); child; child = child->next_sibling()) {
  190. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  191. return IterationDecision::Break;
  192. }
  193. return IterationDecision::Continue;
  194. }
  195. template<typename U, typename Callback>
  196. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  197. {
  198. if (is<U>(static_cast<T const&>(*this))) {
  199. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  200. return IterationDecision::Break;
  201. }
  202. for (auto* child = first_child(); child; child = child->next_sibling()) {
  203. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  204. return IterationDecision::Break;
  205. }
  206. return IterationDecision::Continue;
  207. }
  208. template<typename U, typename Callback>
  209. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  210. {
  211. if (is<U>(static_cast<T const&>(*this))) {
  212. if (callback(static_cast<U const&>(*this)) == IterationDecision::Break)
  213. return IterationDecision::Break;
  214. }
  215. for (auto* child = first_child(); child; child = child->next_sibling()) {
  216. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  217. return IterationDecision::Break;
  218. }
  219. return IterationDecision::Continue;
  220. }
  221. template<typename Callback>
  222. IterationDecision for_each_in_subtree(Callback callback) const
  223. {
  224. for (auto* child = first_child(); child; child = child->next_sibling()) {
  225. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  226. return IterationDecision::Break;
  227. }
  228. return IterationDecision::Continue;
  229. }
  230. template<typename Callback>
  231. IterationDecision for_each_in_subtree(Callback callback)
  232. {
  233. for (auto* child = first_child(); child; child = child->next_sibling()) {
  234. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  235. return IterationDecision::Break;
  236. }
  237. return IterationDecision::Continue;
  238. }
  239. template<typename U, typename Callback>
  240. IterationDecision for_each_in_subtree_of_type(Callback callback)
  241. {
  242. for (auto* child = first_child(); child; child = child->next_sibling()) {
  243. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  244. return IterationDecision::Break;
  245. }
  246. return IterationDecision::Continue;
  247. }
  248. template<typename U, typename Callback>
  249. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  250. {
  251. for (auto* child = first_child(); child; child = child->next_sibling()) {
  252. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  253. return IterationDecision::Break;
  254. }
  255. return IterationDecision::Continue;
  256. }
  257. template<typename Callback>
  258. void for_each_child(Callback callback) const
  259. {
  260. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  261. }
  262. template<typename Callback>
  263. void for_each_child(Callback callback)
  264. {
  265. for (auto* node = first_child(); node; node = node->next_sibling())
  266. callback(*node);
  267. }
  268. template<typename U, typename Callback>
  269. void for_each_child_of_type(Callback callback)
  270. {
  271. for (auto* node = first_child(); node; node = node->next_sibling()) {
  272. if (is<U>(node))
  273. callback(verify_cast<U>(*node));
  274. }
  275. }
  276. template<typename U, typename Callback>
  277. void for_each_child_of_type(Callback callback) const
  278. {
  279. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  280. }
  281. template<typename U>
  282. U const* next_sibling_of_type() const
  283. {
  284. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  285. }
  286. template<typename U>
  287. inline U* next_sibling_of_type()
  288. {
  289. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  290. if (is<U>(*sibling))
  291. return &verify_cast<U>(*sibling);
  292. }
  293. return nullptr;
  294. }
  295. template<typename U>
  296. U const* previous_sibling_of_type() const
  297. {
  298. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  299. }
  300. template<typename U>
  301. U* previous_sibling_of_type()
  302. {
  303. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  304. if (is<U>(*sibling))
  305. return &verify_cast<U>(*sibling);
  306. }
  307. return nullptr;
  308. }
  309. template<typename U>
  310. U const* first_child_of_type() const
  311. {
  312. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  313. }
  314. template<typename U>
  315. U const* last_child_of_type() const
  316. {
  317. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  318. }
  319. template<typename U>
  320. U* first_child_of_type()
  321. {
  322. for (auto* child = first_child(); child; child = child->next_sibling()) {
  323. if (is<U>(*child))
  324. return &verify_cast<U>(*child);
  325. }
  326. return nullptr;
  327. }
  328. template<typename U>
  329. U* last_child_of_type()
  330. {
  331. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  332. if (is<U>(*child))
  333. return &verify_cast<U>(*child);
  334. }
  335. return nullptr;
  336. }
  337. template<typename U>
  338. bool has_child_of_type() const
  339. {
  340. return first_child_of_type<U>() != nullptr;
  341. }
  342. template<typename U>
  343. U const* first_ancestor_of_type() const
  344. {
  345. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  346. }
  347. template<typename U>
  348. U* first_ancestor_of_type()
  349. {
  350. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  351. if (is<U>(*ancestor))
  352. return &verify_cast<U>(*ancestor);
  353. }
  354. return nullptr;
  355. }
  356. bool is_parent_of(T const& other) const
  357. {
  358. for (auto* child = first_child(); child; child = child->next_sibling()) {
  359. if (&other == child)
  360. return true;
  361. }
  362. return false;
  363. }
  364. ~TreeNode() = default;
  365. protected:
  366. TreeNode() = default;
  367. void visit_edges(JS::Cell::Visitor& visitor)
  368. {
  369. visitor.visit(m_parent);
  370. visitor.visit(m_first_child);
  371. visitor.visit(m_last_child);
  372. visitor.visit(m_next_sibling);
  373. visitor.visit(m_previous_sibling);
  374. }
  375. private:
  376. T* m_parent { nullptr };
  377. T* m_first_child { nullptr };
  378. T* m_last_child { nullptr };
  379. T* m_next_sibling { nullptr };
  380. T* m_previous_sibling { nullptr };
  381. };
  382. template<typename T>
  383. inline void TreeNode<T>::remove_child(JS::NonnullGCPtr<T> node)
  384. {
  385. VERIFY(node->m_parent == this);
  386. if (m_first_child == node)
  387. m_first_child = node->m_next_sibling;
  388. if (m_last_child == node)
  389. m_last_child = node->m_previous_sibling;
  390. if (node->m_next_sibling)
  391. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  392. if (node->m_previous_sibling)
  393. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  394. node->m_next_sibling = nullptr;
  395. node->m_previous_sibling = nullptr;
  396. node->m_parent = nullptr;
  397. }
  398. template<typename T>
  399. inline void TreeNode<T>::append_child(JS::NonnullGCPtr<T> node)
  400. {
  401. VERIFY(!node->m_parent);
  402. if (!static_cast<T*>(this)->is_child_allowed(*node))
  403. return;
  404. if (m_last_child)
  405. m_last_child->m_next_sibling = node.ptr();
  406. node->m_previous_sibling = m_last_child;
  407. node->m_parent = static_cast<T*>(this);
  408. m_last_child = node.ptr();
  409. if (!m_first_child)
  410. m_first_child = m_last_child;
  411. }
  412. template<typename T>
  413. inline void TreeNode<T>::insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child)
  414. {
  415. if (!child)
  416. return append_child(move(node));
  417. VERIFY(!node->m_parent);
  418. VERIFY(child->parent() == this);
  419. node->m_previous_sibling = child->m_previous_sibling;
  420. node->m_next_sibling = child;
  421. if (child->m_previous_sibling)
  422. child->m_previous_sibling->m_next_sibling = node;
  423. if (m_first_child == child)
  424. m_first_child = node;
  425. child->m_previous_sibling = node;
  426. node->m_parent = static_cast<T*>(this);
  427. }
  428. template<typename T>
  429. inline void TreeNode<T>::prepend_child(JS::NonnullGCPtr<T> node)
  430. {
  431. VERIFY(!node->m_parent);
  432. if (!static_cast<T*>(this)->is_child_allowed(*node))
  433. return;
  434. if (m_first_child)
  435. m_first_child->m_previous_sibling = node.ptr();
  436. node->m_next_sibling = m_first_child;
  437. node->m_parent = static_cast<T*>(this);
  438. m_first_child = node.ptr();
  439. if (!m_last_child)
  440. m_last_child = m_first_child;
  441. node->inserted_into(static_cast<T&>(*this));
  442. static_cast<T*>(this)->children_changed();
  443. }
  444. template<typename T>
  445. inline bool TreeNode<T>::is_ancestor_of(TreeNode<T> const& other) const
  446. {
  447. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  448. if (ancestor == this)
  449. return true;
  450. }
  451. return false;
  452. }
  453. template<typename T>
  454. inline bool TreeNode<T>::is_inclusive_ancestor_of(TreeNode<T> const& other) const
  455. {
  456. return &other == this || is_ancestor_of(other);
  457. }
  458. template<typename T>
  459. inline bool TreeNode<T>::is_descendant_of(TreeNode<T> const& other) const
  460. {
  461. return other.is_ancestor_of(*this);
  462. }
  463. template<typename T>
  464. inline bool TreeNode<T>::is_inclusive_descendant_of(TreeNode<T> const& other) const
  465. {
  466. return other.is_inclusive_ancestor_of(*this);
  467. }
  468. // https://dom.spec.whatwg.org/#concept-tree-following
  469. template<typename T>
  470. inline bool TreeNode<T>::is_following(TreeNode<T> const& other) const
  471. {
  472. // An object A is following an object B if A and B are in the same tree and A comes after B in tree order.
  473. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  474. if (node == &other)
  475. return true;
  476. }
  477. return false;
  478. }
  479. }