From 474493471326e59978d3e2ece186d12851aaa616 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Wed, 11 Mar 2020 13:02:09 +0100 Subject: [PATCH] Fix #78221: DOMNode::normalize() doesn't remove empty text nodes If a text node is not followed by another text node, we remove it, if its textContent is empty. --- ext/dom/php_dom.c | 8 ++++++++ ext/dom/tests/bug78221.phpt | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 ext/dom/tests/bug78221.phpt diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c index 72ae3c3ffea36..ed67f047fa875 100644 --- a/ext/dom/php_dom.c +++ b/ext/dom/php_dom.c @@ -1383,6 +1383,14 @@ void dom_normalize (xmlNodePtr nodep) break; } } + strContent = xmlNodeGetContent(child); + if (*strContent == '\0') { + nextp = child->next; + xmlUnlinkNode(child); + php_libxml_node_free_resource(child); + child = nextp; + continue; + } break; case XML_ELEMENT_NODE: dom_normalize (child); diff --git a/ext/dom/tests/bug78221.phpt b/ext/dom/tests/bug78221.phpt new file mode 100644 index 0000000000000..a9bf50d98ea02 --- /dev/null +++ b/ext/dom/tests/bug78221.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #78221 (DOMNode::normalize() doesn't remove empty text nodes) +--SKIPIF-- + +--FILE-- +loadHTML('

foo

'); +$p = $doc->getElementById('x'); +$p->childNodes[0]->textContent = ''; +$p->normalize(); +var_dump($p->childNodes->length); +?> +--EXPECT-- +int(0)