php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70029 nodeValue of DOMElement list content of children nodes
Submitted: 2015-07-08 20:57 UTC Modified: 2015-09-09 22:53 UTC
From: Laurent dot Lyaudet at gmail dot com Assigned: cmb (profile)
Status: Closed Package: DOM XML related
PHP Version: 5.4.42 OS: Debian Linux
Private report: No CVE-ID: None
 [2015-07-08 20:57 UTC] Laurent dot Lyaudet at gmail dot com
Description:
------------
Hi,

When using nodeValue attribute  of a DOMElement, the content of descendant (children, grandchildren, etc.) nodes of the DOMElement are listed.
This bug emerged from a discussion on bug 65569
https://bugs.php.net/bug.php?id=65569

Best regards,
  Laurent Lyaudet

Test script:
---------------
<?php
$xml = new DOMDocument();
$xml->loadXml('<tag><childtag>myString</childtag></tag>');
echo $xml->documentElement->nodeValue, "\n";

?>

or

<?php
$xml = new DOMDocument();
$xml->loadXml('<tag><childtag><grandchildtag>myString</grandchildtag></childtag></tag>');
echo $xml->documentElement->nodeValue, "\n";

?>


Expected result:
----------------
no output

Actual result:
--------------
myString

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-08 21:21 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2015-07-08 21:21 UTC] cmb@php.net
Confirmed: <http://3v4l.org/nrYAR>.

According to the "Document Object Model Core" specification[1]
nodeValue is supposed to yield null for Element nodes, so the
behavior may be considered as bug. However, it works this way
since the beginnings of the DOM extension, and the
documentation[2] isn't particularly clear with regard to the exact
behavior, so changing the behavior would introduce a BC break
which is not easily noticed.

Nonetheless, this should be fixed for PHP 7 (even though beta1 has
already been tagged, which marks the feature freeze), in my
opinion.

[1] <http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-F68D080>
[2] <http://de2.php.net/manual/en/class.domnode.php#domnode.props.nodevalue>
 [2015-09-05 00:03 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=25a52d98d7cb2d4f3b3636eac45c71a1ae03c760
Log: Fix #70029: nodeValue of DOMElement list content of children nodes
 [2015-09-05 00:03 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2015-09-05 00:05 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2015-09-05 00:05 UTC] cmb@php.net
The fix for this bug has been committed.

Thank you for the report, and for helping us make PHP better.
 [2015-09-05 20:02 UTC] bwoebi@php.net
Could that fix please be noted in UPGRADING?

That just broke my code... :-/

Yes, it's non-standard, but still an useful side-effect,
 [2015-09-06 01:11 UTC] me at kelunik dot com
Yes, this is a bug, but I'd like to see it reverted and marked as WONTFIX.

Fixing it has exactly zero benefits (except spec compliance) while it introduces a BC break and removes a useful features.
 [2015-09-06 08:05 UTC] mbeccati@php.net
Among other things, the fix breaks PHPUnit:

https://revive.beccati.com/bamboo/browse/PHP-PHPUN-477
 [2015-09-06 13:38 UTC] cmb@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b344f55f4dc033cba41a94cbe0d2fdc2ad606a6
Log: Revert &quot;Fix #70029: nodeValue of DOMElement list content of children nodes&quot;
 [2015-09-06 13:39 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened -Assigned To: cmb +Assigned To:
 [2015-09-06 13:39 UTC] cmb@php.net
I have reverted the fix and the respective UPGRADING and NEWS
entries.

> Fixing it has exactly zero benefits (except spec compliance)
> while it introduces a BC break and removes a useful features.

IMHO, standard compliance is a benefit, especially as the manual
states[1]:

| The DOM extension allows you to operate on XML documents through
| the DOM API with PHP 5.

Furthermore I don't see a good reason to use DOMNode::nodeValue
instead of DOMNode::textContent to get the text content of a node.

[1] <http://www.php.net/manual/en/intro.dom.php>
 [2015-09-08 19:05 UTC] Laurent dot Lyaudet at gmail dot com
Hi,

Please correct it again. 
It's way better to have standard compliance than having code relying on non documented, unwanted behavior. Code relying on non standard is not portable and reference implementation of DOM API in PHP should be standard compliant.
Moreover the feature is not removed since it already exists with textContent as you pointed out.

The only negative point is BC break but it is only a minor point since it's so easy to change nodeValue for textContent in that case.
It's sufficient to explain this possible fix in the release note.

If, for a reason I don't see, it's more clever to have textContent than having null in nodeValue of DOMElements, then it should be first explained why. And it should be explained not only here but also at the W3C where the debate should be.

Best regards,
   Laurent Lyaudet
 [2015-09-08 21:28 UTC] me at kelunik dot com
textContent seems like a good alternative, missed that, it's probably fine with a small explanation in UPGRADE then.
 [2015-09-08 22:03 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=337788
Log: DOMElement::nodeValue does not conform to W3C specs (related to bug #70029)
 [2015-09-09 22:53 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2015-09-09 22:53 UTC] cmb@php.net
After some further discussion on the internals mailing list[1], it
seems to be best to postpone the "fix" until PHP 7.1.

[1] <http://markmail.org/message/d7bwuawbccnjklvm>
 [2015-09-15 12:13 UTC] ab@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b344f55f4dc033cba41a94cbe0d2fdc2ad606a6
Log: Revert &quot;Fix #70029: nodeValue of DOMElement list content of children nodes&quot;
 [2015-09-15 12:13 UTC] ab@php.net
-Status: Re-Opened +Status: Closed
 [2015-09-15 12:13 UTC] ab@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=25a52d98d7cb2d4f3b3636eac45c71a1ae03c760
Log: Fix #70029: nodeValue of DOMElement list content of children nodes
 [2016-07-20 11:36 UTC] davey@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b344f55f4dc033cba41a94cbe0d2fdc2ad606a6
Log: Revert &quot;Fix #70029: nodeValue of DOMElement list content of children nodes&quot;
 [2016-07-20 11:36 UTC] davey@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=25a52d98d7cb2d4f3b3636eac45c71a1ae03c760
Log: Fix #70029: nodeValue of DOMElement list content of children nodes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC