Skip to content

Commit

Permalink
Moved to an html5 document abstraction to resolve encoding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDC committed Oct 22, 2017
1 parent 2ee0e8f commit b40feef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "library",
"require": {
"jandc/twig-wrapper": "^0.3",
"jandc/css-from-html-extractor": "^1.0"
"jandc/css-from-html-extractor": "^1.0",
"masterminds/html5": "^2.3"
},
"license": "MIT",
"authors": [
Expand Down
11 changes: 5 additions & 6 deletions src/CriticalCssProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use DOMElement;
use Masterminds\HTML5;
use Twig_Environment;
use Twig_Error_Runtime;
use CSSFromHTMLExtractor\Twig\Extension as ExtractorExtension;
Expand All @@ -25,11 +26,9 @@ public function process($rawHtml, $name = '', $context = [], $environment = null
{

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

/** @var ExtractorExtension $extractorExtension */
$extractorExtension = $environment->getExtension(ExtractorExtension::class);
foreach ($document->getElementsByTagName('link') as $linkTag) {
Expand Down Expand Up @@ -68,7 +67,7 @@ public function process($rawHtml, $name = '', $context = [], $environment = null
$headStyle = new DOMElement('style', $criticalCss);
$document->getElementsByTagName('head')->item(0)->appendChild($headStyle);

return html_entity_decode($document->saveHTML(),null,'unicode');
return $html5->saveHTML($document);
} catch (\Exception $exception) {
error_log($exception->getMessage());
return $rawHtml;
Expand Down

0 comments on commit b40feef

Please sign in to comment.