|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31130 DomDocument::saveHTML() produces outdated HTML
Submitted: 2004-12-16 18:36 UTC Modified: 2004-12-18 22:41 UTC
Avg. Score:3.7 ± 1.4
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: tom at whyscream dot net Assigned:
Status: Wont fix Package: DOM XML related
PHP Version: 5.0.3 OS: Linux (gentoo)
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 — but make sure to vote on the bug!
Your email address:
Solve the problem:
25 - 3 = ?
Subscribe to this entry?

 [2004-12-16 18:36 UTC] tom at whyscream dot net
The HTML that is generated by DomDocument::saveHTML() (and probably saveHTMLFile() too) does not validate as XHTML (as in '1.0 Transitional'). In stead it follows common practice for HTML4.
Since some time, it's Good Practice (TM) to use simple rules like:
- always give a value to an attribute
- close single-tag elements with a '/>' instead of '>'

All common browsers understand this syntax, even when a HTML 4.0 DTD is specified.

With the current implementation, it's impossible to create valid XHTML (snippets) using the php5 DOM extension.

Reproduce code:
$doc = new DomDocument();
$input = $doc->createElement('input');
$input->setAttribute('type', 'checkbox');
$input->setAttribute('checked', 'checked');
echo $doc->saveHTML();

Expected result:
This should generate a string like:
<input type="checkbox" checked="checked" />
which is valid XHTML.

Actual result:
The generated string looks like this: 
<input type="checkbox" checked>
This code gives 2 parse errors:
- no value for attribute 'checked'
- end tag for 'input' omitted (i,e, missing the forward slash)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-16 20:03 UTC]
Tried ->saveXML() ?
 [2004-12-17 07:47 UTC]
It's called saveHTML and not saveXHTML for a reason  ;) saveHTML produces HTML 4.0 and as Derick pointed out, you have to use saveXML (which works perfectly fine for me).

If you really want to have changed that, complain to the libxml2 people. We just use their function.
 [2004-12-18 22:41 UTC] tom at whyscream dot net
When I submitted the bug, I had some problems  with saveXML() that prevented me from producing usable XHTML with it. I found out how to work around these issues the next day, and of course I was wrong (it was there all the time, but I missed it while checking the manual). Sorry for bothering you :)
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 17 23:01:27 2024 UTC