TreeNode.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 const* next_in_pre_order() const
  125. {
  126. return const_cast<TreeNode*>(this)->next_in_pre_order();
  127. }
  128. T* previous_in_pre_order()
  129. {
  130. if (auto* node = previous_sibling()) {
  131. while (node->last_child())
  132. node = node->last_child();
  133. return node;
  134. }
  135. return parent();
  136. }
  137. T const* previous_in_pre_order() const
  138. {
  139. return const_cast<TreeNode*>(this)->previous_in_pre_order();
  140. }
  141. bool is_before(T const& other) const
  142. {
  143. if (this == &other)
  144. return false;
  145. for (auto* node = this; node; node = node->next_in_pre_order()) {
  146. if (node == &other)
  147. return true;
  148. }
  149. return false;
  150. }
  151. // https://dom.spec.whatwg.org/#concept-tree-preceding (Object A is 'typename U' and Object B is 'this')
  152. template<typename U>
  153. bool has_preceding_node_of_type_in_tree_order() const
  154. {
  155. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  156. if (is<U>(node))
  157. return true;
  158. }
  159. return false;
  160. }
  161. // https://dom.spec.whatwg.org/#concept-tree-following (Object A is 'typename U' and Object B is 'this')
  162. template<typename U>
  163. bool has_following_node_of_type_in_tree_order() const
  164. {
  165. for (auto* node = next_in_pre_order(); node; node = node->next_in_pre_order()) {
  166. if (is<U>(node))
  167. return true;
  168. }
  169. return false;
  170. }
  171. template<typename Callback>
  172. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  173. {
  174. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  175. return IterationDecision::Break;
  176. for (auto* child = first_child(); child; child = child->next_sibling()) {
  177. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  178. return IterationDecision::Break;
  179. }
  180. return IterationDecision::Continue;
  181. }
  182. template<typename Callback>
  183. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  184. {
  185. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  186. return IterationDecision::Break;
  187. for (auto* child = first_child(); child; child = child->next_sibling()) {
  188. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  189. return IterationDecision::Break;
  190. }
  191. return IterationDecision::Continue;
  192. }
  193. template<typename U, typename Callback>
  194. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  195. {
  196. if (is<U>(static_cast<const T&>(*this))) {
  197. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  198. return IterationDecision::Break;
  199. }
  200. for (auto* child = first_child(); child; child = child->next_sibling()) {
  201. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  202. return IterationDecision::Break;
  203. }
  204. return IterationDecision::Continue;
  205. }
  206. template<typename U, typename Callback>
  207. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  208. {
  209. if (is<U>(static_cast<const T&>(*this))) {
  210. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  211. return IterationDecision::Break;
  212. }
  213. for (auto* child = first_child(); child; child = child->next_sibling()) {
  214. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  215. return IterationDecision::Break;
  216. }
  217. return IterationDecision::Continue;
  218. }
  219. template<typename Callback>
  220. IterationDecision for_each_in_subtree(Callback callback) const
  221. {
  222. for (auto* child = first_child(); child; child = child->next_sibling()) {
  223. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  224. return IterationDecision::Break;
  225. }
  226. return IterationDecision::Continue;
  227. }
  228. template<typename Callback>
  229. IterationDecision for_each_in_subtree(Callback callback)
  230. {
  231. for (auto* child = first_child(); child; child = child->next_sibling()) {
  232. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  233. return IterationDecision::Break;
  234. }
  235. return IterationDecision::Continue;
  236. }
  237. template<typename U, typename Callback>
  238. IterationDecision for_each_in_subtree_of_type(Callback callback)
  239. {
  240. for (auto* child = first_child(); child; child = child->next_sibling()) {
  241. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  242. return IterationDecision::Break;
  243. }
  244. return IterationDecision::Continue;
  245. }
  246. template<typename U, typename Callback>
  247. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  248. {
  249. for (auto* child = first_child(); child; child = child->next_sibling()) {
  250. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  251. return IterationDecision::Break;
  252. }
  253. return IterationDecision::Continue;
  254. }
  255. template<typename Callback>
  256. void for_each_child(Callback callback) const
  257. {
  258. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  259. }
  260. template<typename Callback>
  261. void for_each_child(Callback callback)
  262. {
  263. for (auto* node = first_child(); node; node = node->next_sibling())
  264. callback(*node);
  265. }
  266. template<typename U, typename Callback>
  267. void for_each_child_of_type(Callback callback)
  268. {
  269. for (auto* node = first_child(); node; node = node->next_sibling()) {
  270. if (is<U>(node))
  271. callback(verify_cast<U>(*node));
  272. }
  273. }
  274. template<typename U, typename Callback>
  275. void for_each_child_of_type(Callback callback) const
  276. {
  277. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  278. }
  279. template<typename U>
  280. const U* next_sibling_of_type() const
  281. {
  282. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  283. }
  284. template<typename U>
  285. inline U* next_sibling_of_type()
  286. {
  287. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  288. if (is<U>(*sibling))
  289. return &verify_cast<U>(*sibling);
  290. }
  291. return nullptr;
  292. }
  293. template<typename U>
  294. const U* previous_sibling_of_type() const
  295. {
  296. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  297. }
  298. template<typename U>
  299. U* previous_sibling_of_type()
  300. {
  301. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  302. if (is<U>(*sibling))
  303. return &verify_cast<U>(*sibling);
  304. }
  305. return nullptr;
  306. }
  307. template<typename U>
  308. const U* first_child_of_type() const
  309. {
  310. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  311. }
  312. template<typename U>
  313. const U* last_child_of_type() const
  314. {
  315. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  316. }
  317. template<typename U>
  318. U* first_child_of_type()
  319. {
  320. for (auto* child = first_child(); child; child = child->next_sibling()) {
  321. if (is<U>(*child))
  322. return &verify_cast<U>(*child);
  323. }
  324. return nullptr;
  325. }
  326. template<typename U>
  327. U* last_child_of_type()
  328. {
  329. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  330. if (is<U>(*child))
  331. return &verify_cast<U>(*child);
  332. }
  333. return nullptr;
  334. }
  335. template<typename U>
  336. bool has_child_of_type() const
  337. {
  338. return first_child_of_type<U>() != nullptr;
  339. }
  340. template<typename U>
  341. const U* first_ancestor_of_type() const
  342. {
  343. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  344. }
  345. template<typename U>
  346. U* first_ancestor_of_type()
  347. {
  348. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  349. if (is<U>(*ancestor))
  350. return &verify_cast<U>(*ancestor);
  351. }
  352. return nullptr;
  353. }
  354. bool is_parent_of(T const& other) const
  355. {
  356. for (auto* child = first_child(); child; child = child->next_sibling()) {
  357. if (&other == child)
  358. return true;
  359. }
  360. return false;
  361. }
  362. ~TreeNode()
  363. {
  364. VERIFY(!m_parent);
  365. T* next_child = nullptr;
  366. for (auto* child = m_first_child; child; child = next_child) {
  367. next_child = child->m_next_sibling;
  368. child->m_parent = nullptr;
  369. child->unref();
  370. }
  371. }
  372. protected:
  373. TreeNode() = default;
  374. bool m_deletion_has_begun { false };
  375. bool m_in_removed_last_ref { false };
  376. private:
  377. int m_ref_count { 1 };
  378. T* m_parent { nullptr };
  379. T* m_first_child { nullptr };
  380. T* m_last_child { nullptr };
  381. T* m_next_sibling { nullptr };
  382. T* m_previous_sibling { nullptr };
  383. };
  384. template<typename T>
  385. inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
  386. {
  387. VERIFY(node->m_parent == this);
  388. if (m_first_child == node)
  389. m_first_child = node->m_next_sibling;
  390. if (m_last_child == node)
  391. m_last_child = node->m_previous_sibling;
  392. if (node->m_next_sibling)
  393. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  394. if (node->m_previous_sibling)
  395. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  396. node->m_next_sibling = nullptr;
  397. node->m_previous_sibling = nullptr;
  398. node->m_parent = nullptr;
  399. node->unref();
  400. }
  401. template<typename T>
  402. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
  403. {
  404. VERIFY(!node->m_parent);
  405. if (!static_cast<T*>(this)->is_child_allowed(*node))
  406. return;
  407. if (m_last_child)
  408. m_last_child->m_next_sibling = node.ptr();
  409. node->m_previous_sibling = m_last_child;
  410. node->m_parent = static_cast<T*>(this);
  411. m_last_child = node.ptr();
  412. if (!m_first_child)
  413. m_first_child = m_last_child;
  414. [[maybe_unused]] auto& rc = node.leak_ref();
  415. }
  416. template<typename T>
  417. inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
  418. {
  419. if (!child)
  420. return append_child(move(node));
  421. VERIFY(!node->m_parent);
  422. VERIFY(child->parent() == this);
  423. node->m_previous_sibling = child->m_previous_sibling;
  424. node->m_next_sibling = child;
  425. if (child->m_previous_sibling)
  426. child->m_previous_sibling->m_next_sibling = node;
  427. if (m_first_child == child)
  428. m_first_child = node;
  429. child->m_previous_sibling = node;
  430. node->m_parent = static_cast<T*>(this);
  431. [[maybe_unused]] auto& rc = node.leak_ref();
  432. }
  433. template<typename T>
  434. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
  435. {
  436. VERIFY(!node->m_parent);
  437. if (!static_cast<T*>(this)->is_child_allowed(*node))
  438. return;
  439. if (m_first_child)
  440. m_first_child->m_previous_sibling = node.ptr();
  441. node->m_next_sibling = m_first_child;
  442. node->m_parent = static_cast<T*>(this);
  443. m_first_child = node.ptr();
  444. if (!m_last_child)
  445. m_last_child = m_first_child;
  446. node->inserted_into(static_cast<T&>(*this));
  447. [[maybe_unused]] auto& rc = node.leak_ref();
  448. static_cast<T*>(this)->children_changed();
  449. }
  450. template<typename T>
  451. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  452. {
  453. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  454. if (ancestor == this)
  455. return true;
  456. }
  457. return false;
  458. }
  459. template<typename T>
  460. inline bool TreeNode<T>::is_inclusive_ancestor_of(const TreeNode<T>& other) const
  461. {
  462. return &other == this || is_ancestor_of(other);
  463. }
  464. template<typename T>
  465. inline bool TreeNode<T>::is_descendant_of(const TreeNode<T>& other) const
  466. {
  467. return other.is_ancestor_of(*this);
  468. }
  469. template<typename T>
  470. inline bool TreeNode<T>::is_inclusive_descendant_of(const TreeNode<T>& other) const
  471. {
  472. return other.is_inclusive_ancestor_of(*this);
  473. }
  474. }