TreeWalker.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. TreeWalker::TreeWalker(Node& root)
  14. : PlatformObject(root.realm())
  15. , m_root(root)
  16. , m_current(root)
  17. {
  18. }
  19. TreeWalker::~TreeWalker() = default;
  20. JS::ThrowCompletionOr<void> TreeWalker::initialize(JS::Realm& realm)
  21. {
  22. MUST_OR_THROW_OOM(Base::initialize(realm));
  23. set_prototype(&Bindings::ensure_web_prototype<Bindings::TreeWalkerPrototype>(realm, "TreeWalker"));
  24. return {};
  25. }
  26. void TreeWalker::visit_edges(Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(m_filter.ptr());
  30. visitor.visit(m_root.ptr());
  31. visitor.visit(m_current.ptr());
  32. }
  33. // https://dom.spec.whatwg.org/#dom-document-createtreewalker
  34. WebIDL::ExceptionOr<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 = MUST_OR_THROW_OOM(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. }
  182. // 5. Set result to the result of filtering node within this.
  183. result = TRY(filter(*node));
  184. // 6. If result is FILTER_ACCEPT, then set this’s current to node and return node.
  185. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  186. m_current = *node;
  187. return node;
  188. }
  189. }
  190. }
  191. // https://dom.spec.whatwg.org/#concept-node-filter
  192. JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node)
  193. {
  194. // 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException.
  195. if (m_active)
  196. return throw_completion(WebIDL::InvalidStateError::create(realm(), "NodeIterator is already active"));
  197. // 2. Let n be node’s nodeType attribute value − 1.
  198. auto n = node.node_type() - 1;
  199. // 3. If the nth bit (where 0 is the least significant bit) of traverser’s whatToShow is not set, then return FILTER_SKIP.
  200. if (!(m_what_to_show & (1u << n)))
  201. return NodeFilter::Result::FILTER_SKIP;
  202. // 4. If traverser’s filter is null, then return FILTER_ACCEPT.
  203. if (!m_filter)
  204. return NodeFilter::Result::FILTER_ACCEPT;
  205. // 5. Set traverser’s active flag.
  206. m_active = true;
  207. // 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ».
  208. // If this throws an exception, then unset traverser’s active flag and rethrow the exception.
  209. auto result = WebIDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, &node);
  210. if (result.is_abrupt()) {
  211. m_active = false;
  212. return result;
  213. }
  214. // 7. Unset traverser’s active flag.
  215. m_active = false;
  216. // 8. Return result.
  217. auto result_value = TRY(result.value()->to_i32(vm()));
  218. return static_cast<NodeFilter::Result>(result_value);
  219. }
  220. // https://dom.spec.whatwg.org/#concept-traverse-children
  221. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::traverse_children(ChildTraversalType type)
  222. {
  223. // 1. Let node be walker’s current.
  224. JS::GCPtr<Node> node = m_current;
  225. // 2. Set node to node’s first child if type is first, and node’s last child if type is last.
  226. node = type == ChildTraversalType::First ? node->first_child() : node->last_child();
  227. // 3. While node is non-null:
  228. while (node) {
  229. // 1. Let result be the result of filtering node within walker.
  230. auto result = TRY(filter(*node));
  231. // 2. If result is FILTER_ACCEPT, then set walker’s current to node and return node.
  232. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  233. m_current = *node;
  234. return node;
  235. }
  236. // 3. If result is FILTER_SKIP, then:
  237. if (result == NodeFilter::Result::FILTER_SKIP) {
  238. // 1. Let child be node’s first child if type is first, and node’s last child if type is last.
  239. JS::GCPtr<Node> child = type == ChildTraversalType::First ? node->first_child() : node->last_child();
  240. // 2. If child is non-null, then set node to child and continue.
  241. if (child) {
  242. node = child;
  243. continue;
  244. }
  245. }
  246. // 4. While node is non-null:
  247. while (node) {
  248. // 1. Let sibling be node’s next sibling if type is first, and node’s previous sibling if type is last.
  249. JS::GCPtr<Node> sibling = type == ChildTraversalType::First ? node->next_sibling() : node->previous_sibling();
  250. // 2. If sibling is non-null, then set node to sibling and break.
  251. if (sibling) {
  252. node = sibling;
  253. break;
  254. }
  255. // 3. Let parent be node’s parent.
  256. JS::GCPtr<Node> parent = node->parent();
  257. // 4. If parent is null, walker’s root, or walker’s current, then return null.
  258. if (!parent || parent == m_root || parent == m_current)
  259. return nullptr;
  260. // 5. Set node to parent.
  261. node = parent;
  262. }
  263. }
  264. // 4. Return null.
  265. return nullptr;
  266. }
  267. // https://dom.spec.whatwg.org/#concept-traverse-siblings
  268. JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::traverse_siblings(SiblingTraversalType type)
  269. {
  270. // 1. Let node be walker’s current.
  271. JS::GCPtr<Node> node = m_current;
  272. // 2. If node is root, then return null.
  273. if (node == m_root)
  274. return nullptr;
  275. // 3. While true:
  276. while (true) {
  277. // 1. Let sibling be node’s next sibling if type is next, and node’s previous sibling if type is previous.
  278. JS::GCPtr<Node> sibling = type == SiblingTraversalType::Next ? node->next_sibling() : node->previous_sibling();
  279. // 2. While sibling is non-null:
  280. while (sibling) {
  281. // 1. Set node to sibling.
  282. node = sibling;
  283. // 2. Let result be the result of filtering node within walker.
  284. auto result = TRY(filter(*node));
  285. // 3. If result is FILTER_ACCEPT, then set walker’s current to node and return node.
  286. if (result == NodeFilter::Result::FILTER_ACCEPT) {
  287. m_current = *node;
  288. return node;
  289. }
  290. // 4. Set sibling to node’s first child if type is next, and node’s last child if type is previous.
  291. sibling = type == SiblingTraversalType::Next ? node->first_child() : node->last_child();
  292. // 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.
  293. if (result == NodeFilter::Result::FILTER_REJECT || !sibling)
  294. sibling = type == SiblingTraversalType::Next ? node->next_sibling() : node->previous_sibling();
  295. }
  296. // 3. Set node to node’s parent.
  297. node = node->parent();
  298. // 4. If node is null or walker’s root, then return null.
  299. if (!node || node == m_root)
  300. return nullptr;
  301. // 5. If the return value of filtering node within walker is FILTER_ACCEPT, then return null.
  302. if (TRY(filter(*node)) == NodeFilter::Result::FILTER_ACCEPT)
  303. return nullptr;
  304. }
  305. }
  306. }