Skip to content

Commit

Permalink
DOMDocument::loadHTML() UTF-8 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
techi602 committed Mar 30, 2016
1 parent bb535b0 commit a942e3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public function getInlineStyles(\DOMElement $element)
*/
protected function createDomDocumentFromHtml($html)
{
$html = trim($html);
if (strstr('<?xml', $html) !== 0) {

This comment has been minimized.

Copy link
@mlocati

mlocati May 10, 2016

@techi602 Shouldn't this be strpos (or stripos)?

This comment has been minimized.

Copy link
@techi602

techi602 May 10, 2016

Author Contributor

@mlocati whoa you are right. (strpos because xml header is case sensitive) This will cause to always include the xml header twice. Luckily domdocument will process it anyway. I will prepare PR

This comment has been minimized.

Copy link
@techi602

techi602 May 10, 2016

Author Contributor

fixing in #139

$xmlHeader = '<?xml encoding="utf-8" ?>';
$html = $xmlHeader . $html;
}

$document = new \DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$document->loadHTML($html);
Expand Down
8 changes: 8 additions & 0 deletions tests/CssToInlineStylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ public function testInvalidSelector()
$this->assertCorrectConversion($expected, $html, $css);
}

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';

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

private function assertCorrectConversion($expected, $html, $css = null)
{
$this->assertEquals(
Expand Down

0 comments on commit a942e3a

Please sign in to comment.