Skip to content

Commit

Permalink
Save as html instead of xml.
Browse files Browse the repository at this point in the history
This solves the issues with self-closing tags (<br> going to <br></br>)
and assists with utf-8 encoding.
  • Loading branch information
Josh Hunt committed Aug 30, 2016
1 parent b081b74 commit 4e1620a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
30 changes: 16 additions & 14 deletions src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,9 @@ public function getInlineStyles(\DOMElement $element)
*/
protected function createDomDocumentFromHtml($html)
{
$html = trim($html);
if (strstr('<?xml', $html) !== 0) {
$xmlHeader = '<?xml encoding="utf-8" ?>';
$html = $xmlHeader . $html;
}

$document = new \DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$document->loadHTML($html);
$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_use_internal_errors($internalErrors);
$document->formatOutput = true;

Expand All @@ -121,15 +115,23 @@ protected function createDomDocumentFromHtml($html)
*/
protected function getHtmlFromDocument(\DOMDocument $document)
{
$xml = $document->saveXML(null, LIBXML_NOEMPTYTAG);
// retrieve the document element
// we do it this way to preserve the utf-8 encoding
$htmlElement = $document->documentElement;
$html = $document->saveHTML($htmlElement);
$html = trim($html);

$html = preg_replace(
'|<\?xml (.*)\?>|',
'',
$xml
);
// retrieve the doctype
$document->removeChild($htmlElement);
$doctype = $document->saveHTML();
$doctype = trim($doctype);

// if it is the html5 doctype convert it to lowercase
if ($doctype === '<!DOCTYPE html>') {
$doctype = strtolower($doctype);
}

return ltrim($html);
return $doctype."\n".$html;
}

/**
Expand Down
43 changes: 37 additions & 6 deletions tests/CssToInlineStylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ public function testApplyBasicStylesOnElementWithInlineStyles()

public function testBasicRealHTMLExample()
{
$html = '<!DOCTYPE html><html><body><p>foo</p></body></html>';
$html = '<!doctype html><html><head><style>body{color:blue}</style></head><body><p>foo</p></body></html>';
$css = 'p { color: red; }';
$expected = '<p style="color: red;">foo</p>';
$expected = <<<EOF
<!doctype html>
<html>
<head><style>body{color:blue}</style></head>
<body style="color: blue;"><p style="color: red;">foo</p></body>
</html>
EOF;

$this->assertCorrectConversion($expected, $html, $css);
$this->assertEquals($expected, $this->cssToInlineStyles->convert($html, $css));
}

public function testSimpleElementSelector()
Expand Down Expand Up @@ -175,7 +181,7 @@ public function testSpecificity()
EOF;
$expected = <<<EOF
<a class="one" id="ONE" style="padding: 100px; border: 1px solid red; margin: 10px; width: 20px !important;">
<img class="two" id="TWO" style="border: none;"></img></a>
<img class="two" id="TWO" style="border: none;"></a>
EOF;
$this->assertCorrectConversion($expected, $html, $css);
}
Expand All @@ -201,9 +207,34 @@ public function testInvalidSelector()
public function testHtmlEncoding()
{
$text = 'Žluťoučký kůň pije pivo nebo jak to je dál';
$expectedText = '&#x17D;lu&#x165;ou&#x10D;k&#xFD; k&#x16F;&#x148; pije pivo nebo jak to je d&#xE1;l';
$expected = $text;

$this->assertEquals($expectedText, trim(strip_tags($this->cssToInlineStyles->convert($text, ''))));
$this->assertEquals($expected, trim(strip_tags($this->cssToInlineStyles->convert($text))));
}

public function testSpecialCharacters()
{
$text = '1 &lt; 2';
$expected = $text;

$this->assertEquals($expected, trim(strip_tags($this->cssToInlineStyles->convert($text))));
}

public function testSpecialCharactersExplicit()
{
$text = '&amp;lt;script&amp;&gt;';
$expected = $text;

$this->assertEquals($expected, trim(strip_tags($this->cssToInlineStyles->convert($text))));
}

public function testSelfClosingTags()
{
$html = '<br>';
$css = '';
$expected = $html;

$this->assertCorrectConversion($expected, $html, $css);
}

private function assertCorrectConversion($expected, $html, $css = null)
Expand Down

0 comments on commit 4e1620a

Please sign in to comment.