Node.h 25 KB

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