Skip to content

Commit

Permalink
html_entity_decode() clears the DOM entities
Browse files Browse the repository at this point in the history
  • Loading branch information
techi602 committed Jul 22, 2016
1 parent 2ebaccb commit 1526f07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ protected function getHtmlFromDocument(\DOMDocument $document)
$xml
);

/*
* Replace entities such as < > with adding extra &
* but ignores entities with & following by # such as d
* so < will become <
* after calling html_entity_decode() < will stay < etc.
*/
$replace = array(
'<' => '<',
'>' => '>',
);
$html = str_replace(array_keys($replace), array_values($replace), $html);
$html = preg_replace('~(&)([^\#])~', '&$2', $html);
$html = html_entity_decode($html, ENT_COMPAT | ENT_HTML401, 'UTF-8');

return ltrim($html);
}

Expand Down
19 changes: 16 additions & 3 deletions tests/CssToInlineStylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,23 @@ 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';
$text = $expectedText = 'Žluťoučký kůň pije pivo nebo jak to je dál @ €';

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

public function testSpecialCharacters()
{
$text = $expectedText = '1 < 2';

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

public function testSpecialCharactersExplicit()
{
$text = $expectedText = '<script&>';

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

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

0 comments on commit 1526f07

Please sign in to comment.