TreeNode.h 16 KB

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