Node.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/HashTable.h>
  9. #include <AK/IDAllocator.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibJS/Heap/DeferGC.h>
  12. #include <LibJS/Runtime/FunctionObject.h>
  13. #include <LibRegex/Regex.h>
  14. #include <LibWeb/Bindings/MainThreadVM.h>
  15. #include <LibWeb/Bindings/NodePrototype.h>
  16. #include <LibWeb/DOM/Comment.h>
  17. #include <LibWeb/DOM/DocumentType.h>
  18. #include <LibWeb/DOM/Element.h>
  19. #include <LibWeb/DOM/ElementFactory.h>
  20. #include <LibWeb/DOM/Event.h>
  21. #include <LibWeb/DOM/EventDispatcher.h>
  22. #include <LibWeb/DOM/IDLEventListener.h>
  23. #include <LibWeb/DOM/LiveNodeList.h>
  24. #include <LibWeb/DOM/MutationType.h>
  25. #include <LibWeb/DOM/Node.h>
  26. #include <LibWeb/DOM/NodeIterator.h>
  27. #include <LibWeb/DOM/ProcessingInstruction.h>
  28. #include <LibWeb/DOM/Range.h>
  29. #include <LibWeb/DOM/ShadowRoot.h>
  30. #include <LibWeb/DOM/StaticNodeList.h>
  31. #include <LibWeb/HTML/BrowsingContextContainer.h>
  32. #include <LibWeb/HTML/HTMLAnchorElement.h>
  33. #include <LibWeb/HTML/HTMLStyleElement.h>
  34. #include <LibWeb/HTML/Origin.h>
  35. #include <LibWeb/HTML/Parser/HTMLParser.h>
  36. #include <LibWeb/Infra/CharacterTypes.h>
  37. #include <LibWeb/Layout/Node.h>
  38. #include <LibWeb/Layout/TextNode.h>
  39. #include <LibWeb/Layout/Viewport.h>
  40. namespace Web::DOM {
  41. static IDAllocator s_node_id_allocator;
  42. static HashMap<i32, Node*> s_node_directory;
  43. static i32 allocate_node_id(Node* node)
  44. {
  45. i32 id = s_node_id_allocator.allocate();
  46. s_node_directory.set(id, node);
  47. return id;
  48. }
  49. static void deallocate_node_id(i32 node_id)
  50. {
  51. if (!s_node_directory.remove(node_id))
  52. VERIFY_NOT_REACHED();
  53. s_node_id_allocator.deallocate(node_id);
  54. }
  55. Node* Node::from_id(i32 node_id)
  56. {
  57. return s_node_directory.get(node_id).value_or(nullptr);
  58. }
  59. Node::Node(JS::Realm& realm, Document& document, NodeType type)
  60. : EventTarget(realm)
  61. , m_document(&document)
  62. , m_type(type)
  63. , m_id(allocate_node_id(this))
  64. {
  65. }
  66. Node::Node(Document& document, NodeType type)
  67. : Node(document.realm(), document, type)
  68. {
  69. }
  70. Node::~Node() = default;
  71. void Node::finalize()
  72. {
  73. Base::finalize();
  74. if (layout_node() && layout_node()->parent())
  75. layout_node()->parent()->remove_child(*layout_node());
  76. deallocate_node_id(m_id);
  77. }
  78. void Node::visit_edges(Cell::Visitor& visitor)
  79. {
  80. Base::visit_edges(visitor);
  81. visitor.visit(m_document.ptr());
  82. visitor.visit(m_parent.ptr());
  83. visitor.visit(m_first_child.ptr());
  84. visitor.visit(m_last_child.ptr());
  85. visitor.visit(m_next_sibling.ptr());
  86. visitor.visit(m_previous_sibling.ptr());
  87. visitor.visit(m_child_nodes);
  88. visitor.visit(m_layout_node);
  89. for (auto& registered_observer : m_registered_observer_list)
  90. visitor.visit(registered_observer);
  91. }
  92. // https://dom.spec.whatwg.org/#dom-node-baseuri
  93. DeprecatedString Node::base_uri() const
  94. {
  95. // Return this’s node document’s document base URL, serialized.
  96. return document().base_url().to_deprecated_string();
  97. }
  98. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  99. {
  100. for (auto* node = this; node; node = node->parent()) {
  101. if (!is<HTML::HTMLAnchorElement>(*node))
  102. continue;
  103. auto const& anchor_element = static_cast<HTML::HTMLAnchorElement const&>(*node);
  104. if (anchor_element.has_attribute(HTML::AttributeNames::href))
  105. return &anchor_element;
  106. }
  107. return nullptr;
  108. }
  109. const HTML::HTMLElement* Node::enclosing_html_element() const
  110. {
  111. return first_ancestor_of_type<HTML::HTMLElement>();
  112. }
  113. const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(DeprecatedFlyString const& attribute) const
  114. {
  115. for (auto* node = this; node; node = node->parent()) {
  116. if (is<HTML::HTMLElement>(*node) && verify_cast<HTML::HTMLElement>(*node).has_attribute(attribute))
  117. return verify_cast<HTML::HTMLElement>(node);
  118. }
  119. return nullptr;
  120. }
  121. // https://dom.spec.whatwg.org/#concept-descendant-text-content
  122. DeprecatedString Node::descendant_text_content() const
  123. {
  124. StringBuilder builder;
  125. for_each_in_subtree_of_type<Text>([&](auto& text_node) {
  126. builder.append(text_node.data());
  127. return IterationDecision::Continue;
  128. });
  129. return builder.to_deprecated_string();
  130. }
  131. // https://dom.spec.whatwg.org/#dom-node-textcontent
  132. DeprecatedString Node::text_content() const
  133. {
  134. // The textContent getter steps are to return the following, switching on the interface this implements:
  135. // If DocumentFragment or Element, return the descendant text content of this.
  136. if (is<DocumentFragment>(this) || is<Element>(this))
  137. return descendant_text_content();
  138. // If CharacterData, return this’s data.
  139. if (is<CharacterData>(this))
  140. return static_cast<CharacterData const&>(*this).data();
  141. // If Attr node, return this's value.
  142. if (is<Attr>(*this))
  143. return static_cast<Attr const&>(*this).value();
  144. // Otherwise, return null
  145. return {};
  146. }
  147. // https://dom.spec.whatwg.org/#ref-for-dom-node-textcontent%E2%91%A0
  148. void Node::set_text_content(DeprecatedString const& content)
  149. {
  150. // The textContent setter steps are to, if the given value is null, act as if it was the empty string instead,
  151. // and then do as described below, switching on the interface this implements:
  152. // If DocumentFragment or Element, string replace all with the given value within this.
  153. if (is<DocumentFragment>(this) || is<Element>(this)) {
  154. string_replace_all(content);
  155. }
  156. // If CharacterData, replace data with node this, offset 0, count this’s length, and data the given value.
  157. else if (is<CharacterData>(this)) {
  158. auto* character_data_node = verify_cast<CharacterData>(this);
  159. character_data_node->set_data(content);
  160. // FIXME: CharacterData::set_data is not spec compliant. Make this match the spec when set_data becomes spec compliant.
  161. // Do note that this will make this function able to throw an exception.
  162. }
  163. // If Attr, set an existing attribute value with this and the given value.
  164. if (is<Attr>(*this)) {
  165. static_cast<Attr&>(*this).set_value(content);
  166. }
  167. // Otherwise, do nothing.
  168. set_needs_style_update(true);
  169. }
  170. // https://dom.spec.whatwg.org/#dom-node-nodevalue
  171. DeprecatedString Node::node_value() const
  172. {
  173. // The nodeValue getter steps are to return the following, switching on the interface this implements:
  174. // If Attr, return this’s value.
  175. if (is<Attr>(this)) {
  176. return verify_cast<Attr>(this)->value();
  177. }
  178. // If CharacterData, return this’s data.
  179. if (is<CharacterData>(this)) {
  180. return verify_cast<CharacterData>(this)->data();
  181. }
  182. // Otherwise, return null.
  183. return {};
  184. }
  185. // https://dom.spec.whatwg.org/#ref-for-dom-node-nodevalue%E2%91%A0
  186. void Node::set_node_value(DeprecatedString const& value)
  187. {
  188. // The nodeValue setter steps are to, if the given value is null, act as if it was the empty string instead,
  189. // and then do as described below, switching on the interface this implements:
  190. // If Attr, set an existing attribute value with this and the given value.
  191. if (is<Attr>(this)) {
  192. verify_cast<Attr>(this)->set_value(value);
  193. } else if (is<CharacterData>(this)) {
  194. // If CharacterData, replace data with node this, offset 0, count this’s length, and data the given value.
  195. verify_cast<CharacterData>(this)->set_data(value);
  196. }
  197. // Otherwise, do nothing.
  198. }
  199. void Node::invalidate_style()
  200. {
  201. if (is_document()) {
  202. auto& document = static_cast<DOM::Document&>(*this);
  203. document.set_needs_full_style_update(true);
  204. document.schedule_style_update();
  205. return;
  206. }
  207. for_each_in_inclusive_subtree([&](Node& node) {
  208. node.m_needs_style_update = true;
  209. if (node.has_children())
  210. node.m_child_needs_style_update = true;
  211. if (auto* shadow_root = node.is_element() ? static_cast<DOM::Element&>(node).shadow_root_internal() : nullptr) {
  212. node.m_child_needs_style_update = true;
  213. shadow_root->m_needs_style_update = true;
  214. if (shadow_root->has_children())
  215. shadow_root->m_child_needs_style_update = true;
  216. }
  217. return IterationDecision::Continue;
  218. });
  219. for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host())
  220. ancestor->m_child_needs_style_update = true;
  221. document().schedule_style_update();
  222. }
  223. DeprecatedString Node::child_text_content() const
  224. {
  225. if (!is<ParentNode>(*this))
  226. return DeprecatedString::empty();
  227. StringBuilder builder;
  228. verify_cast<ParentNode>(*this).for_each_child([&](auto& child) {
  229. if (is<Text>(child))
  230. builder.append(verify_cast<Text>(child).text_content());
  231. });
  232. return builder.to_deprecated_string();
  233. }
  234. // https://dom.spec.whatwg.org/#concept-tree-root
  235. Node& Node::root()
  236. {
  237. // The root of an object is itself, if its parent is null, or else it is the root of its parent.
  238. // The root of a tree is any object participating in that tree whose parent is null.
  239. Node* root = this;
  240. while (root->parent())
  241. root = root->parent();
  242. return *root;
  243. }
  244. // https://dom.spec.whatwg.org/#concept-shadow-including-root
  245. Node& Node::shadow_including_root()
  246. {
  247. // The shadow-including root of an object is its root’s host’s shadow-including root,
  248. // if the object’s root is a shadow root; otherwise its root.
  249. auto& node_root = root();
  250. if (is<ShadowRoot>(node_root))
  251. return static_cast<ShadowRoot&>(node_root).host()->shadow_including_root();
  252. return node_root;
  253. }
  254. // https://dom.spec.whatwg.org/#connected
  255. bool Node::is_connected() const
  256. {
  257. // An element is connected if its shadow-including root is a document.
  258. return shadow_including_root().is_document();
  259. }
  260. Element* Node::parent_element()
  261. {
  262. if (!parent() || !is<Element>(parent()))
  263. return nullptr;
  264. return verify_cast<Element>(parent());
  265. }
  266. Element const* Node::parent_element() const
  267. {
  268. if (!parent() || !is<Element>(parent()))
  269. return nullptr;
  270. return verify_cast<Element>(parent());
  271. }
  272. // https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  273. WebIDL::ExceptionOr<void> Node::ensure_pre_insertion_validity(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child) const
  274. {
  275. // 1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.
  276. if (!is<Document>(this) && !is<DocumentFragment>(this) && !is<Element>(this))
  277. return WebIDL::HierarchyRequestError::create(realm(), "Can only insert into a document, document fragment or element");
  278. // 2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException.
  279. if (node->is_host_including_inclusive_ancestor_of(*this))
  280. return WebIDL::HierarchyRequestError::create(realm(), "New node is an ancestor of this node");
  281. // 3. If child is non-null and its parent is not parent, then throw a "NotFoundError" DOMException.
  282. if (child && child->parent() != this)
  283. return WebIDL::NotFoundError::create(realm(), "This node is not the parent of the given child");
  284. // FIXME: All the following "Invalid node type for insertion" messages could be more descriptive.
  285. // 4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException.
  286. if (!is<DocumentFragment>(*node) && !is<DocumentType>(*node) && !is<Element>(*node) && !is<Text>(*node) && !is<Comment>(*node) && !is<ProcessingInstruction>(*node))
  287. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  288. // 5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException.
  289. if ((is<Text>(*node) && is<Document>(this)) || (is<DocumentType>(*node) && !is<Document>(this)))
  290. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  291. // 6. If parent is a document, and any of the statements below, switched on the interface node implements, are true, then throw a "HierarchyRequestError" DOMException.
  292. if (is<Document>(this)) {
  293. // DocumentFragment
  294. if (is<DocumentFragment>(*node)) {
  295. // If node has more than one element child or has a Text node child.
  296. // Otherwise, if node has one element child and either parent has an element child, child is a doctype, or child is non-null and a doctype is following child.
  297. auto node_element_child_count = verify_cast<DocumentFragment>(*node).child_element_count();
  298. if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
  299. || (node_element_child_count == 1 && (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) || (child && child->has_following_node_of_type_in_tree_order<DocumentType>())))) {
  300. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  301. }
  302. } else if (is<Element>(*node)) {
  303. // Element
  304. // If parent has an element child, child is a doctype, or child is non-null and a doctype is following child.
  305. if (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) || (child && child->has_following_node_of_type_in_tree_order<DocumentType>()))
  306. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  307. } else if (is<DocumentType>(*node)) {
  308. // DocumentType
  309. // parent has a doctype child, child is non-null and an element is preceding child, or child is null and parent has an element child.
  310. if (has_child_of_type<DocumentType>() || (child && child->has_preceding_node_of_type_in_tree_order<Element>()) || (!child && has_child_of_type<Element>()))
  311. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  312. }
  313. }
  314. return {};
  315. }
  316. // https://dom.spec.whatwg.org/#concept-node-insert
  317. void Node::insert_before(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child, bool suppress_observers)
  318. {
  319. // 1. Let nodes be node’s children, if node is a DocumentFragment node; otherwise « node ».
  320. Vector<JS::Handle<Node>> nodes;
  321. if (is<DocumentFragment>(*node))
  322. nodes = node->children_as_vector();
  323. else
  324. nodes.append(JS::make_handle(*node));
  325. // 2. Let count be nodes’s size.
  326. auto count = nodes.size();
  327. // 3. If count is 0, then return.
  328. if (count == 0)
  329. return;
  330. // 4. If node is a DocumentFragment node, then:
  331. if (is<DocumentFragment>(*node)) {
  332. // 1. Remove its children with the suppress observers flag set.
  333. node->remove_all_children(true);
  334. // 2. Queue a tree mutation record for node with « », nodes, null, and null.
  335. // NOTE: This step intentionally does not pay attention to the suppress observers flag.
  336. auto added_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors();
  337. auto removed_node_list = StaticNodeList::create(realm(), nodes).release_value_but_fixme_should_propagate_errors();
  338. node->queue_tree_mutation_record(added_node_list, removed_node_list, nullptr, nullptr);
  339. }
  340. // 5. If child is non-null, then:
  341. if (child) {
  342. // 1. For each live range whose start node is parent and start offset is greater than child’s index, increase its start offset by count.
  343. for (auto& range : Range::live_ranges()) {
  344. if (range->start_container() == this && range->start_offset() > child->index())
  345. MUST(range->set_start(*range->start_container(), range->start_offset() + count));
  346. }
  347. // 2. For each live range whose end node is parent and end offset is greater than child’s index, increase its end offset by count.
  348. for (auto& range : Range::live_ranges()) {
  349. if (range->end_container() == this && range->end_offset() > child->index())
  350. MUST(range->set_end(*range->end_container(), range->end_offset() + count));
  351. }
  352. }
  353. // 6. Let previousSibling be child’s previous sibling or parent’s last child if child is null.
  354. JS::GCPtr<Node> previous_sibling;
  355. if (child)
  356. previous_sibling = child->previous_sibling();
  357. else
  358. previous_sibling = last_child();
  359. // 7. For each node in nodes, in tree order:
  360. // FIXME: In tree order
  361. for (auto& node_to_insert : nodes) {
  362. // 1. Adopt node into parent’s node document.
  363. document().adopt_node(*node_to_insert);
  364. // 2. If child is null, then append node to parent’s children.
  365. if (!child)
  366. append_child_impl(*node_to_insert);
  367. // 3. Otherwise, insert node into parent’s children before child’s index.
  368. else
  369. insert_before_impl(*node_to_insert, child);
  370. // FIXME: 4. If parent is a shadow host and node is a slottable, then assign a slot for node.
  371. // FIXME: 5. If parent’s root is a shadow root, and parent is a slot whose assigned nodes is the empty list, then run signal a slot change for parent.
  372. // FIXME: 6. Run assign slottables for a tree with node’s root.
  373. // FIXME: This should be shadow-including.
  374. // 7. For each shadow-including inclusive descendant inclusiveDescendant of node, in shadow-including tree order:
  375. node_to_insert->for_each_in_inclusive_subtree([&](Node& inclusive_descendant) {
  376. // 1. Run the insertion steps with inclusiveDescendant.
  377. inclusive_descendant.inserted();
  378. // 2. If inclusiveDescendant is connected, then:
  379. if (inclusive_descendant.is_connected()) {
  380. // FIXME: 1. If inclusiveDescendant is custom, then enqueue a custom element callback reaction with inclusiveDescendant, callback name "connectedCallback", and an empty argument list.
  381. // FIXME: 2. Otherwise, try to upgrade inclusiveDescendant.
  382. // NOTE: If this successfully upgrades inclusiveDescendant, its connectedCallback will be enqueued automatically during the upgrade an element algorithm.
  383. }
  384. return IterationDecision::Continue;
  385. });
  386. }
  387. // 8. If suppress observers flag is unset, then queue a tree mutation record for parent with nodes, « », previousSibling, and child.
  388. if (!suppress_observers) {
  389. auto added_node_list = StaticNodeList::create(realm(), move(nodes)).release_value_but_fixme_should_propagate_errors();
  390. auto removed_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors();
  391. queue_tree_mutation_record(added_node_list, removed_node_list, previous_sibling.ptr(), child.ptr());
  392. }
  393. // 9. Run the children changed steps for parent.
  394. children_changed();
  395. // FIXME: This will need to become smarter when we implement the :has() selector.
  396. invalidate_style();
  397. }
  398. // https://dom.spec.whatwg.org/#concept-node-pre-insert
  399. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> Node::pre_insert(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child)
  400. {
  401. // 1. Ensure pre-insertion validity of node into parent before child.
  402. TRY(ensure_pre_insertion_validity(node, child));
  403. // 2. Let referenceChild be child.
  404. auto reference_child = child;
  405. // 3. If referenceChild is node, then set referenceChild to node’s next sibling.
  406. if (reference_child == node)
  407. reference_child = node->next_sibling();
  408. // 4. Insert node into parent before referenceChild.
  409. insert_before(node, reference_child);
  410. // 5. Return node.
  411. return node;
  412. }
  413. // https://dom.spec.whatwg.org/#dom-node-removechild
  414. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> Node::remove_child(JS::NonnullGCPtr<Node> child)
  415. {
  416. // The removeChild(child) method steps are to return the result of pre-removing child from this.
  417. return pre_remove(child);
  418. }
  419. // https://dom.spec.whatwg.org/#concept-node-pre-remove
  420. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> Node::pre_remove(JS::NonnullGCPtr<Node> child)
  421. {
  422. // 1. If child’s parent is not parent, then throw a "NotFoundError" DOMException.
  423. if (child->parent() != this)
  424. return WebIDL::NotFoundError::create(realm(), "Child does not belong to this node");
  425. // 2. Remove child.
  426. child->remove();
  427. // 3. Return child.
  428. return child;
  429. }
  430. // https://dom.spec.whatwg.org/#concept-node-append
  431. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> Node::append_child(JS::NonnullGCPtr<Node> node)
  432. {
  433. // To append a node to a parent, pre-insert node into parent before null.
  434. return pre_insert(node, nullptr);
  435. }
  436. // https://dom.spec.whatwg.org/#concept-node-remove
  437. void Node::remove(bool suppress_observers)
  438. {
  439. // 1. Let parent be node’s parent
  440. auto* parent = this->parent();
  441. // 2. Assert: parent is non-null.
  442. VERIFY(parent);
  443. // 3. Let index be node’s index.
  444. auto index = this->index();
  445. // 4. For each live range whose start node is an inclusive descendant of node, set its start to (parent, index).
  446. for (auto& range : Range::live_ranges()) {
  447. if (range->start_container()->is_inclusive_descendant_of(*this))
  448. MUST(range->set_start(*parent, index));
  449. }
  450. // 5. For each live range whose end node is an inclusive descendant of node, set its end to (parent, index).
  451. for (auto& range : Range::live_ranges()) {
  452. if (range->end_container()->is_inclusive_descendant_of(*this))
  453. MUST(range->set_end(*parent, index));
  454. }
  455. // 6. For each live range whose start node is parent and start offset is greater than index, decrease its start offset by 1.
  456. for (auto& range : Range::live_ranges()) {
  457. if (range->start_container() == parent && range->start_offset() > index)
  458. MUST(range->set_start(*range->start_container(), range->start_offset() - 1));
  459. }
  460. // 7. For each live range whose end node is parent and end offset is greater than index, decrease its end offset by 1.
  461. for (auto& range : Range::live_ranges()) {
  462. if (range->end_container() == parent && range->end_offset() > index)
  463. MUST(range->set_end(*range->end_container(), range->end_offset() - 1));
  464. }
  465. // 8. For each NodeIterator object iterator whose root’s node document is node’s node document, run the NodeIterator pre-removing steps given node and iterator.
  466. document().for_each_node_iterator([&](NodeIterator& node_iterator) {
  467. node_iterator.run_pre_removing_steps(*this);
  468. });
  469. // 9. Let oldPreviousSibling be node’s previous sibling.
  470. JS::GCPtr<Node> old_previous_sibling = previous_sibling();
  471. // 10. Let oldNextSibling be node’s next sibling.
  472. JS::GCPtr<Node> old_next_sibling = next_sibling();
  473. // 11. Remove node from its parent’s children.
  474. parent->remove_child_impl(*this);
  475. // FIXME: 12. If node is assigned, then run assign slottables for node’s assigned slot.
  476. // FIXME: 13. If parent’s root is a shadow root, and parent is a slot whose assigned nodes is the empty list, then run signal a slot change for parent.
  477. // FIXME: 14. If node has an inclusive descendant that is a slot, then:
  478. // 1. Run assign slottables for a tree with parent’s root.
  479. // 2. Run assign slottables for a tree with node.
  480. // 15. Run the removing steps with node and parent.
  481. removed_from(parent);
  482. // FIXME: 16. Let isParentConnected be parent’s connected. (Currently unused so not included)
  483. // FIXME: 17. If node is custom and isParentConnected is true, then enqueue a custom element callback reaction with node,
  484. // callback name "disconnectedCallback", and an empty argument list.
  485. // NOTE: It is intentional for now that custom elements do not get parent passed. This might change in the future if there is a need.
  486. // FIXME: This should be shadow-including.
  487. // 18. For each shadow-including descendant descendant of node, in shadow-including tree order, then:
  488. for_each_in_subtree([&](Node& descendant) {
  489. // 1. Run the removing steps with descendant
  490. descendant.removed_from(nullptr);
  491. // FIXME: 2. If descendant is custom and isParentConnected is true, then enqueue a custom element callback reaction with descendant,
  492. // callback name "disconnectedCallback", and an empty argument list.
  493. return IterationDecision::Continue;
  494. });
  495. // 19. For each inclusive ancestor inclusiveAncestor of parent, and then for each registered of inclusiveAncestor’s registered observer list,
  496. // if registered’s options["subtree"] is true, then append a new transient registered observer
  497. // whose observer is registered’s observer, options is registered’s options, and source is registered to node’s registered observer list.
  498. for (auto* inclusive_ancestor = parent; inclusive_ancestor; inclusive_ancestor = inclusive_ancestor->parent()) {
  499. for (auto& registered : inclusive_ancestor->m_registered_observer_list) {
  500. if (registered.options().subtree) {
  501. auto transient_observer = TransientRegisteredObserver::create(registered.observer(), registered.options(), registered);
  502. m_registered_observer_list.append(move(transient_observer));
  503. }
  504. }
  505. }
  506. // 20. If suppress observers flag is unset, then queue a tree mutation record for parent with « », « node », oldPreviousSibling, and oldNextSibling.
  507. if (!suppress_observers) {
  508. Vector<JS::Handle<Node>> removed_nodes;
  509. removed_nodes.append(JS::make_handle(*this));
  510. auto added_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors();
  511. auto removed_node_list = StaticNodeList::create(realm(), move(removed_nodes)).release_value_but_fixme_should_propagate_errors();
  512. parent->queue_tree_mutation_record(added_node_list, removed_node_list, old_previous_sibling.ptr(), old_next_sibling.ptr());
  513. }
  514. // 21. Run the children changed steps for parent.
  515. parent->children_changed();
  516. document().invalidate_layout();
  517. }
  518. // https://dom.spec.whatwg.org/#concept-node-replace
  519. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> Node::replace_child(JS::NonnullGCPtr<Node> node, JS::NonnullGCPtr<Node> child)
  520. {
  521. // If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.
  522. if (!is<Document>(this) && !is<DocumentFragment>(this) && !is<Element>(this))
  523. return WebIDL::HierarchyRequestError::create(realm(), "Can only insert into a document, document fragment or element");
  524. // 2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException.
  525. if (node->is_host_including_inclusive_ancestor_of(*this))
  526. return WebIDL::HierarchyRequestError::create(realm(), "New node is an ancestor of this node");
  527. // 3. If child’s parent is not parent, then throw a "NotFoundError" DOMException.
  528. if (child->parent() != this)
  529. return WebIDL::NotFoundError::create(realm(), "This node is not the parent of the given child");
  530. // FIXME: All the following "Invalid node type for insertion" messages could be more descriptive.
  531. // 4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException.
  532. if (!is<DocumentFragment>(*node) && !is<DocumentType>(*node) && !is<Element>(*node) && !is<Text>(*node) && !is<Comment>(*node) && !is<ProcessingInstruction>(*node))
  533. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  534. // 5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException.
  535. if ((is<Text>(*node) && is<Document>(this)) || (is<DocumentType>(*node) && !is<Document>(this)))
  536. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  537. // If parent is a document, and any of the statements below, switched on the interface node implements, are true, then throw a "HierarchyRequestError" DOMException.
  538. if (is<Document>(this)) {
  539. // DocumentFragment
  540. if (is<DocumentFragment>(*node)) {
  541. // If node has more than one element child or has a Text node child.
  542. // Otherwise, if node has one element child and either parent has an element child that is not child or a doctype is following child.
  543. auto node_element_child_count = verify_cast<DocumentFragment>(*node).child_element_count();
  544. if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
  545. || (node_element_child_count == 1 && (first_child_of_type<Element>() != child || child->has_following_node_of_type_in_tree_order<DocumentType>()))) {
  546. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  547. }
  548. } else if (is<Element>(*node)) {
  549. // Element
  550. // parent has an element child that is not child or a doctype is following child.
  551. if (first_child_of_type<Element>() != child || child->has_following_node_of_type_in_tree_order<DocumentType>())
  552. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  553. } else if (is<DocumentType>(*node)) {
  554. // DocumentType
  555. // parent has a doctype child that is not child, or an element is preceding child.
  556. if (first_child_of_type<DocumentType>() != node || child->has_preceding_node_of_type_in_tree_order<Element>())
  557. return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion");
  558. }
  559. }
  560. // 7. Let referenceChild be child’s next sibling.
  561. JS::GCPtr<Node> reference_child = child->next_sibling();
  562. // 8. If referenceChild is node, then set referenceChild to node’s next sibling.
  563. if (reference_child == node)
  564. reference_child = node->next_sibling();
  565. // 9. Let previousSibling be child’s previous sibling.
  566. JS::GCPtr<Node> previous_sibling = child->previous_sibling();
  567. // 10. Let removedNodes be the empty set.
  568. Vector<JS::Handle<Node>> removed_nodes;
  569. // 11. If child’s parent is non-null, then:
  570. // NOTE: The above can only be false if child is node.
  571. if (child->parent()) {
  572. // 1. Set removedNodes to « child ».
  573. removed_nodes.append(JS::make_handle(*child));
  574. // 2. Remove child with the suppress observers flag set.
  575. child->remove(true);
  576. }
  577. // 12. Let nodes be node’s children if node is a DocumentFragment node; otherwise « node ».
  578. Vector<JS::Handle<Node>> nodes;
  579. if (is<DocumentFragment>(*node))
  580. nodes = node->children_as_vector();
  581. else
  582. nodes.append(JS::make_handle(*node));
  583. // 13. Insert node into parent before referenceChild with the suppress observers flag set.
  584. insert_before(node, reference_child, true);
  585. // 14. Queue a tree mutation record for parent with nodes, removedNodes, previousSibling, and referenceChild.
  586. auto added_node_list = TRY(StaticNodeList::create(realm(), move(nodes)));
  587. auto removed_node_list = TRY(StaticNodeList::create(realm(), move(removed_nodes)));
  588. queue_tree_mutation_record(added_node_list, removed_node_list, previous_sibling.ptr(), reference_child.ptr());
  589. // 15. Return child.
  590. return child;
  591. }
  592. // https://dom.spec.whatwg.org/#concept-node-clone
  593. JS::NonnullGCPtr<Node> Node::clone_node(Document* document, bool clone_children)
  594. {
  595. // 1. If document is not given, let document be node’s node document.
  596. if (!document)
  597. document = m_document.ptr();
  598. JS::GCPtr<Node> copy;
  599. // 2. If node is an element, then:
  600. if (is<Element>(this)) {
  601. // 1. Let copy be the result of creating an element, given document, node’s local name, node’s namespace, node’s namespace prefix, and node’s is value, with the synchronous custom elements flag unset.
  602. auto& element = *verify_cast<Element>(this);
  603. auto element_copy = DOM::create_element(*document, element.local_name(), element.namespace_() /* FIXME: node’s namespace prefix, and node’s is value, with the synchronous custom elements flag unset */).release_value_but_fixme_should_propagate_errors();
  604. // 2. For each attribute in node’s attribute list:
  605. element.for_each_attribute([&](auto& name, auto& value) {
  606. // 1. Let copyAttribute be a clone of attribute.
  607. // 2. Append copyAttribute to copy.
  608. MUST(element_copy->set_attribute(name, value));
  609. });
  610. copy = move(element_copy);
  611. }
  612. // 3. Otherwise, let copy be a node that implements the same interfaces as node, and fulfills these additional requirements, switching on the interface node implements:
  613. else if (is<Document>(this)) {
  614. // Document
  615. auto document_ = verify_cast<Document>(this);
  616. auto document_copy = Document::create(this->realm(), document_->url()).release_value_but_fixme_should_propagate_errors();
  617. // Set copy’s encoding, content type, URL, origin, type, and mode to those of node.
  618. document_copy->set_encoding(document_->encoding());
  619. document_copy->set_content_type(document_->content_type());
  620. document_copy->set_url(document_->url());
  621. document_copy->set_origin(document_->origin());
  622. document_copy->set_document_type(document_->document_type());
  623. document_copy->set_quirks_mode(document_->mode());
  624. copy = move(document_copy);
  625. } else if (is<DocumentType>(this)) {
  626. // DocumentType
  627. auto document_type = verify_cast<DocumentType>(this);
  628. auto document_type_copy = heap().allocate<DocumentType>(realm(), *document).release_allocated_value_but_fixme_should_propagate_errors();
  629. // Set copy’s name, public ID, and system ID to those of node.
  630. document_type_copy->set_name(document_type->name());
  631. document_type_copy->set_public_id(document_type->public_id());
  632. document_type_copy->set_system_id(document_type->system_id());
  633. copy = move(document_type_copy);
  634. } else if (is<Attr>(this)) {
  635. // Attr
  636. // Set copy’s namespace, namespace prefix, local name, and value to those of node.
  637. auto& attr = static_cast<Attr&>(*this);
  638. copy = attr.clone(*document);
  639. } else if (is<Text>(this)) {
  640. // Text
  641. auto text = verify_cast<Text>(this);
  642. // Set copy’s data to that of node.
  643. auto text_copy = heap().allocate<Text>(realm(), *document, text->data()).release_allocated_value_but_fixme_should_propagate_errors();
  644. copy = move(text_copy);
  645. } else if (is<Comment>(this)) {
  646. // Comment
  647. auto comment = verify_cast<Comment>(this);
  648. // Set copy’s data to that of node.
  649. auto comment_copy = heap().allocate<Comment>(realm(), *document, comment->data()).release_allocated_value_but_fixme_should_propagate_errors();
  650. copy = move(comment_copy);
  651. } else if (is<ProcessingInstruction>(this)) {
  652. // ProcessingInstruction
  653. auto processing_instruction = verify_cast<ProcessingInstruction>(this);
  654. // Set copy’s target and data to those of node.
  655. auto processing_instruction_copy = heap().allocate<ProcessingInstruction>(realm(), *document, processing_instruction->data(), processing_instruction->target()).release_allocated_value_but_fixme_should_propagate_errors();
  656. copy = processing_instruction_copy;
  657. }
  658. // Otherwise, Do nothing.
  659. else if (is<DocumentFragment>(this)) {
  660. copy = heap().allocate<DocumentFragment>(realm(), *document).release_allocated_value_but_fixme_should_propagate_errors();
  661. }
  662. // FIXME: 4. Set copy’s node document and document to copy, if copy is a document, and set copy’s node document to document otherwise.
  663. // 5. Run any cloning steps defined for node in other applicable specifications and pass copy, node, document and the clone children flag if set, as parameters.
  664. cloned(*copy, clone_children);
  665. // 6. If the clone children flag is set, clone all the children of node and append them to copy, with document as specified and the clone children flag being set.
  666. if (clone_children) {
  667. for_each_child([&](auto& child) {
  668. MUST(copy->append_child(child.clone_node(document, true)));
  669. });
  670. }
  671. // 7. Return copy.
  672. return *copy;
  673. }
  674. // https://dom.spec.whatwg.org/#dom-node-clonenode
  675. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> Node::clone_node_binding(bool deep)
  676. {
  677. // 1. If this is a shadow root, then throw a "NotSupportedError" DOMException.
  678. if (is<ShadowRoot>(*this))
  679. return WebIDL::NotSupportedError::create(realm(), "Cannot clone shadow root");
  680. // 2. Return a clone of this, with the clone children flag set if deep is true.
  681. return clone_node(nullptr, deep);
  682. }
  683. void Node::set_document(Badge<Document>, Document& document)
  684. {
  685. if (m_document.ptr() == &document)
  686. return;
  687. m_document = &document;
  688. if (needs_style_update() || child_needs_style_update()) {
  689. // NOTE: We unset and reset the "needs style update" flag here.
  690. // This ensures that there's a pending style update in the new document
  691. // that will eventually assign some style to this node if needed.
  692. set_needs_style_update(false);
  693. set_needs_style_update(true);
  694. }
  695. }
  696. bool Node::is_editable() const
  697. {
  698. return parent() && parent()->is_editable();
  699. }
  700. void Node::set_layout_node(Badge<Layout::Node>, JS::NonnullGCPtr<Layout::Node> layout_node)
  701. {
  702. m_layout_node = layout_node;
  703. }
  704. void Node::detach_layout_node(Badge<DOM::Document>)
  705. {
  706. m_layout_node = nullptr;
  707. }
  708. EventTarget* Node::get_parent(Event const&)
  709. {
  710. // FIXME: returns the node’s assigned slot, if node is assigned, and node’s parent otherwise.
  711. return parent();
  712. }
  713. void Node::set_needs_style_update(bool value)
  714. {
  715. if (m_needs_style_update == value)
  716. return;
  717. m_needs_style_update = value;
  718. if (m_needs_style_update) {
  719. for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host()) {
  720. ancestor->m_child_needs_style_update = true;
  721. }
  722. document().schedule_style_update();
  723. }
  724. }
  725. void Node::inserted()
  726. {
  727. set_needs_style_update(true);
  728. }
  729. ParentNode* Node::parent_or_shadow_host()
  730. {
  731. if (is<ShadowRoot>(*this))
  732. return static_cast<ShadowRoot&>(*this).host();
  733. return verify_cast<ParentNode>(parent());
  734. }
  735. Element* Node::parent_or_shadow_host_element()
  736. {
  737. if (is<ShadowRoot>(*this))
  738. return static_cast<ShadowRoot&>(*this).host();
  739. if (!parent())
  740. return nullptr;
  741. if (is<Element>(*parent()))
  742. return static_cast<Element*>(parent());
  743. if (is<ShadowRoot>(*parent()))
  744. return static_cast<ShadowRoot&>(*parent()).host();
  745. return nullptr;
  746. }
  747. JS::NonnullGCPtr<NodeList> Node::child_nodes()
  748. {
  749. if (!m_child_nodes) {
  750. m_child_nodes = LiveNodeList::create(realm(), *this, [this](auto& node) {
  751. return is_parent_of(node);
  752. }).release_value_but_fixme_should_propagate_errors();
  753. }
  754. return *m_child_nodes;
  755. }
  756. Vector<JS::Handle<Node>> Node::children_as_vector() const
  757. {
  758. Vector<JS::Handle<Node>> nodes;
  759. for_each_child([&](auto& child) {
  760. nodes.append(JS::make_handle(child));
  761. });
  762. return nodes;
  763. }
  764. void Node::remove_all_children(bool suppress_observers)
  765. {
  766. while (JS::GCPtr<Node> child = first_child())
  767. child->remove(suppress_observers);
  768. }
  769. // https://dom.spec.whatwg.org/#dom-node-comparedocumentposition
  770. u16 Node::compare_document_position(JS::GCPtr<Node> other)
  771. {
  772. enum Position : u16 {
  773. DOCUMENT_POSITION_EQUAL = 0,
  774. DOCUMENT_POSITION_DISCONNECTED = 1,
  775. DOCUMENT_POSITION_PRECEDING = 2,
  776. DOCUMENT_POSITION_FOLLOWING = 4,
  777. DOCUMENT_POSITION_CONTAINS = 8,
  778. DOCUMENT_POSITION_CONTAINED_BY = 16,
  779. DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32,
  780. };
  781. // 1. If this is other, then return zero.
  782. if (this == other.ptr())
  783. return DOCUMENT_POSITION_EQUAL;
  784. // 2. Let node1 be other and node2 be this.
  785. Node* node1 = other.ptr();
  786. Node* node2 = this;
  787. // 3. Let attr1 and attr2 be null.
  788. Attr* attr1;
  789. Attr* attr2;
  790. // 4. If node1 is an attribute, then set attr1 to node1 and node1 to attr1’s element.
  791. if (is<Attr>(node1)) {
  792. attr1 = verify_cast<Attr>(node1);
  793. node1 = const_cast<Element*>(attr1->owner_element());
  794. }
  795. // 5. If node2 is an attribute, then:
  796. if (is<Attr>(node2)) {
  797. // 1. Set attr2 to node2 and node2 to attr2’s element.
  798. attr2 = verify_cast<Attr>(node2);
  799. node2 = const_cast<Element*>(attr2->owner_element());
  800. // 2. If attr1 and node1 are non-null, and node2 is node1, then:
  801. if (attr1 && node1 && node2 == node1) {
  802. // FIXME: 1. For each attr in node2’s attribute list:
  803. // 1. If attr equals attr1, then return the result of adding DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC and DOCUMENT_POSITION_PRECEDING.
  804. // 2. If attr equals attr2, then return the result of adding DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC and DOCUMENT_POSITION_FOLLOWING.
  805. }
  806. }
  807. // 6. If node1 or node2 is null, or node1’s root is not node2’s root, then return the result of adding
  808. // DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, and either DOCUMENT_POSITION_PRECEDING or DOCUMENT_POSITION_FOLLOWING, with the constraint that this is to be consistent, together.
  809. if ((node1 == nullptr || node2 == nullptr) || (&node1->root() != &node2->root()))
  810. return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | (node1 > node2 ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING);
  811. // 7. If node1 is an ancestor of node2 and attr1 is null, or node1 is node2 and attr2 is non-null, then return the result of adding DOCUMENT_POSITION_CONTAINS to DOCUMENT_POSITION_PRECEDING.
  812. if ((node1->is_ancestor_of(*node2) && !attr1) || (node1 == node2 && attr2))
  813. return DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING;
  814. // 8. If node1 is a descendant of node2 and attr2 is null, or node1 is node2 and attr1 is non-null, then return the result of adding DOCUMENT_POSITION_CONTAINED_BY to DOCUMENT_POSITION_FOLLOWING.
  815. if ((node2->is_ancestor_of(*node1) && !attr2) || (node1 == node2 && attr1))
  816. return DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING;
  817. // 9. If node1 is preceding node2, then return DOCUMENT_POSITION_PRECEDING.
  818. if (node1->is_before(*node2))
  819. return DOCUMENT_POSITION_PRECEDING;
  820. // 10. Return DOCUMENT_POSITION_FOLLOWING.
  821. return DOCUMENT_POSITION_FOLLOWING;
  822. }
  823. // https://dom.spec.whatwg.org/#concept-tree-host-including-inclusive-ancestor
  824. bool Node::is_host_including_inclusive_ancestor_of(Node const& other) const
  825. {
  826. // An object A is a host-including inclusive ancestor of an object B,
  827. // if either A is an inclusive ancestor of B,
  828. if (is_inclusive_ancestor_of(other))
  829. return true;
  830. // or if B’s root has a non-null host and A is a host-including inclusive ancestor of B’s root’s host
  831. if (is<DocumentFragment>(other.root())
  832. && static_cast<DocumentFragment const&>(other.root()).host()
  833. && is_inclusive_ancestor_of(*static_cast<DocumentFragment const&>(other.root()).host())) {
  834. return true;
  835. }
  836. return false;
  837. }
  838. // https://dom.spec.whatwg.org/#dom-node-ownerdocument
  839. JS::GCPtr<Document> Node::owner_document() const
  840. {
  841. // The ownerDocument getter steps are to return null, if this is a document; otherwise this’s node document.
  842. if (is_document())
  843. return nullptr;
  844. return m_document;
  845. }
  846. // This function tells us whether a node is interesting enough to show up
  847. // in the DOM inspector. This hides two things:
  848. // - Non-rendered whitespace
  849. // - Rendered whitespace between block-level elements
  850. bool Node::is_uninteresting_whitespace_node() const
  851. {
  852. if (!is<Text>(*this))
  853. return false;
  854. if (!static_cast<Text const&>(*this).data().is_whitespace())
  855. return false;
  856. if (!layout_node())
  857. return true;
  858. if (auto parent = layout_node()->parent(); parent && parent->is_anonymous())
  859. return true;
  860. return false;
  861. }
  862. void Node::serialize_tree_as_json(JsonObjectSerializer<StringBuilder>& object) const
  863. {
  864. MUST(object.add("name"sv, node_name().view()));
  865. MUST(object.add("id"sv, id()));
  866. if (is_document()) {
  867. MUST(object.add("type"sv, "document"));
  868. } else if (is_element()) {
  869. MUST(object.add("type"sv, "element"));
  870. auto const* element = static_cast<DOM::Element const*>(this);
  871. if (element->has_attributes()) {
  872. auto attributes = MUST(object.add_object("attributes"sv));
  873. element->for_each_attribute([&attributes](auto& name, auto& value) {
  874. MUST(attributes.add(name, value));
  875. });
  876. MUST(attributes.finish());
  877. }
  878. if (element->is_browsing_context_container()) {
  879. auto const* container = static_cast<HTML::BrowsingContextContainer const*>(element);
  880. if (auto const* content_document = container->content_document()) {
  881. auto children = MUST(object.add_array("children"sv));
  882. JsonObjectSerializer<StringBuilder> content_document_object = MUST(children.add_object());
  883. content_document->serialize_tree_as_json(content_document_object);
  884. MUST(content_document_object.finish());
  885. MUST(children.finish());
  886. }
  887. }
  888. } else if (is_text()) {
  889. MUST(object.add("type"sv, "text"));
  890. auto text_node = static_cast<DOM::Text const*>(this);
  891. MUST(object.add("text"sv, text_node->data()));
  892. } else if (is_comment()) {
  893. MUST(object.add("type"sv, "comment"sv));
  894. MUST(object.add("data"sv, static_cast<DOM::Comment const&>(*this).data()));
  895. }
  896. MUST((object.add("visible"sv, !!layout_node())));
  897. if (has_child_nodes()) {
  898. auto children = MUST(object.add_array("children"sv));
  899. for_each_child([&children](DOM::Node& child) {
  900. if (child.is_uninteresting_whitespace_node())
  901. return;
  902. JsonObjectSerializer<StringBuilder> child_object = MUST(children.add_object());
  903. child.serialize_tree_as_json(child_object);
  904. MUST(child_object.finish());
  905. });
  906. // Pseudo-elements don't have DOM nodes,so we have to add them separately.
  907. if (is_element()) {
  908. auto const* element = static_cast<DOM::Element const*>(this);
  909. element->serialize_pseudo_elements_as_json(children);
  910. }
  911. MUST(children.finish());
  912. }
  913. }
  914. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-n-script
  915. bool Node::is_scripting_enabled() const
  916. {
  917. // Scripting is enabled for a node node if node's node document's browsing context is non-null, and scripting is enabled for node's relevant settings object.
  918. return document().browsing_context() && const_cast<Document&>(document()).relevant_settings_object().is_scripting_enabled();
  919. }
  920. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-n-noscript
  921. bool Node::is_scripting_disabled() const
  922. {
  923. // Scripting is disabled for a node when scripting is not enabled, i.e., when its node document's browsing context is null or when scripting is disabled for its relevant settings object.
  924. return !is_scripting_enabled();
  925. }
  926. // https://dom.spec.whatwg.org/#dom-node-contains
  927. bool Node::contains(JS::GCPtr<Node> other) const
  928. {
  929. // The contains(other) method steps are to return true if other is an inclusive descendant of this; otherwise false (including when other is null).
  930. return other && other->is_inclusive_descendant_of(*this);
  931. }
  932. // https://dom.spec.whatwg.org/#concept-shadow-including-descendant
  933. bool Node::is_shadow_including_descendant_of(Node const& other) const
  934. {
  935. // An object A is a shadow-including descendant of an object B,
  936. // if A is a descendant of B,
  937. if (is_descendant_of(other))
  938. return true;
  939. // or A’s root is a shadow root
  940. if (!is<ShadowRoot>(root()))
  941. return false;
  942. // and A’s root’s host is a shadow-including inclusive descendant of B.
  943. auto& shadow_root = verify_cast<ShadowRoot>(root());
  944. // NOTE: While host is nullable because of inheriting from DocumentFragment, shadow roots always have a host.
  945. return shadow_root.host()->is_shadow_including_inclusive_descendant_of(other);
  946. }
  947. // https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-descendant
  948. bool Node::is_shadow_including_inclusive_descendant_of(Node const& other) const
  949. {
  950. // A shadow-including inclusive descendant is an object or one of its shadow-including descendants.
  951. return &other == this || is_shadow_including_descendant_of(other);
  952. }
  953. // https://dom.spec.whatwg.org/#concept-shadow-including-ancestor
  954. bool Node::is_shadow_including_ancestor_of(Node const& other) const
  955. {
  956. // An object A is a shadow-including ancestor of an object B, if and only if B is a shadow-including descendant of A.
  957. return other.is_shadow_including_descendant_of(*this);
  958. }
  959. // https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
  960. bool Node::is_shadow_including_inclusive_ancestor_of(Node const& other) const
  961. {
  962. // A shadow-including inclusive ancestor is an object or one of its shadow-including ancestors.
  963. return other.is_shadow_including_inclusive_descendant_of(*this);
  964. }
  965. // https://dom.spec.whatwg.org/#concept-node-replace-all
  966. void Node::replace_all(JS::GCPtr<Node> node)
  967. {
  968. // 1. Let removedNodes be parent’s children.
  969. auto removed_nodes = children_as_vector();
  970. // 2. Let addedNodes be the empty set.
  971. Vector<JS::Handle<Node>> added_nodes;
  972. // 3. If node is a DocumentFragment node, then set addedNodes to node’s children.
  973. if (node && is<DocumentFragment>(*node)) {
  974. added_nodes = node->children_as_vector();
  975. }
  976. // 4. Otherwise, if node is non-null, set addedNodes to « node ».
  977. else if (node) {
  978. added_nodes.append(JS::make_handle(*node));
  979. }
  980. // 5. Remove all parent’s children, in tree order, with the suppress observers flag set.
  981. remove_all_children(true);
  982. // 6. If node is non-null, then insert node into parent before null with the suppress observers flag set.
  983. if (node)
  984. insert_before(*node, nullptr, true);
  985. // 7. If either addedNodes or removedNodes is not empty, then queue a tree mutation record for parent with addedNodes, removedNodes, null, and null.
  986. if (!added_nodes.is_empty() || !removed_nodes.is_empty()) {
  987. auto added_node_list = StaticNodeList::create(realm(), move(added_nodes)).release_value_but_fixme_should_propagate_errors();
  988. auto removed_node_list = StaticNodeList::create(realm(), move(removed_nodes)).release_value_but_fixme_should_propagate_errors();
  989. queue_tree_mutation_record(added_node_list, removed_node_list, nullptr, nullptr);
  990. }
  991. }
  992. // https://dom.spec.whatwg.org/#string-replace-all
  993. void Node::string_replace_all(DeprecatedString const& string)
  994. {
  995. // 1. Let node be null.
  996. JS::GCPtr<Node> node;
  997. // 2. If string is not the empty string, then set node to a new Text node whose data is string and node document is parent’s node document.
  998. if (!string.is_empty())
  999. node = heap().allocate<Text>(realm(), document(), string).release_allocated_value_but_fixme_should_propagate_errors();
  1000. // 3. Replace all with node within parent.
  1001. replace_all(node);
  1002. }
  1003. // https://w3c.github.io/DOM-Parsing/#dfn-fragment-serializing-algorithm
  1004. WebIDL::ExceptionOr<DeprecatedString> Node::serialize_fragment(DOMParsing::RequireWellFormed require_well_formed) const
  1005. {
  1006. // 1. Let context document be the value of node's node document.
  1007. auto const& context_document = document();
  1008. // 2. If context document is an HTML document, return an HTML serialization of node.
  1009. if (context_document.is_html_document())
  1010. return HTML::HTMLParser::serialize_html_fragment(*this);
  1011. // 3. Otherwise, context document is an XML document; return an XML serialization of node passing the flag require well-formed.
  1012. return DOMParsing::serialize_node_to_xml_string(*this, require_well_formed);
  1013. }
  1014. // https://dom.spec.whatwg.org/#dom-node-issamenode
  1015. bool Node::is_same_node(Node const* other_node) const
  1016. {
  1017. // The isSameNode(otherNode) method steps are to return true if otherNode is this; otherwise false.
  1018. return this == other_node;
  1019. }
  1020. // https://dom.spec.whatwg.org/#dom-node-isequalnode
  1021. bool Node::is_equal_node(Node const* other_node) const
  1022. {
  1023. // The isEqualNode(otherNode) method steps are to return true if otherNode is non-null and this equals otherNode; otherwise false.
  1024. if (!other_node)
  1025. return false;
  1026. // Fast path for testing a node against itself.
  1027. if (this == other_node)
  1028. return true;
  1029. // A node A equals a node B if all of the following conditions are true:
  1030. // A and B implement the same interfaces.
  1031. if (node_name() != other_node->node_name())
  1032. return false;
  1033. // The following are equal, switching on the interface A implements:
  1034. switch (node_type()) {
  1035. case (u16)NodeType::DOCUMENT_TYPE_NODE: {
  1036. // Its name, public ID, and system ID.
  1037. auto& this_doctype = verify_cast<DocumentType>(*this);
  1038. auto& other_doctype = verify_cast<DocumentType>(*other_node);
  1039. if (this_doctype.name() != other_doctype.name()
  1040. || this_doctype.public_id() != other_doctype.public_id()
  1041. || this_doctype.system_id() != other_doctype.system_id())
  1042. return false;
  1043. break;
  1044. }
  1045. case (u16)NodeType::ELEMENT_NODE: {
  1046. // Its namespace, namespace prefix, local name, and its attribute list’s size.
  1047. auto& this_element = verify_cast<Element>(*this);
  1048. auto& other_element = verify_cast<Element>(*other_node);
  1049. if (this_element.namespace_() != other_element.namespace_()
  1050. || this_element.prefix() != other_element.prefix()
  1051. || this_element.local_name() != other_element.local_name()
  1052. || this_element.attribute_list_size() != other_element.attribute_list_size())
  1053. return false;
  1054. // If A is an element, each attribute in its attribute list has an attribute that equals an attribute in B’s attribute list.
  1055. bool has_same_attributes = true;
  1056. this_element.for_each_attribute([&](auto& name, auto& value) {
  1057. if (other_element.get_attribute(name) != value)
  1058. has_same_attributes = false;
  1059. });
  1060. if (!has_same_attributes)
  1061. return false;
  1062. break;
  1063. }
  1064. case (u16)NodeType::COMMENT_NODE:
  1065. case (u16)NodeType::TEXT_NODE: {
  1066. // Its data.
  1067. auto& this_cdata = verify_cast<CharacterData>(*this);
  1068. auto& other_cdata = verify_cast<CharacterData>(*other_node);
  1069. if (this_cdata.data() != other_cdata.data())
  1070. return false;
  1071. break;
  1072. }
  1073. case (u16)NodeType::ATTRIBUTE_NODE: {
  1074. // Its namespace, local name, and value.
  1075. auto& this_attr = verify_cast<Attr>(*this);
  1076. auto& other_attr = verify_cast<Attr>(*other_node);
  1077. if (this_attr.namespace_uri() != other_attr.namespace_uri())
  1078. return false;
  1079. if (this_attr.local_name() != other_attr.local_name())
  1080. return false;
  1081. if (this_attr.value() != other_attr.value())
  1082. return false;
  1083. break;
  1084. }
  1085. case (u16)NodeType::PROCESSING_INSTRUCTION_NODE: {
  1086. // Its target and data.
  1087. auto& this_processing_instruction = verify_cast<ProcessingInstruction>(*this);
  1088. auto& other_processing_instruction = verify_cast<ProcessingInstruction>(*other_node);
  1089. if (this_processing_instruction.target() != other_processing_instruction.target())
  1090. return false;
  1091. if (this_processing_instruction.data() != other_processing_instruction.data())
  1092. return false;
  1093. break;
  1094. }
  1095. default:
  1096. break;
  1097. }
  1098. // A and B have the same number of children.
  1099. size_t this_child_count = child_count();
  1100. size_t other_child_count = other_node->child_count();
  1101. if (this_child_count != other_child_count)
  1102. return false;
  1103. // Each child of A equals the child of B at the identical index.
  1104. // FIXME: This can be made nicer. child_at_index() is O(n).
  1105. for (size_t i = 0; i < this_child_count; ++i) {
  1106. auto* this_child = child_at_index(i);
  1107. auto* other_child = other_node->child_at_index(i);
  1108. VERIFY(this_child);
  1109. VERIFY(other_child);
  1110. if (!this_child->is_equal_node(other_child))
  1111. return false;
  1112. }
  1113. return true;
  1114. }
  1115. // https://dom.spec.whatwg.org/#in-a-document-tree
  1116. bool Node::in_a_document_tree() const
  1117. {
  1118. // An element is in a document tree if its root is a document.
  1119. return root().is_document();
  1120. }
  1121. // https://dom.spec.whatwg.org/#dom-node-getrootnode
  1122. JS::NonnullGCPtr<Node> Node::get_root_node(GetRootNodeOptions const& options)
  1123. {
  1124. // The getRootNode(options) method steps are to return this’s shadow-including root if options["composed"] is true;
  1125. if (options.composed)
  1126. return shadow_including_root();
  1127. // otherwise this’s root.
  1128. return root();
  1129. }
  1130. DeprecatedString Node::debug_description() const
  1131. {
  1132. StringBuilder builder;
  1133. builder.append(node_name().to_lowercase());
  1134. if (is_element()) {
  1135. auto& element = static_cast<DOM::Element const&>(*this);
  1136. if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null())
  1137. builder.appendff("#{}", id);
  1138. for (auto const& class_name : element.class_names())
  1139. builder.appendff(".{}", class_name);
  1140. }
  1141. return builder.to_deprecated_string();
  1142. }
  1143. // https://dom.spec.whatwg.org/#concept-node-length
  1144. size_t Node::length() const
  1145. {
  1146. // 1. If node is a DocumentType or Attr node, then return 0.
  1147. if (is_document_type() || is_attribute())
  1148. return 0;
  1149. // 2. If node is a CharacterData node, then return node’s data’s length.
  1150. if (is_character_data()) {
  1151. auto* character_data_node = verify_cast<CharacterData>(this);
  1152. return character_data_node->data().length();
  1153. }
  1154. // 3. Return the number of node’s children.
  1155. return child_count();
  1156. }
  1157. Painting::Paintable const* Node::paintable() const
  1158. {
  1159. if (!layout_node())
  1160. return nullptr;
  1161. return layout_node()->paintable();
  1162. }
  1163. Painting::PaintableBox const* Node::paint_box() const
  1164. {
  1165. if (!layout_node())
  1166. return nullptr;
  1167. if (!layout_node()->is_box())
  1168. return nullptr;
  1169. return static_cast<Layout::Box const&>(*layout_node()).paint_box();
  1170. }
  1171. // https://dom.spec.whatwg.org/#queue-a-mutation-record
  1172. void Node::queue_mutation_record(DeprecatedFlyString const& type, DeprecatedString attribute_name, DeprecatedString attribute_namespace, DeprecatedString old_value, JS::NonnullGCPtr<NodeList> added_nodes, JS::NonnullGCPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling) const
  1173. {
  1174. // NOTE: We defer garbage collection until the end of the scope, since we can't safely use MutationObserver* as a hashmap key otherwise.
  1175. // FIXME: This is a total hack.
  1176. JS::DeferGC defer_gc(heap());
  1177. // 1. Let interestedObservers be an empty map.
  1178. // mutationObserver -> mappedOldValue
  1179. OrderedHashMap<MutationObserver*, DeprecatedString> interested_observers;
  1180. // 2. Let nodes be the inclusive ancestors of target.
  1181. Vector<JS::Handle<Node const>> nodes;
  1182. nodes.append(JS::make_handle(*this));
  1183. for (auto* parent_node = parent(); parent_node; parent_node = parent_node->parent())
  1184. nodes.append(JS::make_handle(*parent_node));
  1185. // 3. For each node in nodes, and then for each registered of node’s registered observer list:
  1186. for (auto& node : nodes) {
  1187. for (auto& registered_observer : node->m_registered_observer_list) {
  1188. // 1. Let options be registered’s options.
  1189. auto& options = registered_observer.options();
  1190. // 2. If none of the following are true
  1191. // - node is not target and options["subtree"] is false
  1192. // - type is "attributes" and options["attributes"] either does not exist or is false
  1193. // - type is "attributes", options["attributeFilter"] exists, and options["attributeFilter"] does not contain name or namespace is non-null
  1194. // - type is "characterData" and options["characterData"] either does not exist or is false
  1195. // - type is "childList" and options["childList"] is false
  1196. // then:
  1197. if (!(node.ptr() != this && !options.subtree)
  1198. && !(type == MutationType::attributes && (!options.attributes.has_value() || !options.attributes.value()))
  1199. && !(type == MutationType::attributes && options.attribute_filter.has_value() && (!attribute_namespace.is_null() || !options.attribute_filter->contains_slow(attribute_name)))
  1200. && !(type == MutationType::characterData && (!options.character_data.has_value() || !options.character_data.value()))
  1201. && !(type == MutationType::childList && !options.child_list)) {
  1202. // 1. Let mo be registered’s observer.
  1203. auto mutation_observer = registered_observer.observer();
  1204. // 2. If interestedObservers[mo] does not exist, then set interestedObservers[mo] to null.
  1205. if (!interested_observers.contains(mutation_observer))
  1206. interested_observers.set(mutation_observer, {});
  1207. // 3. If either type is "attributes" and options["attributeOldValue"] is true, or type is "characterData" and options["characterDataOldValue"] is true, then set interestedObservers[mo] to oldValue.
  1208. if ((type == MutationType::attributes && options.attribute_old_value.has_value() && options.attribute_old_value.value()) || (type == MutationType::characterData && options.character_data_old_value.has_value() && options.character_data_old_value.value()))
  1209. interested_observers.set(mutation_observer, old_value);
  1210. }
  1211. }
  1212. }
  1213. // 4. For each observer → mappedOldValue of interestedObservers:
  1214. for (auto& interested_observer : interested_observers) {
  1215. // 1. Let record be a new MutationRecord object with its type set to type, target set to target, attributeName set to name, attributeNamespace set to namespace, oldValue set to mappedOldValue,
  1216. // addedNodes set to addedNodes, removedNodes set to removedNodes, previousSibling set to previousSibling, and nextSibling set to nextSibling.
  1217. auto record = MutationRecord::create(realm(), type, *this, added_nodes, removed_nodes, previous_sibling, next_sibling, attribute_name, attribute_namespace, /* mappedOldValue */ interested_observer.value).release_value_but_fixme_should_propagate_errors();
  1218. // 2. Enqueue record to observer’s record queue.
  1219. interested_observer.key->enqueue_record({}, move(record));
  1220. }
  1221. // 5. Queue a mutation observer microtask.
  1222. Bindings::queue_mutation_observer_microtask(document());
  1223. }
  1224. // https://dom.spec.whatwg.org/#queue-a-tree-mutation-record
  1225. void Node::queue_tree_mutation_record(JS::NonnullGCPtr<NodeList> added_nodes, JS::NonnullGCPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling)
  1226. {
  1227. // 1. Assert: either addedNodes or removedNodes is not empty.
  1228. VERIFY(added_nodes->length() > 0 || removed_nodes->length() > 0);
  1229. // 2. Queue a mutation record of "childList" for target with null, null, null, addedNodes, removedNodes, previousSibling, and nextSibling.
  1230. queue_mutation_record(MutationType::childList, {}, {}, {}, move(added_nodes), move(removed_nodes), previous_sibling, next_sibling);
  1231. }
  1232. void Node::append_child_impl(JS::NonnullGCPtr<Node> node)
  1233. {
  1234. VERIFY(!node->m_parent);
  1235. if (!is_child_allowed(*node))
  1236. return;
  1237. if (m_last_child)
  1238. m_last_child->m_next_sibling = node.ptr();
  1239. node->m_previous_sibling = m_last_child;
  1240. node->m_parent = this;
  1241. m_last_child = node.ptr();
  1242. if (!m_first_child)
  1243. m_first_child = m_last_child;
  1244. }
  1245. void Node::insert_before_impl(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child)
  1246. {
  1247. if (!child)
  1248. return append_child_impl(move(node));
  1249. VERIFY(!node->m_parent);
  1250. VERIFY(child->parent() == this);
  1251. node->m_previous_sibling = child->m_previous_sibling;
  1252. node->m_next_sibling = child;
  1253. if (child->m_previous_sibling)
  1254. child->m_previous_sibling->m_next_sibling = node;
  1255. if (m_first_child == child)
  1256. m_first_child = node;
  1257. child->m_previous_sibling = node;
  1258. node->m_parent = this;
  1259. }
  1260. void Node::remove_child_impl(JS::NonnullGCPtr<Node> node)
  1261. {
  1262. VERIFY(node->m_parent.ptr() == this);
  1263. if (m_first_child == node)
  1264. m_first_child = node->m_next_sibling;
  1265. if (m_last_child == node)
  1266. m_last_child = node->m_previous_sibling;
  1267. if (node->m_next_sibling)
  1268. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  1269. if (node->m_previous_sibling)
  1270. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  1271. node->m_next_sibling = nullptr;
  1272. node->m_previous_sibling = nullptr;
  1273. node->m_parent = nullptr;
  1274. }
  1275. bool Node::is_ancestor_of(Node const& other) const
  1276. {
  1277. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  1278. if (ancestor == this)
  1279. return true;
  1280. }
  1281. return false;
  1282. }
  1283. bool Node::is_inclusive_ancestor_of(Node const& other) const
  1284. {
  1285. return &other == this || is_ancestor_of(other);
  1286. }
  1287. bool Node::is_descendant_of(Node const& other) const
  1288. {
  1289. return other.is_ancestor_of(*this);
  1290. }
  1291. bool Node::is_inclusive_descendant_of(Node const& other) const
  1292. {
  1293. return other.is_inclusive_ancestor_of(*this);
  1294. }
  1295. // https://dom.spec.whatwg.org/#concept-tree-following
  1296. bool Node::is_following(Node const& other) const
  1297. {
  1298. // An object A is following an object B if A and B are in the same tree and A comes after B in tree order.
  1299. for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
  1300. if (node == &other)
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. void Node::build_accessibility_tree(AccessibilityTreeNode& parent)
  1306. {
  1307. if (is_uninteresting_whitespace_node())
  1308. return;
  1309. if (is_document()) {
  1310. auto* document = static_cast<DOM::Document*>(this);
  1311. auto* document_element = document->document_element();
  1312. if (document_element) {
  1313. parent.set_value(document_element);
  1314. if (document_element->has_child_nodes())
  1315. document_element->for_each_child([&parent](DOM::Node& child) {
  1316. child.build_accessibility_tree(parent);
  1317. });
  1318. }
  1319. } else if (is_element()) {
  1320. auto const* element = static_cast<DOM::Element const*>(this);
  1321. if (is<HTML::HTMLScriptElement>(element) || is<HTML::HTMLStyleElement>(element))
  1322. return;
  1323. if (element->include_in_accessibility_tree()) {
  1324. auto current_node = AccessibilityTreeNode::create(&document(), this).release_value_but_fixme_should_propagate_errors();
  1325. parent.append_child(current_node);
  1326. if (has_child_nodes()) {
  1327. for_each_child([&current_node](DOM::Node& child) {
  1328. child.build_accessibility_tree(*current_node);
  1329. });
  1330. }
  1331. } else if (has_child_nodes()) {
  1332. for_each_child([&parent](DOM::Node& child) {
  1333. child.build_accessibility_tree(parent);
  1334. });
  1335. }
  1336. } else if (is_text()) {
  1337. parent.append_child(AccessibilityTreeNode::create(&document(), this).release_value_but_fixme_should_propagate_errors());
  1338. if (has_child_nodes()) {
  1339. for_each_child([&parent](DOM::Node& child) {
  1340. child.build_accessibility_tree(parent);
  1341. });
  1342. }
  1343. }
  1344. }
  1345. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_te
  1346. ErrorOr<String> Node::name_or_description(NameOrDescription target, Document const& document, HashTable<i32>& visited_nodes) const
  1347. {
  1348. // The text alternative for a given element is computed as follows:
  1349. // 1. Set the root node to the given element, the current node to the root node, and the total accumulated text to the empty string (""). If the root node's role prohibits naming, return the empty string ("").
  1350. auto const* root_node = this;
  1351. auto const* current_node = root_node;
  1352. StringBuilder total_accumulated_text;
  1353. visited_nodes.set(id());
  1354. if (is_element()) {
  1355. auto const* element = static_cast<DOM::Element const*>(this);
  1356. // 2. Compute the text alternative for the current node:
  1357. // A. If the current node is hidden and is not directly referenced by aria-labelledby or aria-describedby, nor directly referenced by a native host language text alternative element (e.g. label in HTML) or attribute, return the empty string.
  1358. // FIXME: Check for references
  1359. if (element->aria_hidden() == "true" || !layout_node())
  1360. return String {};
  1361. // B. Otherwise:
  1362. // - if computing a name, and the current node has an aria-labelledby attribute that contains at least one valid IDREF, and the current node is not already part of an aria-labelledby traversal,
  1363. // process its IDREFs in the order they occur:
  1364. // - or, if computing a description, and the current node has an aria-describedby attribute that contains at least one valid IDREF, and the current node is not already part of an aria-describedby traversal,
  1365. // process its IDREFs in the order they occur:
  1366. if ((target == NameOrDescription::Name && Node::first_valid_id(element->aria_labelled_by(), document).has_value())
  1367. || (target == NameOrDescription::Description && Node::first_valid_id(element->aria_described_by(), document).has_value())) {
  1368. // i. Set the accumulated text to the empty string.
  1369. total_accumulated_text.clear();
  1370. Vector<StringView> id_list;
  1371. if (target == NameOrDescription::Name) {
  1372. id_list = element->aria_labelled_by().split_view(Infra::is_ascii_whitespace);
  1373. } else {
  1374. id_list = element->aria_described_by().split_view(Infra::is_ascii_whitespace);
  1375. }
  1376. // ii. For each IDREF:
  1377. for (auto const& id_ref : id_list) {
  1378. auto node = document.get_element_by_id(id_ref);
  1379. if (!node)
  1380. continue;
  1381. if (visited_nodes.contains(node->id()))
  1382. continue;
  1383. // a. Set the current node to the node referenced by the IDREF.
  1384. current_node = node;
  1385. // b. Compute the text alternative of the current node beginning with step 2. Set the result to that text alternative.
  1386. auto result = TRY(node->name_or_description(target, document, visited_nodes));
  1387. // c. Append the result, with a space, to the accumulated text.
  1388. TRY(Node::append_with_space(total_accumulated_text, result));
  1389. }
  1390. // iii. Return the accumulated text.
  1391. return total_accumulated_text.to_string();
  1392. }
  1393. // C. Otherwise, if computing a name, and if the current node has an aria-label attribute whose value is not the empty string, nor, when trimmed of white space, is not the empty string:
  1394. if (target == NameOrDescription::Name && !element->aria_label().is_empty() && !element->aria_label().trim_whitespace().is_empty()) {
  1395. // TODO: - If traversal of the current node is due to recursion and the current node is an embedded control as defined in step 2E, ignore aria-label and skip to rule 2E.
  1396. // - Otherwise, return the value of aria-label.
  1397. return String::from_deprecated_string(element->aria_label());
  1398. }
  1399. // TODO: D. Otherwise, if the current node's native markup provides an attribute (e.g. title) or element (e.g. HTML label) that defines a text alternative,
  1400. // return that alternative in the form of a flat string as defined by the host language, unless the element is marked as presentational (role="presentation" or role="none").
  1401. // TODO: E. Otherwise, if the current node is a control embedded within the label (e.g. the label element in HTML or any element directly referenced by aria-labelledby) for another widget, where the user can adjust the embedded
  1402. // control's value, then include the embedded control as part of the text alternative in the following manner:
  1403. // - If the embedded control has role textbox, return its value.
  1404. // - If the embedded control has role menu button, return the text alternative of the button.
  1405. // - If the embedded control has role combobox or listbox, return the text alternative of the chosen option.
  1406. // - If the embedded control has role range (e.g., a spinbutton or slider):
  1407. // - If the aria-valuetext property is present, return its value,
  1408. // - Otherwise, if the aria-valuenow property is present, return its value,
  1409. // - Otherwise, use the value as specified by a host language attribute.
  1410. // F. Otherwise, if the current node's role allows name from content, or if the current node is referenced by aria-labelledby, aria-describedby, or is a native host language text alternative element (e.g. label in HTML), or is a descendant of a native host language text alternative element:
  1411. auto role = element->role_or_default();
  1412. if (role.has_value() && ARIA::allows_name_from_content(role.value())) {
  1413. // i. Set the accumulated text to the empty string.
  1414. total_accumulated_text.clear();
  1415. // ii. Check for CSS generated textual content associated with the current node and include it in the accumulated text. The CSS :before and :after pseudo elements [CSS2] can provide textual content for elements that have a content model.
  1416. auto before = element->get_pseudo_element_node(CSS::Selector::PseudoElement::Before);
  1417. auto after = element->get_pseudo_element_node(CSS::Selector::PseudoElement::After);
  1418. // - For :before pseudo elements, User agents MUST prepend CSS textual content, without a space, to the textual content of the current node.
  1419. if (before)
  1420. TRY(Node::prepend_without_space(total_accumulated_text, before->computed_values().content().data));
  1421. // - For :after pseudo elements, User agents MUST append CSS textual content, without a space, to the textual content of the current node.
  1422. if (after)
  1423. TRY(Node::append_without_space(total_accumulated_text, after->computed_values().content().data));
  1424. // iii. For each child node of the current node:
  1425. element->for_each_child([&total_accumulated_text, current_node, target, &document, visited_nodes](
  1426. DOM::Node const& child_node) mutable {
  1427. if (visited_nodes.contains(child_node.id()))
  1428. return;
  1429. // a. Set the current node to the child node.
  1430. current_node = &child_node;
  1431. // b. Compute the text alternative of the current node beginning with step 2. Set the result to that text alternative.
  1432. auto result = MUST(current_node->name_or_description(target, document, visited_nodes));
  1433. // c. Append the result to the accumulated text.
  1434. total_accumulated_text.append(result);
  1435. });
  1436. // iv. Return the accumulated text.
  1437. return total_accumulated_text.to_string();
  1438. // Important: Each node in the subtree is consulted only once. If text has been collected from a descendant, but is referenced by another IDREF in some descendant node, then that second, or subsequent, reference is not followed. This is done to avoid infinite loops.
  1439. }
  1440. }
  1441. // G. Otherwise, if the current node is a Text node, return its textual contents.
  1442. if (is_text()) {
  1443. auto const* text_node = static_cast<DOM::Text const*>(this);
  1444. return String::from_deprecated_string(text_node->data());
  1445. }
  1446. // TODO: H. Otherwise, if the current node is a descendant of an element whose Accessible Name or Accessible Description is being computed, and contains descendants, proceed to 2F.i.
  1447. // I. Otherwise, if the current node has a Tooltip attribute, return its value.
  1448. // https://www.w3.org/TR/accname-1.2/#dfn-tooltip-attribute
  1449. // Any host language attribute that would result in a user agent generating a tooltip such as in response to a mouse hover in desktop user agents.
  1450. // FIXME: Support SVG tooltips and CSS tooltips
  1451. if (is<HTML::HTMLElement>(this)) {
  1452. auto const* element = static_cast<HTML::HTMLElement const*>(this);
  1453. auto tooltip = element->title();
  1454. if (!tooltip.is_empty() && !tooltip.is_null())
  1455. return String::from_deprecated_string(tooltip);
  1456. }
  1457. // Append the result of each step above, with a space, to the total accumulated text.
  1458. //
  1459. // After all steps are completed, the total accumulated text is used as the accessible name or accessible description of the element that initiated the computation.
  1460. return total_accumulated_text.to_string();
  1461. }
  1462. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_name
  1463. ErrorOr<String> Node::accessible_name(Document const& document) const
  1464. {
  1465. HashTable<i32> visited_nodes;
  1466. // User agents MUST compute an accessible name using the rules outlined below in the section titled Accessible Name and Description Computation.
  1467. return name_or_description(NameOrDescription::Name, document, visited_nodes);
  1468. }
  1469. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_description
  1470. ErrorOr<String> Node::accessible_description(Document const& document) const
  1471. {
  1472. // If aria-describedby is present, user agents MUST compute the accessible description by concatenating the text alternatives for elements referenced by an aria-describedby attribute on the current element.
  1473. // The text alternatives for the referenced elements are computed using a number of methods, outlined below in the section titled Accessible Name and Description Computation.
  1474. if (is_element()) {
  1475. HashTable<i32> visited_nodes;
  1476. StringBuilder builder;
  1477. auto const* element = static_cast<Element const*>(this);
  1478. auto id_list = element->aria_described_by().split_view(Infra::is_ascii_whitespace);
  1479. for (auto const& id : id_list) {
  1480. if (auto description_element = document.get_element_by_id(id)) {
  1481. auto description = TRY(
  1482. description_element->name_or_description(NameOrDescription::Description, document,
  1483. visited_nodes));
  1484. if (!description.is_empty()) {
  1485. if (builder.is_empty()) {
  1486. builder.append(description);
  1487. } else {
  1488. builder.append(" "sv);
  1489. builder.append(description);
  1490. }
  1491. }
  1492. }
  1493. }
  1494. return builder.to_string();
  1495. }
  1496. return String {};
  1497. }
  1498. Optional<StringView> Node::first_valid_id(DeprecatedString const& value, Document const& document)
  1499. {
  1500. auto id_list = value.split_view(Infra::is_ascii_whitespace);
  1501. for (auto const& id : id_list) {
  1502. if (document.get_element_by_id(id))
  1503. return id;
  1504. }
  1505. return {};
  1506. }
  1507. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_te
  1508. ErrorOr<void> Node::append_without_space(StringBuilder x, StringView const& result)
  1509. {
  1510. // - If X is empty, copy the result to X.
  1511. // - If X is non-empty, copy the result to the end of X.
  1512. TRY(x.try_append(result));
  1513. return {};
  1514. }
  1515. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_te
  1516. ErrorOr<void> Node::append_with_space(StringBuilder x, StringView const& result)
  1517. {
  1518. // - If X is empty, copy the result to X.
  1519. if (x.is_empty()) {
  1520. TRY(x.try_append(result));
  1521. } else {
  1522. // - If X is non-empty, add a space to the end of X and then copy the result to X after the space.
  1523. TRY(x.try_append(" "sv));
  1524. TRY(x.try_append(result));
  1525. }
  1526. return {};
  1527. }
  1528. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_te
  1529. ErrorOr<void> Node::prepend_without_space(StringBuilder x, StringView const& result)
  1530. {
  1531. // - If X is empty, copy the result to X.
  1532. if (x.is_empty()) {
  1533. x.append(result);
  1534. } else {
  1535. // - If X is non-empty, copy the result to the start of X.
  1536. auto temp = TRY(x.to_string());
  1537. x.clear();
  1538. TRY(x.try_append(result));
  1539. TRY(x.try_append(temp));
  1540. }
  1541. return {};
  1542. }
  1543. // https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_te
  1544. ErrorOr<void> Node::prepend_with_space(StringBuilder x, StringView const& result)
  1545. {
  1546. // - If X is empty, copy the result to X.
  1547. if (x.is_empty()) {
  1548. TRY(x.try_append(result));
  1549. } else {
  1550. // - If X is non-empty, copy the result to the start of X, and add a space after the copy.
  1551. auto temp = TRY(x.to_string());
  1552. x.clear();
  1553. TRY(x.try_append(result));
  1554. TRY(x.try_append(" "sv));
  1555. TRY(x.try_append(temp));
  1556. }
  1557. return {};
  1558. }
  1559. }