1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:57:44 +00:00

LibWeb: Make NodeIterator behave like other browser engines

If invoking a NodeFilter ends up deleting a node from the DOM, it's not
enough to only adjust the NodeIterator reference nodes in the
pre-removing steps. We must also adjust the current traversal pointer.

This is not in the spec, but it's how other engines behave, so let's do
the same.

I've encapsulated the Node + before-or-after-flag in a struct called
NodePointer so that we can use the same pre-removing steps for both the
traversal pointer and for the NodeIterator's reference node.

Note that when invoking the NodeFilter, we have to remember the node we
passed to the filter function, so that we can return it if accepted by
the filter.

This gets us another point on Acid3. :^)
This commit is contained in:
Andreas Kling 2022-03-23 00:12:12 +01:00
parent a0b0b29fa1
commit fd7a059e09
2 changed files with 96 additions and 61 deletions

View file

@ -24,8 +24,8 @@ public:
static NonnullRefPtr<NodeIterator> create(Node& root, unsigned what_to_show, RefPtr<NodeFilter>);
NonnullRefPtr<Node> root() { return m_root; }
NonnullRefPtr<Node> reference_node() { return m_reference; }
bool pointer_before_reference_node() const { return m_pointer_before_reference; }
NonnullRefPtr<Node> reference_node() { return m_reference.node; }
bool pointer_before_reference_node() const { return m_reference.is_before_node; }
unsigned what_to_show() const { return m_what_to_show; }
NodeFilter* filter() { return m_filter; }
@ -52,11 +52,21 @@ private:
// https://dom.spec.whatwg.org/#concept-traversal-root
NonnullRefPtr<DOM::Node> m_root;
// https://dom.spec.whatwg.org/#nodeiterator-reference
NonnullRefPtr<DOM::Node> m_reference;
struct NodePointer {
NonnullRefPtr<DOM::Node> node;
// https://dom.spec.whatwg.org/#nodeiterator-pointer-before-reference
bool m_pointer_before_reference { true };
// https://dom.spec.whatwg.org/#nodeiterator-pointer-before-reference
bool is_before_node { true };
};
void run_pre_removing_steps_with_node_pointer(Node&, NodePointer&);
// https://dom.spec.whatwg.org/#nodeiterator-reference
NodePointer m_reference;
// While traversal is ongoing, we keep track of the current node pointer.
// This allows us to adjust it during traversal if calling the filter ends up removing the node from the DOM.
Optional<NodePointer> m_traversal_pointer;
// https://dom.spec.whatwg.org/#concept-traversal-whattoshow
unsigned m_what_to_show { 0 };