php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73175 DOMDocument::saveHTML's void elements list is not updated
Submitted: 2016-09-26 09:42 UTC Modified: 2021-03-12 12:59 UTC
Votes:9
Avg. Score:4.3 ± 0.8
Reproduced:8 of 8 (100.0%)
Same Version:2 (25.0%)
Same OS:1 (12.5%)
From: duhamelantoine1995 at gmail dot com Assigned: cmb (profile)
Status: Closed Package: DOM XML related
PHP Version: 7.0.11 OS: Debian 8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
42 - 40 = ?
Subscribe to this entry?

 
 [2016-09-26 09:42 UTC] duhamelantoine1995 at gmail dot com
Description:
------------
The DOMDocument::saveHTML and DOMDocument::saveHTMLFile methods does not product valid HTML when using void elements introduced in HTML5.

The official HTML definition give a complete list of the void elements in HTML: https://www.w3.org/TR/html-markup/syntax.html#syntax-elements

 > The following is a complete list of the void elements in HTML:
 >
 > area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

In my tests (PHP 5&7, Debian 8 and Windows 10), PHP DOM render the following tags as non-void elements:
 - command
 - embed
 - keygen
 - source
 - track
 - wbr

Test script:
---------------
$implementation = new DOMImplementation;
$dom = $implementation->createDocument(
    null,
    'html',
    $implementation->createDocumentType('html', null, null)
);
$dom->formatOutput = true;

$body = $dom->createElement('body');
$div = $dom->createElement('div');
$dom->documentElement->appendChild($body);
$body->appendChild($div);

$voidElem = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr',];

foreach ($voidElem as $tagName) {
    $div->appendChild($dom->createElement($tagName));
}
echo $dom->saveHTML();

Expected result:
----------------
<!DOCTYPE html>
<html><body><div>
<area>
<base>
<br><col>
<command><embed><hr>
<img><input><keygen><link>
<meta>
<param>
<source><track><wbr>
</div></body></html>

Actual result:
--------------
<!DOCTYPE html>
<html><body><div>
<area>
<base>
<br><col>
<command></command><embed></embed><hr>
<img><input><keygen></keygen><link>
<meta>
<param>
<source></source><track></track><wbr></wbr>
</div></body></html>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-12 12:59 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-03-12 12:59 UTC] cmb@php.net
This is a known limitation of libxml2, and apparently there are no
plans to ever fix this.  There is not much we can do about it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 02:01:30 2024 UTC