TreeWalker.cpp 13 KB

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