TreeNode.h 16 KB

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