Node.h 24 KB

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