Node.h 28 KB

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