tijsverkoyen / CssToInlineStyles

CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very usefull when you're sending emails.
BSD 3-Clause "New" or "Revised" License
5.8k stars 187 forks source link

`mb_encode_numericentity` vs `htmlspecialchars_decode(htmlentities($html))` #246

Open hirasso opened 10 months ago

hirasso commented 10 months ago

Hi there! This is actually not an issue with your library (which is amazing!), rather a question. Feel free to close this if you don't have the time right now :)

I'm in the process of migrating a few projects to PHP 8.2 and trying to find the correct replacement for this:

$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))

While searching my code base, I found an old version of CssToInlineStyles that was also using this function. And I see that you have replaced it with this since then:

$document->loadHTML(mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, 0x1FFFFF], 'UTF-8'));

https://github.com/tijsverkoyen/CssToInlineStyles/blob/83ee6f38df0a63106a9e4536e3060458b74ccedb/src/CssToInlineStyles.php#L116C29-L116C52

This looks very cryptic to me and I'd like to understand better what's going on here. In my (manual) tests, the following worked with all strings that I tested it with:

htmlspecialchars_decode(htmlentities($html));

But I'm a bit afraid that I'm missing something...

I'd be grateful for any insight you might have on the matter 🙂