TreeWalker.cpp 13 KB

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