Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #23 from murwa/master
Browse files Browse the repository at this point in the history
Upgrade emogrifier to v5
  • Loading branch information
tonchik-tm authored May 13, 2021
2 parents 054d3dc + 124d0c0 commit 7b823d5
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
vendor/
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"pelago/emogrifier": "^2.0.0"
"pelago/emogrifier": "^5.0.0",
"ext-dom": "*",
"ext-fileinfo": "*"
},

"autoload": {
Expand Down
165 changes: 165 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use DOMDocument;
use DOMXPath;
use Pelago\Emogrifier;
use Pelago\Emogrifier\CssInliner;

/**
* This class creates a collection of html pages with some improvements.
Expand All @@ -30,7 +30,7 @@ class Html extends Base
'outputDir' => ''
];

public function __construct($options=[])
public function __construct($options = [])
{
$this->setOptions(array_replace_recursive($this->defaultOptions, $options));
}
Expand Down Expand Up @@ -81,12 +81,12 @@ public function getAllPages()
* The method replaces css class to inline css rules.
* @param $content
* @return string
* @throws \Symfony\Component\CssSelector\Exception\ParseException
*/
private function setInlineCss($content)
{
$content = str_replace(['<!--', '-->'], '', $content);
$parser = new Emogrifier($content);
return $parser->emogrify();
return CssInliner::fromHtml($content)->inlineCss()->render();
}

/**
Expand All @@ -102,7 +102,8 @@ private function setInlineImages($content)
$xpath->registerNamespace("xml", "http://www.w3.org/1999/xhtml");

$images = $xpath->query("//img");
foreach ($images as $img) { /** @var \DOMNode $img */
foreach ($images as $img) {
/** @var \DOMNode $img */
$attrImage = $img->getAttribute('src');
$pi = pathinfo($attrImage);
$image = $this->getOutputDir() . '/' . $pi['basename'];
Expand All @@ -128,7 +129,7 @@ private function setOnlyContent($content)

$html = '';
$body = $xpath->query("//body")->item(0);
foreach($body->childNodes as $node) {
foreach ($body->childNodes as $node) {
$html .= $dom->saveHTML($node);
}
unset($dom, $xpath, $body, $content);
Expand Down

0 comments on commit 7b823d5

Please sign in to comment.