TreeWalker.cpp 13 KB

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