Skip to content

Commit efec22b

Browse files
committed
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.
1 parent 656eac7 commit efec22b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ PHP NEWS
66
. Fixed bug #79434 (PHP 7.3 and PHP-7.4 crash with NULL-pointer dereference
77
on !CS constant). (Nikita)
88

9+
- DOM:
10+
. Fixed bug #78221 (DOMNode::normalize() doesn't remove empty text nodes).
11+
(cmb)
12+
913
- MBString:
1014
. Fixed bug #79441 (Segfault in mb_chr() if internal encoding is unsupported).
1115
(Girgias)

ext/dom/php_dom.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,14 @@ void dom_normalize (xmlNodePtr nodep)
13831383
break;
13841384
}
13851385
}
1386+
strContent = xmlNodeGetContent(child);
1387+
if (*strContent == '\0') {
1388+
nextp = child->next;
1389+
xmlUnlinkNode(child);
1390+
php_libxml_node_free_resource(child);
1391+
child = nextp;
1392+
continue;
1393+
}
13861394
break;
13871395
case XML_ELEMENT_NODE:
13881396
dom_normalize (child);

ext/dom/tests/bug78221.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #78221 (DOMNode::normalize() doesn't remove empty text nodes)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('dom')) die('skip dom extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$doc = new DOMDocument();
10+
$doc->loadHTML('<p id=x>foo</p>');
11+
$p = $doc->getElementById('x');
12+
$p->childNodes[0]->textContent = '';
13+
$p->normalize();
14+
var_dump($p->childNodes->length);
15+
?>
16+
--EXPECT--
17+
int(0)

0 commit comments

Comments
 (0)