html5-dom-document-php icon indicating copy to clipboard operation
html5-dom-document-php copied to clipboard

preserveWhiteSpace not supported

Open ygoe opened this issue 7 years ago • 6 comments

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.

ygoe avatar Oct 31 '18 23:10 ygoe

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?

ivopetkov avatar Nov 13 '18 08:11 ivopetkov

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.

ygoe avatar Nov 23 '18 22:11 ygoe

Do you have the code in GitHub? Will be happy to take a look and possibly improve preserveWhiteSpace in HTML5DOMDocument.

ivopetkov avatar Dec 11 '18 07:12 ivopetkov

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.

ygoe avatar Dec 29 '18 17:12 ygoe

I have the same problem with some unit-test (by replacing DOMDocument) and I think that the new "saveHTML()" method removes some "\n", or?

voku avatar Sep 11 '19 21:09 voku