Node.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/Badge.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/FlyString.h>
  10. #include <AK/JsonObjectSerializer.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/TypeCasts.h>
  13. #include <AK/Vector.h>
  14. #include <LibWeb/DOM/AccessibilityTreeNode.h>
  15. #include <LibWeb/DOM/EventTarget.h>
  16. #include <LibWeb/DOMParsing/XMLSerializer.h>
  17. #include <LibWeb/WebIDL/ExceptionOr.h>
  18. namespace Web::DOM {
  19. enum class NodeType : u16 {
  20. INVALID = 0,
  21. ELEMENT_NODE = 1,
  22. ATTRIBUTE_NODE = 2,
  23. TEXT_NODE = 3,
  24. CDATA_SECTION_NODE = 4,
  25. ENTITY_REFERENCE_NODE = 5,
  26. ENTITY_NODE = 6,
  27. PROCESSING_INSTRUCTION_NODE = 7,
  28. COMMENT_NODE = 8,
  29. DOCUMENT_NODE = 9,
  30. DOCUMENT_TYPE_NODE = 10,
  31. DOCUMENT_FRAGMENT_NODE = 11,
  32. NOTATION_NODE = 12
  33. };
  34. enum class NameOrDescription {
  35. Name,
  36. Description
  37. };
  38. struct GetRootNodeOptions {
  39. bool composed { false };
  40. };
  41. class Node : public EventTarget {
  42. WEB_PLATFORM_OBJECT(Node, EventTarget);
  43. public:
  44. ParentNode* parent_or_shadow_host();
  45. ParentNode const* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
  46. Element* parent_or_shadow_host_element();
  47. Element const* parent_or_shadow_host_element() const { return const_cast<Node*>(this)->parent_or_shadow_host_element(); }
  48. virtual ~Node();
  49. NodeType type() const { return m_type; }
  50. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  51. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  52. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  53. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  54. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  55. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  56. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  57. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  58. bool is_slottable() const { return is_element() || is_text(); }
  59. bool is_attribute() const { return type() == NodeType::ATTRIBUTE_NODE; }
  60. bool is_cdata_section() const { return type() == NodeType::CDATA_SECTION_NODE; }
  61. virtual bool is_shadow_root() const { return false; }
  62. virtual bool requires_svg_container() const { return false; }
  63. virtual bool is_svg_container() const { return false; }
  64. virtual bool is_svg_svg_element() const { return false; }
  65. bool in_a_document_tree() const;
  66. // NOTE: This is intended for the JS bindings.
  67. u16 node_type() const { return (u16)m_type; }
  68. virtual bool is_editable() const;
  69. virtual bool is_html_element() const { return false; }
  70. virtual bool is_html_html_element() const { return false; }
  71. virtual bool is_html_anchor_element() const { return false; }
  72. virtual bool is_html_base_element() const { return false; }
  73. virtual bool is_html_body_element() const { return false; }
  74. virtual bool is_html_input_element() const { return false; }
  75. virtual bool is_html_progress_element() const { return false; }
  76. virtual bool is_html_script_element() const { return false; }
  77. virtual bool is_html_template_element() const { return false; }
  78. virtual bool is_navigable_container() const { return false; }
  79. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> pre_insert(JS::NonnullGCPtr<Node>, JS::GCPtr<Node>);
  80. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> pre_remove(JS::NonnullGCPtr<Node>);
  81. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> append_child(JS::NonnullGCPtr<Node>);
  82. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> remove_child(JS::NonnullGCPtr<Node>);
  83. void insert_before(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child, bool suppress_observers = false);
  84. void remove(bool suppress_observers = false);
  85. void remove_all_children(bool suppress_observers = false);
  86. enum DocumentPosition : u16 {
  87. DOCUMENT_POSITION_EQUAL = 0,
  88. DOCUMENT_POSITION_DISCONNECTED = 1,
  89. DOCUMENT_POSITION_PRECEDING = 2,
  90. DOCUMENT_POSITION_FOLLOWING = 4,
  91. DOCUMENT_POSITION_CONTAINS = 8,
  92. DOCUMENT_POSITION_CONTAINED_BY = 16,
  93. DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32,
  94. };
  95. u16 compare_document_position(JS::GCPtr<Node> other);
  96. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> replace_child(JS::NonnullGCPtr<Node> node, JS::NonnullGCPtr<Node> child);
  97. JS::NonnullGCPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
  98. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> clone_node_binding(bool deep);
  99. // NOTE: This is intended for the JS bindings.
  100. bool has_child_nodes() const { return has_children(); }
  101. JS::NonnullGCPtr<NodeList> child_nodes();
  102. Vector<JS::Handle<Node>> children_as_vector() const;
  103. virtual DeprecatedFlyString node_name() const = 0;
  104. DeprecatedString base_uri() const;
  105. DeprecatedString descendant_text_content() const;
  106. DeprecatedString text_content() const;
  107. void set_text_content(DeprecatedString const&);
  108. DeprecatedString node_value() const;
  109. void set_node_value(DeprecatedString const&);
  110. JS::GCPtr<HTML::Navigable> navigable() const;
  111. Document& document() { return *m_document; }
  112. Document const& document() const { return *m_document; }
  113. JS::GCPtr<Document> owner_document() const;
  114. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  115. const HTML::HTMLElement* enclosing_html_element() const;
  116. const HTML::HTMLElement* enclosing_html_element_with_attribute(DeprecatedFlyString const&) const;
  117. DeprecatedString child_text_content() const;
  118. Node& root();
  119. Node const& root() const
  120. {
  121. return const_cast<Node*>(this)->root();
  122. }
  123. Node& shadow_including_root();
  124. Node const& shadow_including_root() const
  125. {
  126. return const_cast<Node*>(this)->shadow_including_root();
  127. }
  128. bool is_connected() const;
  129. Node* parent_node() { return parent(); }
  130. Node const* parent_node() const { return parent(); }
  131. Element* parent_element();
  132. Element const* parent_element() const;
  133. virtual void inserted();
  134. virtual void removed_from(Node*) { }
  135. virtual void children_changed() { }
  136. virtual void adopted_from(Document&) { }
  137. virtual void cloned(Node&, bool) {};
  138. Layout::Node const* layout_node() const { return m_layout_node; }
  139. Layout::Node* layout_node() { return m_layout_node; }
  140. Painting::PaintableBox const* paintable_box() const;
  141. Painting::Paintable const* paintable() const;
  142. void set_layout_node(Badge<Layout::Node>, JS::NonnullGCPtr<Layout::Node>);
  143. void detach_layout_node(Badge<DOM::Document>);
  144. virtual bool is_child_allowed(Node const&) const { return true; }
  145. bool needs_style_update() const { return m_needs_style_update; }
  146. void set_needs_style_update(bool);
  147. bool child_needs_style_update() const { return m_child_needs_style_update; }
  148. void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; }
  149. void invalidate_style();
  150. void set_document(Badge<Document>, Document&);
  151. virtual EventTarget* get_parent(Event const&) override;
  152. template<typename T>
  153. bool fast_is() const = delete;
  154. WebIDL::ExceptionOr<void> ensure_pre_insertion_validity(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child) const;
  155. bool is_host_including_inclusive_ancestor_of(Node const&) const;
  156. bool is_scripting_enabled() const;
  157. bool is_scripting_disabled() const;
  158. bool contains(JS::GCPtr<Node>) const;
  159. // Used for dumping the DOM Tree
  160. void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
  161. bool is_shadow_including_descendant_of(Node const&) const;
  162. bool is_shadow_including_inclusive_descendant_of(Node const&) const;
  163. bool is_shadow_including_ancestor_of(Node const&) const;
  164. bool is_shadow_including_inclusive_ancestor_of(Node const&) const;
  165. i32 id() const { return m_id; }
  166. static Node* from_id(i32 node_id);
  167. WebIDL::ExceptionOr<DeprecatedString> serialize_fragment(DOMParsing::RequireWellFormed) const;
  168. void replace_all(JS::GCPtr<Node>);
  169. void string_replace_all(DeprecatedString const&);
  170. bool is_same_node(Node const*) const;
  171. bool is_equal_node(Node const*) const;
  172. JS::NonnullGCPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
  173. bool is_uninteresting_whitespace_node() const;
  174. DeprecatedString debug_description() const;
  175. size_t length() const;
  176. auto& registered_observers_list() { return m_registered_observer_list; }
  177. auto const& registered_observers_list() const { return m_registered_observer_list; }
  178. void add_registered_observer(RegisteredObserver& registered_observer) { m_registered_observer_list.append(registered_observer); }
  179. void queue_mutation_record(FlyString const& type, DeprecatedString attribute_name, DeprecatedString attribute_namespace, DeprecatedString old_value, JS::NonnullGCPtr<NodeList> added_nodes, JS::NonnullGCPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling) const;
  180. // https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-descendant
  181. template<typename Callback>
  182. IterationDecision for_each_shadow_including_inclusive_descendant(Callback);
  183. // https://dom.spec.whatwg.org/#concept-shadow-including-descendant
  184. template<typename Callback>
  185. IterationDecision for_each_shadow_including_descendant(Callback);
  186. Node* parent() { return m_parent.ptr(); }
  187. Node const* parent() const { return m_parent.ptr(); }
  188. bool has_children() const { return m_first_child; }
  189. Node* next_sibling() { return m_next_sibling.ptr(); }
  190. Node* previous_sibling() { return m_previous_sibling.ptr(); }
  191. Node* first_child() { return m_first_child.ptr(); }
  192. Node* last_child() { return m_last_child.ptr(); }
  193. Node const* next_sibling() const { return m_next_sibling.ptr(); }
  194. Node const* previous_sibling() const { return m_previous_sibling.ptr(); }
  195. Node const* first_child() const { return m_first_child.ptr(); }
  196. Node const* last_child() const { return m_last_child.ptr(); }
  197. size_t child_count() const
  198. {
  199. size_t count = 0;
  200. for (auto* child = first_child(); child; child = child->next_sibling())
  201. ++count;
  202. return count;
  203. }
  204. Node* child_at_index(int index)
  205. {
  206. int count = 0;
  207. for (auto* child = first_child(); child; child = child->next_sibling()) {
  208. if (count == index)
  209. return child;
  210. ++count;
  211. }
  212. return nullptr;
  213. }
  214. Node const* child_at_index(int index) const
  215. {
  216. return const_cast<Node*>(this)->child_at_index(index);
  217. }
  218. // https://dom.spec.whatwg.org/#concept-tree-index
  219. size_t index() const
  220. {
  221. // The index of an object is its number of preceding siblings, or 0 if it has none.
  222. size_t index = 0;
  223. for (auto* node = previous_sibling(); node; node = node->previous_sibling())
  224. ++index;
  225. return index;
  226. }
  227. Optional<size_t> index_of_child(Node const& search_child)
  228. {
  229. VERIFY(search_child.parent() == this);
  230. size_t index = 0;
  231. auto* child = first_child();
  232. VERIFY(child);
  233. do {
  234. if (child == &search_child)
  235. return index;
  236. index++;
  237. } while (child && (child = child->next_sibling()));
  238. return {};
  239. }
  240. template<typename ChildType>
  241. Optional<size_t> index_of_child(Node const& search_child)
  242. {
  243. VERIFY(search_child.parent() == this);
  244. size_t index = 0;
  245. auto* child = first_child();
  246. VERIFY(child);
  247. do {
  248. if (!is<ChildType>(child))
  249. continue;
  250. if (child == &search_child)
  251. return index;
  252. index++;
  253. } while (child && (child = child->next_sibling()));
  254. return {};
  255. }
  256. bool is_ancestor_of(Node const&) const;
  257. bool is_inclusive_ancestor_of(Node const&) const;
  258. bool is_descendant_of(Node const&) const;
  259. bool is_inclusive_descendant_of(Node const&) const;
  260. bool is_following(Node const&) const;
  261. Node* next_in_pre_order()
  262. {
  263. if (first_child())
  264. return first_child();
  265. Node* node;
  266. if (!(node = next_sibling())) {
  267. node = parent();
  268. while (node && !node->next_sibling())
  269. node = node->parent();
  270. if (node)
  271. node = node->next_sibling();
  272. }
  273. return node;
  274. }
  275. Node* next_in_pre_order(Node const* stay_within)
  276. {
  277. if (first_child())
  278. return first_child();
  279. Node* node = static_cast<Node*>(this);
  280. Node* next = nullptr;
  281. while (!(next = node->next_sibling())) {
  282. node = node->parent();
  283. if (!node || node == stay_within)
  284. return nullptr;
  285. }
  286. return next;
  287. }
  288. Node const* next_in_pre_order() const
  289. {
  290. return const_cast<Node*>(this)->next_in_pre_order();
  291. }
  292. Node const* next_in_pre_order(Node const* stay_within) const
  293. {
  294. return const_cast<Node*>(this)->next_in_pre_order(stay_within);
  295. }
  296. Node* previous_in_pre_order()
  297. {
  298. if (auto* node = previous_sibling()) {
  299. while (node->last_child())
  300. node = node->last_child();
  301. return node;
  302. }
  303. return parent();
  304. }
  305. Node const* previous_in_pre_order() const
  306. {
  307. return const_cast<Node*>(this)->previous_in_pre_order();
  308. }
  309. bool is_before(Node const& other) const
  310. {
  311. if (this == &other)
  312. return false;
  313. for (auto* node = this; node; node = node->next_in_pre_order()) {
  314. if (node == &other)
  315. return true;
  316. }
  317. return false;
  318. }
  319. // https://dom.spec.whatwg.org/#concept-tree-preceding (Object A is 'typename U' and Object B is 'this')
  320. template<typename U>
  321. bool has_preceding_node_of_type_in_tree_order() const
  322. {
  323. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  324. if (is<U>(node))
  325. return true;
  326. }
  327. return false;
  328. }
  329. // https://dom.spec.whatwg.org/#concept-tree-following (Object A is 'typename U' and Object B is 'this')
  330. template<typename U>
  331. bool has_following_node_of_type_in_tree_order() const
  332. {
  333. for (auto* node = next_in_pre_order(); node; node = node->next_in_pre_order()) {
  334. if (is<U>(node))
  335. return true;
  336. }
  337. return false;
  338. }
  339. template<typename Callback>
  340. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  341. {
  342. if (callback(static_cast<Node const&>(*this)) == IterationDecision::Break)
  343. return IterationDecision::Break;
  344. for (auto* child = first_child(); child; child = child->next_sibling()) {
  345. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  346. return IterationDecision::Break;
  347. }
  348. return IterationDecision::Continue;
  349. }
  350. template<typename Callback>
  351. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  352. {
  353. if (callback(static_cast<Node&>(*this)) == IterationDecision::Break)
  354. return IterationDecision::Break;
  355. for (auto* child = first_child(); child; child = child->next_sibling()) {
  356. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  357. return IterationDecision::Break;
  358. }
  359. return IterationDecision::Continue;
  360. }
  361. template<typename U, typename Callback>
  362. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  363. {
  364. if (is<U>(static_cast<Node&>(*this))) {
  365. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  366. return IterationDecision::Break;
  367. }
  368. for (auto* child = first_child(); child; child = child->next_sibling()) {
  369. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  370. return IterationDecision::Break;
  371. }
  372. return IterationDecision::Continue;
  373. }
  374. template<typename U, typename Callback>
  375. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  376. {
  377. if (is<U>(static_cast<Node const&>(*this))) {
  378. if (callback(static_cast<U const&>(*this)) == IterationDecision::Break)
  379. return IterationDecision::Break;
  380. }
  381. for (auto* child = first_child(); child; child = child->next_sibling()) {
  382. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  383. return IterationDecision::Break;
  384. }
  385. return IterationDecision::Continue;
  386. }
  387. template<typename Callback>
  388. IterationDecision for_each_in_subtree(Callback callback) const
  389. {
  390. for (auto* child = first_child(); child; child = child->next_sibling()) {
  391. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  392. return IterationDecision::Break;
  393. }
  394. return IterationDecision::Continue;
  395. }
  396. template<typename Callback>
  397. IterationDecision for_each_in_subtree(Callback callback)
  398. {
  399. for (auto* child = first_child(); child; child = child->next_sibling()) {
  400. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  401. return IterationDecision::Break;
  402. }
  403. return IterationDecision::Continue;
  404. }
  405. template<typename U, typename Callback>
  406. IterationDecision for_each_in_subtree_of_type(Callback callback)
  407. {
  408. for (auto* child = first_child(); child; child = child->next_sibling()) {
  409. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  410. return IterationDecision::Break;
  411. }
  412. return IterationDecision::Continue;
  413. }
  414. template<typename U, typename Callback>
  415. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  416. {
  417. for (auto* child = first_child(); child; child = child->next_sibling()) {
  418. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  419. return IterationDecision::Break;
  420. }
  421. return IterationDecision::Continue;
  422. }
  423. template<typename Callback>
  424. void for_each_child(Callback callback) const
  425. {
  426. return const_cast<Node*>(this)->template for_each_child(move(callback));
  427. }
  428. template<typename Callback>
  429. void for_each_child(Callback callback)
  430. {
  431. for (auto* node = first_child(); node; node = node->next_sibling())
  432. callback(*node);
  433. }
  434. template<typename U, typename Callback>
  435. void for_each_child_of_type(Callback callback)
  436. {
  437. for (auto* node = first_child(); node; node = node->next_sibling()) {
  438. if (is<U>(node))
  439. callback(verify_cast<U>(*node));
  440. }
  441. }
  442. template<typename U, typename Callback>
  443. void for_each_child_of_type(Callback callback) const
  444. {
  445. return const_cast<Node*>(this)->template for_each_child_of_type<U>(move(callback));
  446. }
  447. template<typename U>
  448. U const* next_sibling_of_type() const
  449. {
  450. return const_cast<Node*>(this)->template next_sibling_of_type<U>();
  451. }
  452. template<typename U>
  453. inline U* next_sibling_of_type()
  454. {
  455. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  456. if (is<U>(*sibling))
  457. return &verify_cast<U>(*sibling);
  458. }
  459. return nullptr;
  460. }
  461. template<typename U>
  462. U const* previous_sibling_of_type() const
  463. {
  464. return const_cast<Node*>(this)->template previous_sibling_of_type<U>();
  465. }
  466. template<typename U>
  467. U* previous_sibling_of_type()
  468. {
  469. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  470. if (is<U>(*sibling))
  471. return &verify_cast<U>(*sibling);
  472. }
  473. return nullptr;
  474. }
  475. template<typename U>
  476. U const* first_child_of_type() const
  477. {
  478. return const_cast<Node*>(this)->template first_child_of_type<U>();
  479. }
  480. template<typename U>
  481. U const* last_child_of_type() const
  482. {
  483. return const_cast<Node*>(this)->template last_child_of_type<U>();
  484. }
  485. template<typename U>
  486. U* first_child_of_type()
  487. {
  488. for (auto* child = first_child(); child; child = child->next_sibling()) {
  489. if (is<U>(*child))
  490. return &verify_cast<U>(*child);
  491. }
  492. return nullptr;
  493. }
  494. template<typename U>
  495. U* last_child_of_type()
  496. {
  497. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  498. if (is<U>(*child))
  499. return &verify_cast<U>(*child);
  500. }
  501. return nullptr;
  502. }
  503. template<typename U>
  504. bool has_child_of_type() const
  505. {
  506. return first_child_of_type<U>() != nullptr;
  507. }
  508. template<typename U>
  509. U const* first_ancestor_of_type() const
  510. {
  511. return const_cast<Node*>(this)->template first_ancestor_of_type<U>();
  512. }
  513. template<typename U>
  514. U* first_ancestor_of_type()
  515. {
  516. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  517. if (is<U>(*ancestor))
  518. return &verify_cast<U>(*ancestor);
  519. }
  520. return nullptr;
  521. }
  522. bool is_parent_of(Node const& other) const
  523. {
  524. for (auto* child = first_child(); child; child = child->next_sibling()) {
  525. if (&other == child)
  526. return true;
  527. }
  528. return false;
  529. }
  530. ErrorOr<String> accessible_name(Document const&) const;
  531. ErrorOr<String> accessible_description(Document const&) const;
  532. protected:
  533. Node(JS::Realm&, Document&, NodeType);
  534. Node(Document&, NodeType);
  535. virtual void visit_edges(Cell::Visitor&) override;
  536. virtual void finalize() override;
  537. JS::GCPtr<Document> m_document;
  538. JS::GCPtr<Layout::Node> m_layout_node;
  539. NodeType m_type { NodeType::INVALID };
  540. bool m_needs_style_update { false };
  541. bool m_child_needs_style_update { false };
  542. i32 m_id;
  543. // https://dom.spec.whatwg.org/#registered-observer-list
  544. // "Nodes have a strong reference to registered observers in their registered observer list." https://dom.spec.whatwg.org/#garbage-collection
  545. Vector<JS::NonnullGCPtr<RegisteredObserver>> m_registered_observer_list;
  546. void build_accessibility_tree(AccessibilityTreeNode& parent);
  547. ErrorOr<String> name_or_description(NameOrDescription, Document const&, HashTable<i32>&) const;
  548. private:
  549. void queue_tree_mutation_record(JS::NonnullGCPtr<NodeList> added_nodes, JS::NonnullGCPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
  550. void insert_before_impl(JS::NonnullGCPtr<Node>, JS::GCPtr<Node> child);
  551. void append_child_impl(JS::NonnullGCPtr<Node>);
  552. void remove_child_impl(JS::NonnullGCPtr<Node>);
  553. static Optional<StringView> first_valid_id(DeprecatedString const&, Document const&);
  554. static ErrorOr<void> append_without_space(StringBuilder, StringView const&);
  555. static ErrorOr<void> append_with_space(StringBuilder, StringView const&);
  556. static ErrorOr<void> prepend_without_space(StringBuilder, StringView const&);
  557. static ErrorOr<void> prepend_with_space(StringBuilder, StringView const&);
  558. JS::GCPtr<Node> m_parent;
  559. JS::GCPtr<Node> m_first_child;
  560. JS::GCPtr<Node> m_last_child;
  561. JS::GCPtr<Node> m_next_sibling;
  562. JS::GCPtr<Node> m_previous_sibling;
  563. JS::GCPtr<NodeList> m_child_nodes;
  564. };
  565. }