diff --git a/src/CssToInlineStyles.php b/src/CssToInlineStyles.php index 5f9052b..313880b 100644 --- a/src/CssToInlineStyles.php +++ b/src/CssToInlineStyles.php @@ -100,15 +100,9 @@ public function getInlineStyles(\DOMElement $element) */ protected function createDomDocumentFromHtml($html) { - $html = trim($html); - if (strstr(''; - $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; @@ -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 = strtolower($doctype); + } - return ltrim($html); + return $doctype."\n".$html; } /** diff --git a/tests/CssToInlineStylesTest.php b/tests/CssToInlineStylesTest.php index cb2cdfe..deaec20 100644 --- a/tests/CssToInlineStylesTest.php +++ b/tests/CssToInlineStylesTest.php @@ -84,11 +84,17 @@ public function testApplyBasicStylesOnElementWithInlineStyles() public function testBasicRealHTMLExample() { - $html = '
foo
'; + $html = 'foo
'; $css = 'p { color: red; }'; - $expected = 'foo
'; + $expected = <<foo
+ +EOF; - $this->assertCorrectConversion($expected, $html, $css); + $this->assertEquals($expected, $this->cssToInlineStyles->convert($html, $css)); } public function testSimpleElementSelector() @@ -175,7 +181,7 @@ public function testSpecificity() EOF; $expected = <<