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 = << - + EOF; $this->assertCorrectConversion($expected, $html, $css); } @@ -201,9 +207,34 @@ public function testInvalidSelector() public function testHtmlEncoding() { $text = 'Žluťoučký kůň pije pivo nebo jak to je dál'; - $expectedText = 'Žluťoučký kůň pije pivo nebo jak to je dá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 < 2'; + $expected = $text; + + $this->assertEquals($expected, trim(strip_tags($this->cssToInlineStyles->convert($text)))); + } + + public function testSpecialCharactersExplicit() + { + $text = '&lt;script&>'; + $expected = $text; + + $this->assertEquals($expected, trim(strip_tags($this->cssToInlineStyles->convert($text)))); + } + + public function testSelfClosingTags() + { + $html = '
'; + $css = ''; + $expected = $html; + + $this->assertCorrectConversion($expected, $html, $css); } private function assertCorrectConversion($expected, $html, $css = null)