Node.h 25 KB

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