How to make HTML5 work with DOMDocument?

16,616

Solution 1

Unfortunately, or possibly fortunately, domdocument is designed to not try to preserve formatting from the original document. This is to make the parser's internal state easier to manage by keeping all elements the same style. Afaik most parsers will create a tree representation in memory and not worry about the textual formatting until the user requests such. This is why your self closed tags are output with separate closing tags. The good news is that it doesn't matter.

As to style tags and script tags getting <> converted to &lt;&gt;, you may be able to avoid the conversion by surrounding the contents of the element in question with the recommended cdata tags thusly:

<style>
  /*<![CDATA[*/
    body > div {
      width: 50%;
    }
  /*]]>*/
</style>

The comment /* */ around the cdata declarations are to allow for broken clients which don't know about cdata sections and instead treat the declarations as CSS code. If you're using the document internally only then you may omit the /* */ comment surrounds and have the cdata declaration only. You may encounter issues with the aforementioned broken clients if you manipulate the document and then send it to the browser without checking to ensure the /* */ comments are retained; I am unsure whether domdocument will retain these or not.

Solution 2

Use html5lib. It can parse html5 and produce a DOMDocument. Example:

require_once '/path/to/HTML5/Parser.php';
$dom = HTML5_Parser::parse('<html><body>...');

Documentation

Solution 3

If you want to support HTML5, do not touch DOMDocument at all.

Currently the best option seems to be https://github.com/Masterminds/html5-php

Previously the best option was https://github.com/html5lib/html5lib-php but as the description says, it's "currently unmaintained". And this has been status for since October 2011 so I'm not holding my breath anymore.

I haven't used html5-php in production so I cannot provide any real world experiences about that. I've used html5lib-php in production and I would say that it's parsing well formed documents correctly but it has unexpected errors with some simple syntax errors. On the other hand, it seems to implement adoption agency algorithm and some other weird corner cases correctly. If html5lib-php were still maintained, I'd still prefer it. However, as things currently stand, I'd prefer using html5-php and possibly help fixing remaining bugs there.

Solution 4

I tried both html5lib and html5php but neither worked with the HTML I was provided with. An alternative that was able to parse the HTML was: https://github.com/ivopetkov/html5-dom-document-php

The main class extends PHP's native DomDocument.

Solution 5

10 years has been passed but the problem still exists on PHP DOMDocument, I found 2 ways to fix the issue.

Solution 1

Add LIBXML_NOERROR as option to the loadHTML method like this:

<?php

$dom = new DOMDocument();

$dom->loadHTML('<header data-attribute="foo">bar<', LIBXML_NOERROR);

echo $dom->saveHTML();
// outputs the html with valid closing tag without any error
?>

Solution 2

Add libxml_use_internal_errors(true) before loading the HTML

<?php

$dom = new DOMDocument();

libxml_use_internal_errors(true);

$dom->loadHTML('<header data-attribute="foo">bar<');

echo $dom->saveHTML();
// outputs the html with valid closing tag without any error
?>
Share:
16,616
Alex
Author by

Alex

I'm still learning so I'm only here to ask questions :P

Updated on June 03, 2022

Comments

  • Alex
    Alex about 2 years

    I'm attempting to parse HTML code with DOMDocument, do stuff like changes to it, then assemble it back to a string which I send to the output.

    But there a few issues regarding parsing, meaning that what I send to DOMDocument does not always come back in the same form :)

    Here's a list:

    1. using ->loadHTML:

      • formats my document regardless of the preserveWhitespace and formatOutput settings (loosing whitespaces on preformatted text)
      • gives me errors when I have html5 tags like <header>, <footer> etc. But they can be supressed, so I can live with this.
      • produces inconsistent markup - for example if I add a <link ... /> element (with a self-closing tag), after parsing/saveHTML the output will be <link .. >
    2. using ->loadXML:

      • encodes entities like > from <style> or <script> tags: body > div becomes body &gt; div
      • all tags are closed the same way, for example <meta ... /> becomes <meta...></meta>; but this can be fixed with an regex.

    I didn't try HTML5lib but I'd prefer DOMDocument instead of a custom parser for performance reasons


    Update:

    So like the Honeymonster mentioned using CDATA fixes the main problem with loadXML.

    Is there any way I could prevent self closing of all empty HTML tags besides a certain set, without using regex?

    Right now I have:

    $html = $dom->saveXML($node);
    
    $html = preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', function($matches){
    
           // ignore only these tags
           $xhtml_tags = array('br', 'hr', 'input', 'frame', 'img', 'area', 'link', 'col', 'base', 'basefont', 'param' ,'meta');
    
           // if a element that is not in the above list is empty,
           // it should close like   `<element></element>` (for eg. empty `<title>`)
           return in_array($matches[1], $xhtml_tags) ? "<{$matches[1]}{$matches[2]} />" : "<{$matches[1]}{$matches[2]}></{$matches[1]}>";
    }, $html);
    

    which works but it will also do the replacements in the CDATA content, which I don't want...

  • Alex
    Alex about 12 years
    wow i can't believe I didn't think of using CDATA :) thanks, that solves many issues with the xml parser, which i wanted to use ;)
  • Wiliam
    Wiliam almost 12 years
    But html5lib can save back the documents and return a string with the nice format? I didn't saw that in the source code.
  • nibra
    nibra over 8 years
    Unfortunately, the version parameter does not refer to the HTML version.
  • frumbert
    frumbert over 6 years
    My main takeaway from this particular library was this: Allows querying the DOM with CSS selectors (currently avaiable: *, tagname, tagname#id, #id, tagname.classname, .classname, tagname[attribute="value"], [attribute="value"], tagname[attribute], [attribute]) - so you can now $foo = $dom->querySelectorAll('img[srcset]'); - very helpful.
  • Mikko Rantalainen
    Mikko Rantalainen almost 4 years
    Since PHP 7.2 extension html-tidy should have some kind of support for HTML5. I don't yet have any experience with it so I cannot speak for the quality of the implementation.
  • Mikko Rantalainen
    Mikko Rantalainen almost 4 years
    Note that you still cannot skip encoding data by yourself if you're going to embed untrusted user input. The user data may easily contain substring ]]> or /*]]>*/.