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: 2017-12-04 22:15 UTC
From: bit2shift at gmail dot com Assigned:
Status: Verified Package: DOM XML related
PHP Version: 7.0.26 OS: Win7 Pro x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bit2shift at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Jun 25 09:01:26 2019 UTC