Skip to content

Commit

Permalink
Add LIBXML_NOBLANKS to make the output same on Linux and Windows
Browse files Browse the repository at this point in the history
We are using this library through Symfony to inline CSS in e-mails. We are also
using snapshot testing for rendered e-mails templates and I discovered that the
output of this library differs between Windows (dev machine) and Linux (CI).

I tried running the test suite on Windows and the test
CssToInlineStylesTest::testSpecificity() fails because of whitespace difference:
on Windows there is an extra \n before the closing </a> in the output.

I tried loading and saving a a super-simplified HTML <a>\n<img>\n</a>
through the DOMDocument without this library but with the same settings
and a similar thing happen (newline before </a> is present on Windows but missing
on Linux).

When the option LIBXML_NOBLANKS is added to loadHTML() the output no longer
differs between the Windows and Linux.

To be honest, I'm not sure if this is a proper fix.
It should be rather considered a bug report with a workaround.
  • Loading branch information
mhujer committed Jan 27, 2022
1 parent 0012aa9 commit 88fc1f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function createDomDocumentFromHtml($html)
{
$document = new \DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_NOBLANKS);
libxml_use_internal_errors($internalErrors);
$document->formatOutput = true;

Expand Down
17 changes: 15 additions & 2 deletions tests/CssToInlineStylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ public function testInlineStylesBlock()
$this->assertCorrectConversion($expected, $html);
}

public function testLinuxWindowsOutputDifference()
{
$html = <<<EOF
<a>
<img>
</a>
EOF;
$expected = <<<EOF
<a>
<img></a>
EOF;
$this->assertCorrectConversion($expected, $html, '');
}

public function testSpecificity()
{
$html = <<<EOF
Expand Down Expand Up @@ -210,8 +224,7 @@ public function testSpecificity()
EOF;
$expected = <<<EOF
<a class="one" id="ONE" style="border: 1px solid red; width: 20px !important; border-bottom: 2px; height: 20px; margin: 10px; padding: 100px;">
<img class="two" id="TWO" style="padding-bottom: 20px; padding: 0; border: none; padding-top: 30px;">
</a>
<img class="two" id="TWO" style="padding-bottom: 20px; padding: 0; border: none; padding-top: 30px;"></a>
EOF;
$this->assertCorrectConversion($expected, $html, $css);
}
Expand Down

0 comments on commit 88fc1f1

Please sign in to comment.