Node.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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/StringBuilder.h>
  9. #include <LibJS/AST.h>
  10. #include <LibJS/Runtime/FunctionObject.h>
  11. #include <LibWeb/Bindings/EventWrapper.h>
  12. #include <LibWeb/Bindings/NodeWrapper.h>
  13. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  14. #include <LibWeb/DOM/Comment.h>
  15. #include <LibWeb/DOM/DocumentType.h>
  16. #include <LibWeb/DOM/Element.h>
  17. #include <LibWeb/DOM/ElementFactory.h>
  18. #include <LibWeb/DOM/Event.h>
  19. #include <LibWeb/DOM/EventDispatcher.h>
  20. #include <LibWeb/DOM/EventListener.h>
  21. #include <LibWeb/DOM/Node.h>
  22. #include <LibWeb/DOM/ProcessingInstruction.h>
  23. #include <LibWeb/DOM/ShadowRoot.h>
  24. #include <LibWeb/HTML/HTMLAnchorElement.h>
  25. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  26. #include <LibWeb/Layout/Node.h>
  27. #include <LibWeb/Layout/TextNode.h>
  28. #include <LibWeb/Origin.h>
  29. namespace Web::DOM {
  30. Node::Node(Document& document, NodeType type)
  31. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  32. , m_document(&document)
  33. , m_type(type)
  34. {
  35. if (!is_document())
  36. m_document->ref_from_node({});
  37. }
  38. Node::~Node()
  39. {
  40. VERIFY(m_deletion_has_begun);
  41. if (layout_node() && layout_node()->parent())
  42. layout_node()->parent()->remove_child(*layout_node());
  43. if (!is_document())
  44. m_document->unref_from_node({});
  45. }
  46. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  47. {
  48. for (auto* node = this; node; node = node->parent()) {
  49. if (is<HTML::HTMLAnchorElement>(*node) && verify_cast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  50. return verify_cast<HTML::HTMLAnchorElement>(node);
  51. }
  52. return nullptr;
  53. }
  54. const HTML::HTMLElement* Node::enclosing_html_element() const
  55. {
  56. return first_ancestor_of_type<HTML::HTMLElement>();
  57. }
  58. const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(const FlyString& attribute) const
  59. {
  60. for (auto* node = this; node; node = node->parent()) {
  61. if (is<HTML::HTMLElement>(*node) && verify_cast<HTML::HTMLElement>(*node).has_attribute(attribute))
  62. return verify_cast<HTML::HTMLElement>(node);
  63. }
  64. return nullptr;
  65. }
  66. String Node::text_content() const
  67. {
  68. StringBuilder builder;
  69. for (auto* child = first_child(); child; child = child->next_sibling()) {
  70. builder.append(child->text_content());
  71. }
  72. return builder.to_string();
  73. }
  74. void Node::set_text_content(const String& content)
  75. {
  76. if (is_text()) {
  77. verify_cast<Text>(this)->set_data(content);
  78. } else {
  79. remove_all_children();
  80. append_child(document().create_text_node(content));
  81. }
  82. set_needs_style_update(true);
  83. document().invalidate_layout();
  84. }
  85. RefPtr<Layout::Node> Node::create_layout_node()
  86. {
  87. return nullptr;
  88. }
  89. void Node::invalidate_style()
  90. {
  91. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  92. element.set_needs_style_update(true);
  93. return IterationDecision::Continue;
  94. });
  95. document().schedule_style_update();
  96. }
  97. bool Node::is_link() const
  98. {
  99. return enclosing_link_element();
  100. }
  101. bool Node::dispatch_event(NonnullRefPtr<Event> event)
  102. {
  103. return EventDispatcher::dispatch(*this, event);
  104. }
  105. String Node::child_text_content() const
  106. {
  107. if (!is<ParentNode>(*this))
  108. return String::empty();
  109. StringBuilder builder;
  110. verify_cast<ParentNode>(*this).for_each_child([&](auto& child) {
  111. if (is<Text>(child))
  112. builder.append(verify_cast<Text>(child).text_content());
  113. });
  114. return builder.build();
  115. }
  116. Node* Node::root()
  117. {
  118. Node* root = this;
  119. while (root->parent())
  120. root = root->parent();
  121. return root;
  122. }
  123. Node* Node::shadow_including_root()
  124. {
  125. auto node_root = root();
  126. if (is<ShadowRoot>(node_root))
  127. return verify_cast<ShadowRoot>(node_root)->host()->shadow_including_root();
  128. return node_root;
  129. }
  130. bool Node::is_connected() const
  131. {
  132. return shadow_including_root() && shadow_including_root()->is_document();
  133. }
  134. Element* Node::parent_element()
  135. {
  136. if (!parent() || !is<Element>(parent()))
  137. return nullptr;
  138. return verify_cast<Element>(parent());
  139. }
  140. const Element* Node::parent_element() const
  141. {
  142. if (!parent() || !is<Element>(parent()))
  143. return nullptr;
  144. return verify_cast<Element>(parent());
  145. }
  146. // https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
  147. ExceptionOr<void> Node::ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const
  148. {
  149. if (!is<Document>(this) && !is<DocumentFragment>(this) && !is<Element>(this))
  150. return DOM::HierarchyRequestError::create("Can only insert into a document, document fragment or element");
  151. if (node->is_host_including_inclusive_ancestor_of(*this))
  152. return DOM::HierarchyRequestError::create("New node is an ancestor of this node");
  153. if (child && child->parent() != this)
  154. return DOM::NotFoundError::create("This node is not the parent of the given child");
  155. // FIXME: All the following "Invalid node type for insertion" messages could be more descriptive.
  156. if (!is<DocumentFragment>(*node) && !is<DocumentType>(*node) && !is<Element>(*node) && !is<Text>(*node) && !is<Comment>(*node) && !is<ProcessingInstruction>(*node))
  157. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  158. if ((is<Text>(*node) && is<Document>(this)) || (is<DocumentType>(*node) && !is<Document>(this)))
  159. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  160. if (is<Document>(this)) {
  161. if (is<DocumentFragment>(*node)) {
  162. auto node_element_child_count = verify_cast<DocumentFragment>(*node).child_element_count();
  163. if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
  164. || (node_element_child_count == 1 && (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) /* FIXME: or child is non-null and a doctype is following child. */))) {
  165. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  166. }
  167. } else if (is<Element>(*node)) {
  168. if (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) /* FIXME: or child is non-null and a doctype is following child. */)
  169. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  170. } else if (is<DocumentType>(*node)) {
  171. if (has_child_of_type<DocumentType>() /* FIXME: or child is non-null and an element is preceding child */ || (!child && has_child_of_type<Element>()))
  172. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  173. }
  174. }
  175. return {};
  176. }
  177. // https://dom.spec.whatwg.org/#concept-node-insert
  178. void Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers)
  179. {
  180. NonnullRefPtrVector<Node> nodes;
  181. if (is<DocumentFragment>(*node))
  182. nodes = verify_cast<DocumentFragment>(*node).child_nodes();
  183. else
  184. nodes.append(node);
  185. auto count = nodes.size();
  186. if (count == 0)
  187. return;
  188. if (is<DocumentFragment>(*node)) {
  189. node->remove_all_children(true);
  190. // FIXME: Queue a tree mutation record for node with « », nodes, null, and null.
  191. }
  192. if (child) {
  193. // FIXME: For each live range whose start node is parent and start offset is greater than child’s index, increase its start offset by count.
  194. // FIXME: For each live range whose end node is parent and end offset is greater than child’s index, increase its end offset by count.
  195. }
  196. // FIXME: Let previousSibling be child’s previous sibling or parent’s last child if child is null. (Currently unused so not included)
  197. for (auto& node_to_insert : nodes) { // FIXME: In tree order
  198. document().adopt_node(node_to_insert);
  199. if (!child)
  200. TreeNode<Node>::append_child(node_to_insert);
  201. else
  202. TreeNode<Node>::insert_before(node_to_insert, child);
  203. // FIXME: If parent is a shadow host and node is a slottable, then assign a slot for node.
  204. // FIXME: 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.
  205. // FIXME: Run assign slottables for a tree with node’s root.
  206. // FIXME: This should be shadow-including.
  207. node_to_insert.for_each_in_inclusive_subtree([&](Node& inclusive_descendant) {
  208. inclusive_descendant.inserted();
  209. if (inclusive_descendant.is_connected()) {
  210. // FIXME: If inclusiveDescendant is custom, then enqueue a custom element callback reaction with inclusiveDescendant,
  211. // callback name "connectedCallback", and an empty argument list.
  212. // FIXME: Otherwise, try to upgrade inclusiveDescendant.
  213. }
  214. return IterationDecision::Continue;
  215. });
  216. }
  217. if (!suppress_observers) {
  218. // FIXME: queue a tree mutation record for parent with nodes, « », previousSibling, and child.
  219. }
  220. children_changed();
  221. }
  222. // https://dom.spec.whatwg.org/#concept-node-pre-insert
  223. ExceptionOr<NonnullRefPtr<Node>> Node::pre_insert(NonnullRefPtr<Node> node, RefPtr<Node> child)
  224. {
  225. auto validity_result = ensure_pre_insertion_validity(node, child);
  226. if (validity_result.is_exception())
  227. return validity_result.exception();
  228. auto reference_child = child;
  229. if (reference_child == node)
  230. reference_child = node->next_sibling();
  231. insert_before(node, reference_child);
  232. return node;
  233. }
  234. // https://dom.spec.whatwg.org/#concept-node-pre-remove
  235. ExceptionOr<NonnullRefPtr<Node>> Node::pre_remove(NonnullRefPtr<Node> child)
  236. {
  237. if (child->parent() != this)
  238. return DOM::NotFoundError::create("Child does not belong to this node");
  239. child->remove();
  240. return child;
  241. }
  242. // https://dom.spec.whatwg.org/#concept-node-append
  243. ExceptionOr<NonnullRefPtr<Node>> Node::append_child(NonnullRefPtr<Node> node)
  244. {
  245. return pre_insert(node, nullptr);
  246. }
  247. // https://dom.spec.whatwg.org/#concept-node-remove
  248. void Node::remove(bool suppress_observers)
  249. {
  250. auto* parent = TreeNode<Node>::parent();
  251. VERIFY(parent);
  252. // FIXME: Let index be node’s index. (Currently unused so not included)
  253. // FIXME: For each live range whose start node is an inclusive descendant of node, set its start to (parent, index).
  254. // FIXME: For each live range whose end node is an inclusive descendant of node, set its end to (parent, index).
  255. // FIXME: For each live range whose start node is parent and start offset is greater than index, decrease its start offset by 1.
  256. // FIXME: For each live range whose end node is parent and end offset is greater than index, decrease its end offset by 1.
  257. // FIXME: 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.
  258. // FIXME: Let oldPreviousSibling be node’s previous sibling. (Currently unused so not included)
  259. // FIXME: Let oldNextSibling be node’s next sibling. (Currently unused so not included)
  260. parent->remove_child(*this);
  261. // FIXME: If node is assigned, then run assign slottables for node’s assigned slot.
  262. // FIXME: 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.
  263. // FIXME: If node has an inclusive descendant that is a slot, then:
  264. // Run assign slottables for a tree with parent’s root.
  265. // Run assign slottables for a tree with node.
  266. removed_from(parent);
  267. // FIXME: Let isParentConnected be parent’s connected. (Currently unused so not included)
  268. // FIXME: If node is custom and isParentConnected is true, then enqueue a custom element callback reaction with node,
  269. // callback name "disconnectedCallback", and an empty argument list.
  270. // FIXME: This should be shadow-including.
  271. for_each_in_subtree([&](Node& descendant) {
  272. descendant.removed_from(nullptr);
  273. // FIXME: If descendant is custom and isParentConnected is true, then enqueue a custom element callback reaction with descendant,
  274. // callback name "disconnectedCallback", and an empty argument list.
  275. return IterationDecision::Continue;
  276. });
  277. if (!suppress_observers) {
  278. // FIXME: queue a tree mutation record for parent with « », « node », oldPreviousSibling, and oldNextSibling.
  279. }
  280. parent->children_changed();
  281. }
  282. // https://dom.spec.whatwg.org/#concept-node-replace
  283. ExceptionOr<NonnullRefPtr<Node>> Node::replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child)
  284. {
  285. // NOTE: This differs slightly from ensure_pre_insertion_validity.
  286. if (!is<Document>(this) && !is<DocumentFragment>(this) && !is<Element>(this))
  287. return DOM::HierarchyRequestError::create("Can only insert into a document, document fragment or element");
  288. if (node->is_host_including_inclusive_ancestor_of(*this))
  289. return DOM::HierarchyRequestError::create("New node is an ancestor of this node");
  290. if (child->parent() != this)
  291. return DOM::NotFoundError::create("This node is not the parent of the given child");
  292. // FIXME: All the following "Invalid node type for insertion" messages could be more descriptive.
  293. if (!is<DocumentFragment>(*node) && !is<DocumentType>(*node) && !is<Element>(*node) && !is<Text>(*node) && !is<Comment>(*node) && !is<ProcessingInstruction>(*node))
  294. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  295. if ((is<Text>(*node) && is<Document>(this)) || (is<DocumentType>(*node) && !is<Document>(this)))
  296. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  297. if (is<Document>(this)) {
  298. if (is<DocumentFragment>(*node)) {
  299. auto node_element_child_count = verify_cast<DocumentFragment>(*node).child_element_count();
  300. if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
  301. || (node_element_child_count == 1 && (first_child_of_type<Element>() != child /* FIXME: or a doctype is following child. */))) {
  302. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  303. }
  304. } else if (is<Element>(*node)) {
  305. if (first_child_of_type<Element>() != child /* FIXME: or a doctype is following child. */)
  306. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  307. } else if (is<DocumentType>(*node)) {
  308. if (first_child_of_type<DocumentType>() != node /* FIXME: or an element is preceding child */)
  309. return DOM::HierarchyRequestError::create("Invalid node type for insertion");
  310. }
  311. }
  312. auto reference_child = child->next_sibling();
  313. if (reference_child == node)
  314. reference_child = node->next_sibling();
  315. // FIXME: Let previousSibling be child’s previous sibling. (Currently unused so not included)
  316. // FIXME: Let removedNodes be the empty set. (Currently unused so not included)
  317. if (child->parent()) {
  318. // FIXME: Set removedNodes to « child ».
  319. child->remove(true);
  320. }
  321. // FIXME: Let nodes be node’s children if node is a DocumentFragment node; otherwise « node ». (Currently unused so not included)
  322. insert_before(node, reference_child, true);
  323. // FIXME: Queue a tree mutation record for parent with nodes, removedNodes, previousSibling, and referenceChild.
  324. return child;
  325. }
  326. // https://dom.spec.whatwg.org/#concept-node-clone
  327. NonnullRefPtr<Node> Node::clone_node(Document* document, bool clone_children) const
  328. {
  329. if (!document)
  330. document = m_document;
  331. RefPtr<Node> copy;
  332. if (is<Element>(this)) {
  333. auto& element = *verify_cast<Element>(this);
  334. 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 */);
  335. element.for_each_attribute([&](auto& name, auto& value) {
  336. element_copy->set_attribute(name, value);
  337. });
  338. copy = move(element_copy);
  339. } else if (is<Document>(this)) {
  340. auto document_ = verify_cast<Document>(this);
  341. auto document_copy = Document::create(document_->url());
  342. document_copy->set_encoding(document_->encoding());
  343. document_copy->set_content_type(document_->content_type());
  344. document_copy->set_origin(document_->origin());
  345. document_copy->set_quirks_mode(document_->mode());
  346. // FIXME: Set type ("xml" or "html")
  347. copy = move(document_copy);
  348. } else if (is<DocumentType>(this)) {
  349. auto document_type = verify_cast<DocumentType>(this);
  350. auto document_type_copy = adopt_ref(*new DocumentType(*document));
  351. document_type_copy->set_name(document_type->name());
  352. document_type_copy->set_public_id(document_type->public_id());
  353. document_type_copy->set_system_id(document_type->system_id());
  354. copy = move(document_type_copy);
  355. } else if (is<Text>(this)) {
  356. auto text = verify_cast<Text>(this);
  357. auto text_copy = adopt_ref(*new Text(*document, text->data()));
  358. copy = move(text_copy);
  359. } else if (is<Comment>(this)) {
  360. auto comment = verify_cast<Comment>(this);
  361. auto comment_copy = adopt_ref(*new Comment(*document, comment->data()));
  362. copy = move(comment_copy);
  363. } else if (is<ProcessingInstruction>(this)) {
  364. auto processing_instruction = verify_cast<ProcessingInstruction>(this);
  365. auto processing_instruction_copy = adopt_ref(*new ProcessingInstruction(*document, processing_instruction->data(), processing_instruction->target()));
  366. copy = move(processing_instruction_copy);
  367. } else if (is<DocumentFragment>(this)) {
  368. auto document_fragment_copy = adopt_ref(*new DocumentFragment(*document));
  369. copy = move(document_fragment_copy);
  370. } else {
  371. dbgln("clone_node() not implemented for NodeType {}", (u16)m_type);
  372. TODO();
  373. }
  374. // 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.
  375. // FIXME: 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.
  376. if (clone_children) {
  377. for_each_child([&](auto& child) {
  378. copy->append_child(child.clone_node(document, true));
  379. });
  380. }
  381. return copy.release_nonnull();
  382. }
  383. // https://dom.spec.whatwg.org/#dom-node-clonenode
  384. ExceptionOr<NonnullRefPtr<Node>> Node::clone_node_binding(bool deep) const
  385. {
  386. if (is<ShadowRoot>(*this))
  387. return NotSupportedError::create("Cannot clone shadow root");
  388. return clone_node(nullptr, deep);
  389. }
  390. void Node::set_document(Badge<Document>, Document& document)
  391. {
  392. if (m_document == &document)
  393. return;
  394. document.ref_from_node({});
  395. m_document->unref_from_node({});
  396. m_document = &document;
  397. }
  398. bool Node::is_editable() const
  399. {
  400. return parent() && parent()->is_editable();
  401. }
  402. JS::Object* Node::create_wrapper(JS::GlobalObject& global_object)
  403. {
  404. return wrap(global_object, *this);
  405. }
  406. void Node::removed_last_ref()
  407. {
  408. if (is<Document>(*this)) {
  409. verify_cast<Document>(*this).removed_last_ref();
  410. return;
  411. }
  412. m_deletion_has_begun = true;
  413. delete this;
  414. }
  415. void Node::set_layout_node(Badge<Layout::Node>, Layout::Node* layout_node) const
  416. {
  417. if (layout_node)
  418. m_layout_node = layout_node->make_weak_ptr();
  419. else
  420. m_layout_node = nullptr;
  421. }
  422. EventTarget* Node::get_parent(const Event&)
  423. {
  424. // FIXME: returns the node’s assigned slot, if node is assigned, and node’s parent otherwise.
  425. return parent();
  426. }
  427. void Node::set_needs_style_update(bool value)
  428. {
  429. if (m_needs_style_update == value)
  430. return;
  431. m_needs_style_update = value;
  432. if (m_needs_style_update) {
  433. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  434. //dbgln("{}", ancestor->node_name());
  435. ancestor->m_child_needs_style_update = true;
  436. }
  437. document().schedule_style_update();
  438. }
  439. }
  440. void Node::inserted()
  441. {
  442. set_needs_style_update(true);
  443. }
  444. ParentNode* Node::parent_or_shadow_host()
  445. {
  446. if (is<ShadowRoot>(*this))
  447. return verify_cast<ShadowRoot>(*this).host();
  448. return verify_cast<ParentNode>(parent());
  449. }
  450. NonnullRefPtrVector<Node> Node::child_nodes() const
  451. {
  452. NonnullRefPtrVector<Node> nodes;
  453. for_each_child([&](auto& child) {
  454. nodes.append(child);
  455. });
  456. return nodes;
  457. }
  458. void Node::remove_all_children(bool suppress_observers)
  459. {
  460. while (RefPtr<Node> child = first_child())
  461. child->remove(suppress_observers);
  462. }
  463. // https://dom.spec.whatwg.org/#dom-node-comparedocumentposition
  464. u16 Node::compare_document_position(RefPtr<Node> other)
  465. {
  466. enum Position : u16 {
  467. DOCUMENT_POSITION_EQUAL = 0,
  468. DOCUMENT_POSITION_DISCONNECTED = 1,
  469. DOCUMENT_POSITION_PRECEDING = 2,
  470. DOCUMENT_POSITION_FOLLOWING = 4,
  471. DOCUMENT_POSITION_CONTAINS = 8,
  472. DOCUMENT_POSITION_CONTAINED_BY = 16,
  473. DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32,
  474. };
  475. if (this == other)
  476. return DOCUMENT_POSITION_EQUAL;
  477. Node* node1 = other.ptr();
  478. Node* node2 = this;
  479. // FIXME: Once LibWeb supports attribute nodes fix to follow the specification.
  480. VERIFY(node1->type() != NodeType::ATTRIBUTE_NODE && node2->type() != NodeType::ATTRIBUTE_NODE);
  481. if ((node1 == nullptr || node2 == nullptr) || (node1->root() != node2->root()))
  482. return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | (node1 > node2 ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING);
  483. if (node1->is_ancestor_of(*node2))
  484. return DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING;
  485. if (node2->is_ancestor_of(*node1))
  486. return DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING;
  487. if (node1->is_before(*node2))
  488. return DOCUMENT_POSITION_PRECEDING;
  489. else
  490. return DOCUMENT_POSITION_FOLLOWING;
  491. }
  492. // https://dom.spec.whatwg.org/#concept-tree-host-including-inclusive-ancestor
  493. bool Node::is_host_including_inclusive_ancestor_of(const Node& other) const
  494. {
  495. return is_inclusive_ancestor_of(other) || (is<DocumentFragment>(other.root()) && verify_cast<DocumentFragment>(other.root())->host() && is_inclusive_ancestor_of(*verify_cast<DocumentFragment>(other.root())->host().ptr()));
  496. }
  497. // https://dom.spec.whatwg.org/#dom-node-ownerdocument
  498. RefPtr<Document> Node::owner_document() const
  499. {
  500. if (is_document())
  501. return nullptr;
  502. return m_document;
  503. }
  504. void Node::serialize_tree_as_json(JsonObjectSerializer<StringBuilder>& object) const
  505. {
  506. object.add("name", node_name().view());
  507. if (is_document()) {
  508. object.add("type", "document");
  509. } else if (is_element()) {
  510. object.add("type", "element");
  511. auto element = static_cast<DOM::Element const*>(this);
  512. if (element->has_attributes()) {
  513. auto attributes = object.add_object("attributes");
  514. element->for_each_attribute([&attributes](auto& name, auto& value) {
  515. attributes.add(name, value);
  516. });
  517. }
  518. } else if (is_text()) {
  519. object.add("type", "text");
  520. auto text_node = static_cast<DOM::Text const*>(this);
  521. object.add("text", text_node->data());
  522. }
  523. if (has_child_nodes()) {
  524. auto children = object.add_array("children");
  525. for_each_child([&children](DOM::Node& child) {
  526. JsonObjectSerializer<StringBuilder> child_object = children.add_object();
  527. child.serialize_tree_as_json(child_object);
  528. });
  529. }
  530. }
  531. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-n-noscript
  532. bool Node::is_scripting_disabled() const
  533. {
  534. // FIXME: or when scripting is disabled for its relevant settings object.
  535. return !document().browsing_context();
  536. }
  537. }