Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

preserveWhiteSpace not supported #23

Open
ygoe opened this issue Oct 31, 2018 · 6 comments
Open

preserveWhiteSpace not supported #23

ygoe opened this issue Oct 31, 2018 · 6 comments

Comments

@ygoe
Copy link

ygoe commented Oct 31, 2018

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.

@ivopetkov
Copy link
Owner

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?

@ygoe
Copy link
Author

ygoe commented Nov 23, 2018

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.

@ivopetkov
Copy link
Owner

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

@ygoe
Copy link
Author

ygoe commented Dec 29, 2018

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.

@voku
Copy link

voku commented Sep 11, 2019

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

@Mrten
Copy link

Mrten commented Aug 29, 2024

A reason, perhaps, that this is not working, is that saveHTML() in nodeMode caches a new HTML5Document that completely ignores all the settings from the current one.

      if (!isset(self::$newObjectsCache['html5domdocument'])) {
            self::$newObjectsCache['html5domdocument'] = new HTML5DOMDocument();
      }

Fixing this is not really easy because of the fixed substr()s after this code.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants