From ab0dc83d2873d7c0e6563e08980a153bdae0a5e3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 20 Nov 2024 11:31:59 +0100 Subject: [PATCH] LibWeb: Make Node.normalize() ignore CDATASection nodes We hadn't modeled the "exclusive text node" concept correctly. --- Libraries/LibWeb/DOM/Node.cpp | 12 +-- Libraries/LibWeb/DOM/Node.h | 1 + .../wpt-import/dom/nodes/Node-normalize.txt | 14 ++++ .../wpt-import/dom/nodes/Node-normalize.html | 83 +++++++++++++++++++ 4 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-normalize.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-normalize.html diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index 8f7109dc50e..a08979e5950 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -235,10 +235,7 @@ WebIDL::ExceptionOr Node::normalize() Vector nodes; auto* current_node = node.previous_sibling(); - while (current_node) { - if (!current_node->is_text()) - break; - + while (current_node && current_node->is_exclusive_text()) { nodes.append(static_cast(current_node)); current_node = current_node->previous_sibling(); } @@ -247,10 +244,7 @@ WebIDL::ExceptionOr Node::normalize() nodes.reverse(); current_node = node.next_sibling(); - while (current_node) { - if (!current_node->is_text()) - break; - + while (current_node && current_node->is_exclusive_text()) { nodes.append(static_cast(current_node)); current_node = current_node->next_sibling(); } @@ -291,7 +285,7 @@ WebIDL::ExceptionOr Node::normalize() auto* current_node = node.next_sibling(); // 6. While currentNode is an exclusive Text node: - while (current_node && is(*current_node)) { + while (current_node && current_node->is_exclusive_text()) { // 1. For each live range whose start node is currentNode, add length to its start offset and set its start node to node. for (auto& range : Range::live_ranges()) { if (range->start_container() == current_node) diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index ef4e099a474..3399b878671 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -111,6 +111,7 @@ public: NodeType type() const { return m_type; } bool is_element() const { return type() == NodeType::ELEMENT_NODE; } bool is_text() const { return type() == NodeType::TEXT_NODE || type() == NodeType::CDATA_SECTION_NODE; } + bool is_exclusive_text() const { return type() == NodeType::TEXT_NODE; } bool is_document() const { return type() == NodeType::DOCUMENT_NODE; } bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; } bool is_comment() const { return type() == NodeType::COMMENT_NODE; } diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-normalize.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-normalize.txt new file mode 100644 index 00000000000..fe6c72eb73f --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-normalize.txt @@ -0,0 +1,14 @@ +Summary + +Harness status: OK + +Rerun + +Found 4 tests + +4 Pass +Details +Result Test Name MessagePass Node.normalize() +Pass Empty text nodes separated by a non-empty text node +Pass Empty text nodes +Pass Non-text nodes with empty textContent values. \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-normalize.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-normalize.html new file mode 100644 index 00000000000..cf2fc2ccd1d --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-normalize.html @@ -0,0 +1,83 @@ + +Node.normalize() + + +
+