TreeWalker.cpp 13 KB

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