Skip to content

Commit b2954c6

Browse files
committed
Fix #70001: Assigning to DOMNode::textContent does additional entity encoding
Assigning to DOMNode::textContent encodes entities, what does not match the behavior of DOMText::__construct() and DOMDocument::createTextNode. This patch changes the behavior of DOMNode::textContent in this regard.
1 parent b59ea79 commit b2954c6

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

ext/dom/node.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,17 +930,16 @@ int dom_node_text_content_write(dom_object *obj, zval *newval TSRMLS_DC)
930930
{
931931
xmlNode *nodep = dom_object_get_node(obj);
932932
zval value_copy;
933-
xmlChar *enc_str;
934933

935934
if (nodep == NULL) {
936935
php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
937936
return FAILURE;
938937
}
939938

940939
convert_to_string_copy(newval, value_copy);
941-
enc_str = xmlEncodeEntitiesReentrant(nodep->doc, Z_STRVAL_P(newval));
942-
xmlNodeSetContent(nodep, enc_str);
943-
xmlFree(enc_str);
940+
/* we have to use xmlNodeAddContent() to get the same behavior as with xmlNewText() */
941+
xmlNodeSetContent(nodep, (xmlChar *) "");
942+
xmlNodeAddContent(nodep, Z_STRVAL_P(newval));
944943
if (newval == &value_copy) {
945944
zval_dtor(newval);
946945
}

ext/dom/tests/bug70001.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #70001 (Assigning to DOMNode::textContent does additional entity encoding)
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$element = new DOMText('<p>foo & bar</p>');
8+
var_dump($element->textContent);
9+
$element = (new DOMDocument())->createTextNode('<p>foo & bar</p>');
10+
var_dump($element->textContent);
11+
$element->textContent = ('<p>foo & bar</p>');
12+
var_dump($element->textContent);
13+
?>
14+
--EXPECT--
15+
string(16) "<p>foo & bar</p>"
16+
string(16) "<p>foo & bar</p>"
17+
string(16) "<p>foo & bar</p>"

0 commit comments

Comments
 (0)