Node.h 26 KB

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