TreeNode.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. size_t child_count() const
  52. {
  53. size_t 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. // https://dom.spec.whatwg.org/#concept-tree-index
  73. size_t index() const
  74. {
  75. // The index of an object is its number of preceding siblings, or 0 if it has none.
  76. size_t index = 0;
  77. for (auto* node = previous_sibling(); node; node = node->previous_sibling())
  78. ++index;
  79. return index;
  80. }
  81. Optional<size_t> index_of_child(const T& search_child)
  82. {
  83. VERIFY(search_child.parent() == this);
  84. size_t index = 0;
  85. auto* child = first_child();
  86. VERIFY(child);
  87. do {
  88. if (child == &search_child)
  89. return index;
  90. index++;
  91. } while (child && (child = child->next_sibling()));
  92. return {};
  93. }
  94. template<typename ChildType>
  95. Optional<size_t> index_of_child(const T& search_child)
  96. {
  97. VERIFY(search_child.parent() == this);
  98. size_t index = 0;
  99. auto* child = first_child();
  100. VERIFY(child);
  101. do {
  102. if (!is<ChildType>(child))
  103. continue;
  104. if (child == &search_child)
  105. return index;
  106. index++;
  107. } while (child && (child = child->next_sibling()));
  108. return {};
  109. }
  110. bool is_ancestor_of(const TreeNode&) const;
  111. bool is_inclusive_ancestor_of(const TreeNode&) const;
  112. bool is_descendant_of(const TreeNode&) const;
  113. bool is_inclusive_descendant_of(const TreeNode&) const;
  114. bool is_following(TreeNode const&) const;
  115. void append_child(NonnullRefPtr<T> node);
  116. void prepend_child(NonnullRefPtr<T> node);
  117. void insert_before(NonnullRefPtr<T> node, RefPtr<T> child);
  118. void remove_child(NonnullRefPtr<T> node);
  119. bool is_child_allowed(const T&) const { return true; }
  120. T* next_in_pre_order()
  121. {
  122. if (first_child())
  123. return first_child();
  124. T* node;
  125. if (!(node = next_sibling())) {
  126. node = parent();
  127. while (node && !node->next_sibling())
  128. node = node->parent();
  129. if (node)
  130. node = node->next_sibling();
  131. }
  132. return node;
  133. }
  134. T* next_in_pre_order(T const* stay_within)
  135. {
  136. if (first_child())
  137. return first_child();
  138. T* node = static_cast<T*>(this);
  139. T* next = nullptr;
  140. while (!(next = node->next_sibling())) {
  141. node = node->parent();
  142. if (!node || node == stay_within)
  143. return nullptr;
  144. }
  145. return next;
  146. }
  147. T const* next_in_pre_order() const
  148. {
  149. return const_cast<TreeNode*>(this)->next_in_pre_order();
  150. }
  151. T const* next_in_pre_order(T const* stay_within) const
  152. {
  153. return const_cast<TreeNode*>(this)->next_in_pre_order(stay_within);
  154. }
  155. T* previous_in_pre_order()
  156. {
  157. if (auto* node = previous_sibling()) {
  158. while (node->last_child())
  159. node = node->last_child();
  160. return node;
  161. }
  162. return parent();
  163. }
  164. T const* previous_in_pre_order() const
  165. {
  166. return const_cast<TreeNode*>(this)->previous_in_pre_order();
  167. }
  168. bool is_before(T const& other) const
  169. {
  170. if (this == &other)
  171. return false;
  172. for (auto* node = this; node; node = node->next_in_pre_order()) {
  173. if (node == &other)
  174. return true;
  175. }
  176. return false;
  177. }
  178. // https://dom.spec.whatwg.org/#concept-tree-preceding (Object A is 'typename U' and Object B is 'this')
  179. template<typename U>
  180. bool has_preceding_node_of_type_in_tree_order() const
  181. {
  182. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  183. if (is<U>(node))
  184. return true;
  185. }
  186. return false;
  187. }
  188. // https://dom.spec.whatwg.org/#concept-tree-following (Object A is 'typename U' and Object B is 'this')
  189. template<typename U>
  190. bool has_following_node_of_type_in_tree_order() const
  191. {
  192. for (auto* node = next_in_pre_order(); node; node = node->next_in_pre_order()) {
  193. if (is<U>(node))
  194. return true;
  195. }
  196. return false;
  197. }
  198. template<typename Callback>
  199. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  200. {
  201. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  202. return IterationDecision::Break;
  203. for (auto* child = first_child(); child; child = child->next_sibling()) {
  204. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  205. return IterationDecision::Break;
  206. }
  207. return IterationDecision::Continue;
  208. }
  209. template<typename Callback>
  210. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  211. {
  212. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  213. return IterationDecision::Break;
  214. for (auto* child = first_child(); child; child = child->next_sibling()) {
  215. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  216. return IterationDecision::Break;
  217. }
  218. return IterationDecision::Continue;
  219. }
  220. template<typename U, typename Callback>
  221. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  222. {
  223. if (is<U>(static_cast<const T&>(*this))) {
  224. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  225. return IterationDecision::Break;
  226. }
  227. for (auto* child = first_child(); child; child = child->next_sibling()) {
  228. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  229. return IterationDecision::Break;
  230. }
  231. return IterationDecision::Continue;
  232. }
  233. template<typename U, typename Callback>
  234. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  235. {
  236. if (is<U>(static_cast<const T&>(*this))) {
  237. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  238. return IterationDecision::Break;
  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 Callback>
  247. IterationDecision for_each_in_subtree(Callback callback) const
  248. {
  249. for (auto* child = first_child(); child; child = child->next_sibling()) {
  250. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  251. return IterationDecision::Break;
  252. }
  253. return IterationDecision::Continue;
  254. }
  255. template<typename Callback>
  256. IterationDecision for_each_in_subtree(Callback callback)
  257. {
  258. for (auto* child = first_child(); child; child = child->next_sibling()) {
  259. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  260. return IterationDecision::Break;
  261. }
  262. return IterationDecision::Continue;
  263. }
  264. template<typename U, typename Callback>
  265. IterationDecision for_each_in_subtree_of_type(Callback callback)
  266. {
  267. for (auto* child = first_child(); child; child = child->next_sibling()) {
  268. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  269. return IterationDecision::Break;
  270. }
  271. return IterationDecision::Continue;
  272. }
  273. template<typename U, typename Callback>
  274. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  275. {
  276. for (auto* child = first_child(); child; child = child->next_sibling()) {
  277. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  278. return IterationDecision::Break;
  279. }
  280. return IterationDecision::Continue;
  281. }
  282. template<typename Callback>
  283. void for_each_child(Callback callback) const
  284. {
  285. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  286. }
  287. template<typename Callback>
  288. void for_each_child(Callback callback)
  289. {
  290. for (auto* node = first_child(); node; node = node->next_sibling())
  291. callback(*node);
  292. }
  293. template<typename U, typename Callback>
  294. void for_each_child_of_type(Callback callback)
  295. {
  296. for (auto* node = first_child(); node; node = node->next_sibling()) {
  297. if (is<U>(node))
  298. callback(verify_cast<U>(*node));
  299. }
  300. }
  301. template<typename U, typename Callback>
  302. void for_each_child_of_type(Callback callback) const
  303. {
  304. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  305. }
  306. template<typename U>
  307. const U* next_sibling_of_type() const
  308. {
  309. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  310. }
  311. template<typename U>
  312. inline U* next_sibling_of_type()
  313. {
  314. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  315. if (is<U>(*sibling))
  316. return &verify_cast<U>(*sibling);
  317. }
  318. return nullptr;
  319. }
  320. template<typename U>
  321. const U* previous_sibling_of_type() const
  322. {
  323. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  324. }
  325. template<typename U>
  326. U* previous_sibling_of_type()
  327. {
  328. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  329. if (is<U>(*sibling))
  330. return &verify_cast<U>(*sibling);
  331. }
  332. return nullptr;
  333. }
  334. template<typename U>
  335. const U* first_child_of_type() const
  336. {
  337. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  338. }
  339. template<typename U>
  340. const U* last_child_of_type() const
  341. {
  342. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  343. }
  344. template<typename U>
  345. U* first_child_of_type()
  346. {
  347. for (auto* child = first_child(); child; child = child->next_sibling()) {
  348. if (is<U>(*child))
  349. return &verify_cast<U>(*child);
  350. }
  351. return nullptr;
  352. }
  353. template<typename U>
  354. U* last_child_of_type()
  355. {
  356. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  357. if (is<U>(*child))
  358. return &verify_cast<U>(*child);
  359. }
  360. return nullptr;
  361. }
  362. template<typename U>
  363. bool has_child_of_type() const
  364. {
  365. return first_child_of_type<U>() != nullptr;
  366. }
  367. template<typename U>
  368. const U* first_ancestor_of_type() const
  369. {
  370. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  371. }
  372. template<typename U>
  373. U* first_ancestor_of_type()
  374. {
  375. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  376. if (is<U>(*ancestor))
  377. return &verify_cast<U>(*ancestor);
  378. }
  379. return nullptr;
  380. }
  381. bool is_parent_of(T const& other) const
  382. {
  383. for (auto* child = first_child(); child; child = child->next_sibling()) {
  384. if (&other == child)
  385. return true;
  386. }
  387. return false;
  388. }
  389. ~TreeNode()
  390. {
  391. VERIFY(!m_parent);
  392. T* next_child = nullptr;
  393. for (auto* child = m_first_child; child; child = next_child) {
  394. next_child = child->m_next_sibling;
  395. child->m_parent = nullptr;
  396. child->unref();
  397. }
  398. }
  399. protected:
  400. TreeNode() = default;
  401. bool m_deletion_has_begun { false };
  402. bool m_in_removed_last_ref { false };
  403. private:
  404. int m_ref_count { 1 };
  405. T* m_parent { nullptr };
  406. T* m_first_child { nullptr };
  407. T* m_last_child { nullptr };
  408. T* m_next_sibling { nullptr };
  409. T* m_previous_sibling { nullptr };
  410. };
  411. template<typename T>
  412. inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
  413. {
  414. VERIFY(node->m_parent == this);
  415. if (m_first_child == node)
  416. m_first_child = node->m_next_sibling;
  417. if (m_last_child == node)
  418. m_last_child = node->m_previous_sibling;
  419. if (node->m_next_sibling)
  420. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  421. if (node->m_previous_sibling)
  422. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  423. node->m_next_sibling = nullptr;
  424. node->m_previous_sibling = nullptr;
  425. node->m_parent = nullptr;
  426. node->unref();
  427. }
  428. template<typename T>
  429. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
  430. {
  431. VERIFY(!node->m_parent);
  432. if (!static_cast<T*>(this)->is_child_allowed(*node))
  433. return;
  434. if (m_last_child)
  435. m_last_child->m_next_sibling = node.ptr();
  436. node->m_previous_sibling = m_last_child;
  437. node->m_parent = static_cast<T*>(this);
  438. m_last_child = node.ptr();
  439. if (!m_first_child)
  440. m_first_child = m_last_child;
  441. [[maybe_unused]] auto& rc = node.leak_ref();
  442. }
  443. template<typename T>
  444. inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
  445. {
  446. if (!child)
  447. return append_child(move(node));
  448. VERIFY(!node->m_parent);
  449. VERIFY(child->parent() == this);
  450. node->m_previous_sibling = child->m_previous_sibling;
  451. node->m_next_sibling = child;
  452. if (child->m_previous_sibling)
  453. child->m_previous_sibling->m_next_sibling = node;
  454. if (m_first_child == child)
  455. m_first_child = node;
  456. child->m_previous_sibling = node;
  457. node->m_parent = static_cast<T*>(this);
  458. [[maybe_unused]] auto& rc = node.leak_ref();
  459. }
  460. template<typename T>
  461. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
  462. {
  463. VERIFY(!node->m_parent);
  464. if (!static_cast<T*>(this)->is_child_allowed(*node))
  465. return;
  466. if (m_first_child)
  467. m_first_child->m_previous_sibling = node.ptr();
  468. node->m_next_sibling = m_first_child;
  469. node->m_parent = static_cast<T*>(this);
  470. m_first_child = node.ptr();
  471. if (!m_last_child)
  472. m_last_child = m_first_child;
  473. node->inserted_into(static_cast<T&>(*this));
  474. [[maybe_unused]] auto& rc = node.leak_ref();
  475. static_cast<T*>(this)->children_changed();
  476. }
  477. template<typename T>
  478. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  479. {
  480. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  481. if (ancestor == this)
  482. return true;
  483. }
  484. return false;
  485. }
  486. template<typename T>
  487. inline bool TreeNode<T>::is_inclusive_ancestor_of(const TreeNode<T>& other) const
  488. {
  489. return &other == this || is_ancestor_of(other);
  490. }
  491. template<typename T>
  492. inline bool TreeNode<T>::is_descendant_of(const TreeNode<T>& other) const
  493. {
  494. return other.is_ancestor_of(*this);
  495. }
  496. template<typename T>
  497. inline bool TreeNode<T>::is_inclusive_descendant_of(const TreeNode<T>& other) const
  498. {
  499. return other.is_inclusive_ancestor_of(*this);
  500. }
  501. // https://dom.spec.whatwg.org/#concept-tree-following
  502. template<typename T>
  503. inline bool TreeNode<T>::is_following(TreeNode<T> const& other) const
  504. {
  505. // 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.
  506. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  507. if (node == &other)
  508. return true;
  509. }
  510. return false;
  511. }
  512. }