php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64899 Elements with values of empty string are turned into void elements
Submitted: 2013-05-22 11:41 UTC Modified: 2013-05-22 15:30 UTC
From: douglas dot wright at pre-school dot org dot uk Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.5.0RC1 OS: Windows 7
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: douglas dot wright at pre-school dot org dot uk
New email:
PHP Version: OS:

 

 [2013-05-22 11:41 UTC] douglas dot wright at pre-school dot org dot uk
Description:
------------
There is a regression in PHP5.5RC where empty text nodes seem to be lost e.g. <script src=""></script> is turned into <script src=""/>

This breaks XHTML pages because browsers don't recognise the self-closing syntax.

In PHP 5.3 and 5.4 the two calls below had different output

$doc->createElement('script') output <script/>
$doc->createElement('script', '') output <script></script>

In PHP5.5, they both output <script/> only. Documents parsed using loadXML suffer this too - <script></script> loses the inner text node and is transformed into <script/>.

Test script:
---------------
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

$doc = new DOMDocument;
$script = $doc->createElement('script', '');
$script->setAttribute('src', 'foo.js');
$doc->appendChild($script);
echo $doc->saveXML();

$doc->loadXML('<script src="abc.js"></script>');
echo $doc->saveXML();

Expected result:
----------------
<?xml version="1.0"?>
<script src="foo.js"></script>
<?xml version="1.0"?>
<script src="foo.js"></script>

Actual result:
--------------
<?xml version="1.0"?>
<script src="foo.js"/>
<?xml version="1.0"?>
<script src="foo.js"/>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-22 15:06 UTC] ab@php.net
The behaviour you describe is pretty valid for XML. saveHTML() method should be 
used if you need a HTML conform output. Also the behavior is different in 5.4 
when using loadXML (tested with your snippet). PHP 5.5 uses libxml 2.9.1 where 5.4 
uses libxml 2.7.8. libxml supports HTML 4.0 parser only AFAIR (yet at least). 

You might be also interested in workarounds suggested by users 
http://de2.php.net/manual/en/domdocument.savehtml.php
 [2013-05-22 15:06 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2013-05-22 15:28 UTC] douglas dot wright at pre-school dot org dot uk
Hi

The workaround of inserting an empty text node manually after element creation still seems to work, but inserting a text node is what the $value param in createElement($name,$value) is surely for?

i.e. 
 
$script = $doc->createElement ('script', '') //not working in 5.5

is the same thing as:
$script = $doc->createElement ('script');
$script->appendChild ($doc->createTextNode ('')); //does work in 5.5

On setting, this creates a Text node with the unparsed contents of the string
 [2013-05-22 15:30 UTC] douglas dot wright at pre-school dot org dot uk
(the quote in the previous comment was from http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-221662474)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC