TreeWalker.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/TreeWalkerPrototype.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/DOM/NodeFilter.h>
  10. #include <LibWeb/DOM/TreeWalker.h>
  11. #include <LibWeb/WebIDL/AbstractOperations.h>
  12. #include <LibWeb/WebIDL/DOMException.h>
  13. namespace Web::DOM {
  14. JS_DEFINE_ALLOCATOR(TreeWalker);
  15. TreeWalker::TreeWalker(Node& root)
  16. : PlatformObject(root.realm())
  17. , m_root(root)
  18. , m_current(root)
  19. {
  20. }
  21. TreeWalker::~TreeWalker() = default;
  22. void TreeWalker::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(TreeWalker);
  26. }
  27. void TreeWalker::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_filter);
  31. visitor.visit(m_root);
  32. visitor.visit(m_current);
  33. }
  34. // https://dom.spec.whatwg.org/#dom-document-createtreewalker
  35. JS::NonnullGCPtr<TreeWalker> TreeWalker::create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter> filter)
  36. {
  37. // 1. Let walker be a new TreeWalker object.
  38. // 2. Set walker’s root and walker’s current to root.
  39. auto& realm = root.realm();
  40. auto walker = realm.heap().allocate<TreeWalker>(realm, root);
  41. // 3. Set walker’s whatToShow to whatToShow.
  42. walker->m_what_to_show = what_to_show;
  43. // 4. Set walker’s filter to filter.
  44. walker->m_filter = filter;
  45. // 5. Return walker.
  46. return walker;
  47. }
  48. // https://dom.spec.whatwg.org/#dom-treewalker-currentnode
  49. JS::NonnullGCPtr<Node> TreeWalker::current_node() const
  50. {
  51. return *m_current;
  52. }
  53. // https://dom.spec.whatwg.org/#dom-treewalker-currentnode
  54. void TreeWalker::set_current_node(Node& node)
  55. {
  56. m_current = node;
  57. }
  58. // https://dom.spec.whatwg.org/#dom-treewalker-parentnode
  59. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::parent_node()
  60. {
  61. // 1. Let node be this’s current.
  62. JS::GCPtr<Node> node = m_current;
  63. // 2. While node is non-null and is not this’s root:
  64. while (node && node != m_root) {
  65. // 1. Set node to node’s parent.
  66. node = node->parent();
  67. // 2. If node is non-null and filtering node within this returns FILTER_ACCEPT,
  68. // then set this’s current to node and return node.
  69. if (node) {
  70. auto result = TRY(filter(*node));
  71. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  72. m_current = *node;
  73. return node;
  74. }
  75. }
  76. }
  77. return nullptr;
  78. }
  79. // https://dom.spec.whatwg.org/#dom-treewalker-firstchild
  80. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::first_child()
  81. {
  82. return traverse_children(ChildTraversalType::First);
  83. }
  84. // https://dom.spec.whatwg.org/#dom-treewalker-lastchild
  85. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::last_child()
  86. {
  87. return traverse_children(ChildTraversalType::Last);
  88. }
  89. // https://dom.spec.whatwg.org/#dom-treewalker-previoussibling
  90. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::previous_sibling()
  91. {
  92. return traverse_siblings(SiblingTraversalType::Previous);
  93. }
  94. // https://dom.spec.whatwg.org/#dom-treewalker-nextsibling
  95. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::next_sibling()
  96. {
  97. return traverse_siblings(SiblingTraversalType::Next);
  98. }
  99. // https://dom.spec.whatwg.org/#dom-treewalker-previousnode
  100. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::previous_node()
  101. {
  102. // 1. Let node be this’s current.
  103. JS::NonnullGCPtr<Node> node = m_current;
  104. // 2. While node is not this’s root:
  105. while (node != m_root) {
  106. // 1. Let sibling be node’s previous sibling.
  107. JS::GCPtr<Node> sibling = node->previous_sibling();
  108. // 2. While sibling is non-null:
  109. while (sibling) {
  110. // 1. Set node to sibling.
  111. node = *sibling;
  112. // 2. Let result be the result of filtering node within this.
  113. auto result = TRY(filter(*node));
  114. // 3. While result is not FILTER_REJECT and node has a child:
  115. while (result != NodeFilter::Result::FILTER_REJECT && node->has_children()) {
  116. // 1. Set node to node’s last child.
  117. node = *node->last_child();
  118. // 2. Set result to the result of filtering node within this.
  119. result = TRY(filter(*node));
  120. }
  121. // 4. If result is FILTER_ACCEPT, then set this’s current to node and return node.
  122. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  123. m_current = node;
  124. return node;
  125. }
  126. // 5. Set sibling to node’s previous sibling.
  127. sibling = node->previous_sibling();
  128. }
  129. // 3. If node is this’s root or node’s parent is null, then return null.
  130. if (node == m_root || !node->parent())
  131. return nullptr;
  132. // 4. Set node to node’s parent.
  133. node = *node->parent();
  134. // 5. If the return value of filtering node within this is FILTER_ACCEPT, then set this’s current to node and return node.
  135. if (TRY(filter(*node)) == NodeFilter::Result::FILTER_ACCEPT) {
  136. m_current = node;
  137. return node;
  138. }
  139. }
  140. // 3. Return null.
  141. return nullptr;
  142. }
  143. // https://dom.spec.whatwg.org/#dom-treewalker-nextnode
  144. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::next_node()
  145. {
  146. // 1. Let node be this’s current.
  147. JS::NonnullGCPtr<Node> node = m_current;
  148. // 2. Let result be FILTER_ACCEPT.
  149. auto result = NodeFilter::Result::FILTER_ACCEPT;
  150. // 3. While true:
  151. while (true) {
  152. // 1. While result is not FILTER_REJECT and node has a child:
  153. while (result != NodeFilter::Result::FILTER_REJECT && node->has_children()) {
  154. // 1. Set node to its first child.
  155. node = *node->first_child();
  156. // 2. Set result to the result of filtering node within this.
  157. result = TRY(filter(*node));
  158. // 3. If result is FILTER_ACCEPT, then set this’s current to node and return node.
  159. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  160. m_current = *node;
  161. return node;
  162. }
  163. }
  164. // 2. Let sibling be null.
  165. JS::GCPtr<Node> sibling = nullptr;
  166. // 3. Let temporary be node.
  167. JS::GCPtr<Node> temporary = node;
  168. // 4. While temporary is non-null:
  169. while (temporary) {
  170. // 1. If temporary is this’s root, then return null.
  171. if (temporary == m_root)
  172. return nullptr;
  173. // 2. Set sibling to temporary’s next sibling.
  174. sibling = temporary->next_sibling();
  175. // 3. If sibling is non-null, then set node to sibling and break.
  176. if (sibling) {
  177. node = *sibling;
  178. break;
  179. }
  180. // 4. Set temporary to temporary’s parent.
  181. temporary = temporary->parent();
  182. // NON-STANDARD: If temporary is null, then return null.
  183. // This prevents us from infinite looping if the current node is not connected.
  184. // Spec bug: https://github.com/whatwg/dom/issues/1102
  185. if (temporary == nullptr) {
  186. return nullptr;
  187. }
  188. }
  189. // 5. Set result to the result of filtering node within this.
  190. result = TRY(filter(*node));
  191. // 6. If result is FILTER_ACCEPT, then set this’s current to node and return node.
  192. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  193. m_current = *node;
  194. return node;
  195. }
  196. }
  197. }
  198. // https://dom.spec.whatwg.org/#concept-node-filter
  199. JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node)
  200. {
  201. // 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException.
  202. if (m_active)
  203. return throw_completion(WebIDL::InvalidStateError::create(realm(), "NodeIterator is already active"_string));
  204. // 2. Let n be node’s nodeType attribute value − 1.
  205. auto n = node.node_type() - 1;
  206. // 3. If the nth bit (where 0 is the least significant bit) of traverser’s whatToShow is not set, then return FILTER_SKIP.
  207. if (!(m_what_to_show & (1u << n)))
  208. return NodeFilter::Result::FILTER_SKIP;
  209. // 4. If traverser’s filter is null, then return FILTER_ACCEPT.
  210. if (!m_filter)
  211. return NodeFilter::Result::FILTER_ACCEPT;
  212. // 5. Set traverser’s active flag.
  213. m_active = true;
  214. // 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ».
  215. // If this throws an exception, then unset traverser’s active flag and rethrow the exception.
  216. auto result = WebIDL::call_user_object_operation(m_filter->callback(), "acceptNode"_string, {}, &node);
  217. if (result.is_abrupt()) {
  218. m_active = false;
  219. return result;
  220. }
  221. // 7. Unset traverser’s active flag.
  222. m_active = false;
  223. // 8. Return result.
  224. auto result_value = TRY(result.value()->to_i32(vm()));
  225. return static_cast<NodeFilter::Result>(result_value);
  226. }
  227. // https://dom.spec.whatwg.org/#concept-traverse-children
  228. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::traverse_children(ChildTraversalType type)
  229. {
  230. // 1. Let node be walker’s current.
  231. JS::GCPtr<Node> node = m_current;
  232. // 2. Set node to node’s first child if type is first, and node’s last child if type is last.
  233. node = type == ChildTraversalType::First ? node->first_child() : node->last_child();
  234. // 3. While node is non-null:
  235. while (node) {
  236. // 1. Let result be the result of filtering node within walker.
  237. auto result = TRY(filter(*node));
  238. // 2. If result is FILTER_ACCEPT, then set walker’s current to node and return node.
  239. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  240. m_current = *node;
  241. return node;
  242. }
  243. // 3. If result is FILTER_SKIP, then:
  244. if (result == NodeFilter::Result::FILTER_SKIP) {
  245. // 1. Let child be node’s first child if type is first, and node’s last child if type is last.
  246. JS::GCPtr<Node> child = type == ChildTraversalType::First ? node->first_child() : node->last_child();
  247. // 2. If child is non-null, then set node to child and continue.
  248. if (child) {
  249. node = child;
  250. continue;
  251. }
  252. }
  253. // 4. While node is non-null:
  254. while (node) {
  255. // 1. Let sibling be node’s next sibling if type is first, and node’s previous sibling if type is last.
  256. JS::GCPtr<Node> sibling = type == ChildTraversalType::First ? node->next_sibling() : node->previous_sibling();
  257. // 2. If sibling is non-null, then set node to sibling and break.
  258. if (sibling) {
  259. node = sibling;
  260. break;
  261. }
  262. // 3. Let parent be node’s parent.
  263. JS::GCPtr<Node> parent = node->parent();
  264. // 4. If parent is null, walker’s root, or walker’s current, then return null.
  265. if (!parent || parent == m_root || parent == m_current)
  266. return nullptr;
  267. // 5. Set node to parent.
  268. node = parent;
  269. }
  270. }
  271. // 4. Return null.
  272. return nullptr;
  273. }
  274. // https://dom.spec.whatwg.org/#concept-traverse-siblings
  275. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::traverse_siblings(SiblingTraversalType type)
  276. {
  277. // 1. Let node be walker’s current.
  278. JS::GCPtr<Node> node = m_current;
  279. // 2. If node is root, then return null.
  280. if (node == m_root)
  281. return nullptr;
  282. // 3. While true:
  283. while (true) {
  284. // 1. Let sibling be node’s next sibling if type is next, and node’s previous sibling if type is previous.
  285. JS::GCPtr<Node> sibling = type == SiblingTraversalType::Next ? node->next_sibling() : node->previous_sibling();
  286. // 2. While sibling is non-null:
  287. while (sibling) {
  288. // 1. Set node to sibling.
  289. node = sibling;
  290. // 2. Let result be the result of filtering node within walker.
  291. auto result = TRY(filter(*node));
  292. // 3. If result is FILTER_ACCEPT, then set walker’s current to node and return node.
  293. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  294. m_current = *node;
  295. return node;
  296. }
  297. // 4. Set sibling to node’s first child if type is next, and node’s last child if type is previous.
  298. sibling = type == SiblingTraversalType::Next ? node->first_child() : node->last_child();
  299. // 5. If result is FILTER_REJECT or sibling is null, then set sibling to node’s next sibling if type is next, and node’s previous sibling if type is previous.
  300. if (result == NodeFilter::Result::FILTER_REJECT || !sibling)
  301. sibling = type == SiblingTraversalType::Next ? node->next_sibling() : node->previous_sibling();
  302. }
  303. // 3. Set node to node’s parent.
  304. node = node->parent();
  305. // 4. If node is null or walker’s root, then return null.
  306. if (!node || node == m_root)
  307. return nullptr;
  308. // 5. If the return value of filtering node within walker is FILTER_ACCEPT, then return null.
  309. if (TRY(filter(*node)) == NodeFilter::Result::FILTER_ACCEPT)
  310. return nullptr;
  311. }
  312. }
  313. }