Alex
Alex

Reputation: 67698

How to make HTML5 work with DOMDocument?

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...

Upvotes: 28

Views: 23484

Answers (6)

Obaydur Rahman
Obaydur Rahman

Reputation: 848

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

Update: PHP 8.4 now supports HTML 5, check the solution 3.

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
?>

Solution 3 (PHP 8.4)

PHP 8.4 introduces the \Dom\HTMLDocument class for better handling of modern HTML5 content. While it still warns about malformed HTML, it offers improved compatibility with modern standards.

<?php

use Dom\HTMLDocument;

$htmlContent = '<header data-attribute="foo">bar<';
$dom = HTMLDocument::createFromString($htmlContent);

echo $dom->saveHTML();

// outputs the html with valid closing tag with a warning for not ending the html properly but no invalid html tag warning.
?>

To suppress warnings, you can either:

  • Use the @ operator (e.g., @$dom->createFromString($htmlContent)).
  • Wrap the code in a try-catch block for better error handling.

Upvotes: 16

Potherca
Potherca

Reputation: 14590

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.

Upvotes: 4

Mikko Rantalainen
Mikko Rantalainen

Reputation: 16015

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.

Upvotes: 12

Justin Levene
Justin Levene

Reputation: 1677

When initialising domDocument, do as follows:

$dom = new DOMDocument(5, 'UTF-8');

Upvotes: -12

Lucy Llewellyn
Lucy Llewellyn

Reputation: 1011

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.

Upvotes: 7

Francis Avila
Francis Avila

Reputation: 31641

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

Upvotes: 14

Related Questions