html5-dom-document-php
html5-dom-document-php copied to clipboard
preserveWhiteSpace not supported
Just wondering why this doesn't produce "minified" HTML output:
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($html);
return $dom->saveHTML();
HTML5DOMDocument inherits from DOMDocument and that should have the preserveWhiteSpace property. It defaults to true and according to numerous Stack Overflow answers, setting it to false produces output without whitespace-only text nodes, like indenting. It doesn't change my HTML in any way however.
The property does exist, is initially true, and is no longer true after setting it to false. It just has no effect here.
In my experience preserveWhiteSpace does not work as described in the DOMDocument docs, furthermore, in HTML5DomDocument (my library) there is no code related to preserving/removing whitespaces (for this part it depends on DOMDocument).
Do you have any example code that behaves differently (related to whitespaces) in DOMDocument and HTML5DomDocument?
No I don't have an example that shows a difference. I'm not using the PHP DOMDocument. I've written something else to reduce whitespace in text nodes now. And it can do so more intelligently, only where it's safe to do so. It doesn't appear to take considerable time or memory.
Do you have the code in GitHub? Will be happy to take a look and possibly improve preserveWhiteSpace in HTML5DOMDocument.
https://gist.github.com/ygoe/065aceb90f9efd81e8f57ee01546b5f7
The code shows how I minify HTML manually. It may not be 100% safe but has worked for me so far. And it still leaves single spaces in places where they're not necessary. But that probably only decides at rendering time together with the stylesheet.
Instead of the minify code block, the preserveWhiteSpace property should do the same. But as I think about it, it's probably not correct because it wouldn't know about the exceptions I made in my implementation.
I have the same problem with some unit-test (by replacing DOMDocument) and I think that the new "saveHTML()" method removes some "\n", or?