]> granicus.if.org Git - php/commitdiff
Fix #78221: DOMNode::normalize() doesn't remove empty text nodes
authorChristoph M. Becker <cmbecker69@gmx.de>
Wed, 11 Mar 2020 12:02:09 +0000 (13:02 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Tue, 7 Apr 2020 11:04:14 +0000 (13:04 +0200)
If a text node is not followed by another text node, we remove it, if
its textContent is empty.

NEWS
ext/dom/php_dom.c
ext/dom/tests/bug78221.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 87e279e02db587ef6f53274fa7341d654a115bc4..ff1aa47d3ba448bde4a0c45428f4d4cb501cee9e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,10 @@ PHP                                                                        NEWS
   . Fixed bug #79434 (PHP 7.3 and PHP-7.4 crash with NULL-pointer dereference
     on !CS constant). (Nikita)
 
+- DOM:
+  . Fixed bug #78221 (DOMNode::normalize() doesn't remove empty text nodes).
+    (cmb)
+
 - MBString:
   . Fixed bug #79441 (Segfault in mb_chr() if internal encoding is unsupported).
     (Girgias)
index 72ae3c3ffea36ef952a32739e8f074cd0deb2b19..ed67f047fa8756f56839619fd1edf01cb0ba17ef 100644 (file)
@@ -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 (file)
index 0000000..a9bf50d
--- /dev/null
@@ -0,0 +1,17 @@
+--TEST--
+Bug #78221 (DOMNode::normalize() doesn't remove empty text nodes)
+--SKIPIF--
+<?php
+if (!extension_loaded('dom')) die('skip dom extension not available');
+?>
+--FILE--
+<?php
+$doc = new DOMDocument();
+$doc->loadHTML('<p id=x>foo</p>');
+$p = $doc->getElementById('x');
+$p->childNodes[0]->textContent = '';
+$p->normalize();
+var_dump($p->childNodes->length);
+?>
+--EXPECT--
+int(0)