Node.h 28 KB

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