TreeNode.h 17 KB

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