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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: duhamelantoine1995 at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 13:01:29 2025 UTC