peachpie icon indicating copy to clipboard operation
peachpie copied to clipboard

DOMDocument::saveHTML() unexpected behavior

Open DRSDavidSoft opened this issue 4 years ago • 3 comments

The following issues are currently returned in Peach Pie, and as inconsistencies to PHP, should be resolved.

Issues

  • [x] It seems that the following DOCTYPE is prepended to the returned string:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    
  • [ ] With saveHTML(), elements such as <i class="icon i7"></i> (empty tags) are automatically returned as self-closing tags, e.g. <i class="icon i7" />. This behavior is unexpected and should be fixed (i.e., it should result in empty tags instead of self-closing tags). Read more...

  • [ ] With saveXML(), elements such as <a></a> (empty tags) should return self-closing tags, however this is not the case. (Please see #799 for test case and details).

DRSDavidSoft avatar Feb 01 '20 19:02 DRSDavidSoft

add 2 - default XML Writer implementation writes always /> if possible , while PHP always writes the full closing element </i>.

jakubmisek avatar Feb 02 '20 15:02 jakubmisek

XmlElement.IsEmpty can be used to affect the output, but we would need to traverse all the elements before the export in saveHTML and set it to false. Also, saveXML uses />, so we would need to revert it back in there.

Another way would be to inherit a custom class from XmlElement and override WriteTo in it. However, that would require us to perform also custom parsing so that its instances are placed in DOM instead of the original. Altogether, it would make the current code more complicated.

So, while it can be done, it would be an additional overhead and I'm not sure it's worth it. Does the current state cause any problems?

roberthusak avatar Feb 11 '20 12:02 roberthusak

In https://github.com/peachpiecompiler/peachpie/issues/799 I'm describing the opposite behavior: PHP uses self-closed tags by default while Peachpie/.NET uses explicit closing tags.

We still need both implementations since PHP (libxml) has an option to enable/disable it.

I came to the same solution: in .NET we can use isEmpty for each XmlNode and extend the class to overwrite a method to set it by default as commented above.

Maybe change the title of this issue?

kripper avatar Jul 23 '20 14:07 kripper