php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75624 DOMNode::$prefix not NULL when unspecified
Submitted: 2017-12-04 16:52 UTC Modified: 2024-03-09 16:09 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bit2shift at gmail dot com Assigned: nielsdos (profile)
Status: Closed Package: DOM XML related
PHP Version: 7.0.26 OS: Win7 Pro x64
Private report: No CVE-ID: None
 [2017-12-04 16:52 UTC] bit2shift at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/class.domnode
---
The documentation states that $prefix is NULL if it is unspecified, but instead I'm getting an empty string.
This causes an annoyance when passing it through to XMLWriter::startElementNs(), as it'll output (an example) <:derp> instead of <derp>.

Test script:
---------------
<?php
$doc = new DOMDocument();
$doc->loadXML('<derp></derp>');
$root = $doc->documentElement;

echo "prefix: [$root->prefix]<br>";
echo "name: [$root->localName]<br>";
echo 'is the prefix NULL? ' . (is_null($root->prefix) ? 'yep' : 'nope');
var_dump($root);

Expected result:
----------------
prefix: []
name: [derp]
is the prefix NULL? yep

--omitted--\test.php:9:
object(DOMElement)[2]
  public 'tagName' => string 'derp' (length=4)
  public 'schemaTypeInfo' => null
  public 'nodeName' => string 'derp' (length=4)
  public 'nodeValue' => string '' (length=0)
  public 'nodeType' => int 1
  public 'parentNode' => string '(object value omitted)' (length=22)
  public 'childNodes' => string '(object value omitted)' (length=22)
  public 'firstChild' => null
  public 'lastChild' => null
  public 'previousSibling' => null
  public 'attributes' => string '(object value omitted)' (length=22)
  public 'ownerDocument' => string '(object value omitted)' (length=22)
  public 'namespaceURI' => null
  public 'prefix' => null
  public 'localName' => string 'derp' (length=4)
  public 'baseURI' => string 'file:/--omitted--/' (length=46)
  public 'textContent' => string '' (length=0)

Actual result:
--------------
prefix: []
name: [derp]
is the prefix NULL? nope

--omitted--\test.php:9:
object(DOMElement)[2]
  public 'tagName' => string 'derp' (length=4)
  public 'schemaTypeInfo' => null
  public 'nodeName' => string 'derp' (length=4)
  public 'nodeValue' => string '' (length=0)
  public 'nodeType' => int 1
  public 'parentNode' => string '(object value omitted)' (length=22)
  public 'childNodes' => string '(object value omitted)' (length=22)
  public 'firstChild' => null
  public 'lastChild' => null
  public 'previousSibling' => null
  public 'attributes' => string '(object value omitted)' (length=22)
  public 'ownerDocument' => string '(object value omitted)' (length=22)
  public 'namespaceURI' => null
  public 'prefix' => string '' (length=0)
  public 'localName' => string 'derp' (length=4)
  public 'baseURI' => string 'file:/--omitted--/' (length=46)
  public 'textContent' => string '' (length=0)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-04 17:11 UTC] bit2shift at gmail dot com
https://github.com/php/php-src/blob/PHP-5.6.32/ext/dom/node.c#L757

This seems to be the origin, it should be ZVAL_NULL(*retval).
 [2017-12-04 17:18 UTC] requinix@php.net
-Status: Open +Status: Verified -PHP Version: 5.6.32 +PHP Version: 7.0.26
 [2017-12-04 17:18 UTC] requinix@php.net
Note that 5.6 is only receiving security fixes.

DOM 3 does indeed say it should be null if unspecified.

Same bug for textContent: should be null for DOCUMENT_NODE, DOCUMENT_TYPE_NODE, and NOTATION_NODE where currently PHP will force an empty string.
 [2017-12-04 22:15 UTC] bit2shift at gmail dot com
For what it's worth, this bug goes all the way to the bleeding-edge 7.2

https://github.com/php/php-src/blob/master/ext/dom/node.c#L693
 [2024-03-09 16:09 UTC] nielsdos@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nielsdos
 [2024-03-09 16:09 UTC] nielsdos@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

This is fixed in the new opt-in spec-compliance mode, which was merged in https://github.com/php/php-src/pull/13031.
For more information about the opt-in mode, please see https://wiki.php.net/rfc/opt_in_dom_spec_compliance.
In short: the behaviour of the old DOM classes are unaffected for backwards-compatibility reasons. There are new DOM classes where your code is correctly handled.
This feature will be available in PHP 8.4.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC