N°7177 Update pelago/emogrifier from 6.0.0 to 7.2.0

This commit is contained in:
Pierre Goiffon
2024-01-26 16:50:33 +01:00
parent 962eb08e40
commit e92b006f70
15 changed files with 318 additions and 136 deletions

View File

@@ -13,6 +13,7 @@ use Sabberworm\CSS\Property\Import as CssImport;
use Sabberworm\CSS\Renderable as CssRenderable;
use Sabberworm\CSS\RuleSet\DeclarationBlock as CssDeclarationBlock;
use Sabberworm\CSS\RuleSet\RuleSet as CssRuleSet;
use Sabberworm\CSS\Settings as ParserSettings;
/**
* Parses and stores a CSS document from a string of CSS, and provides methods to obtain the CSS in parts or as data
@@ -37,13 +38,30 @@ class CssDocument
/**
* @param string $css
* @param bool $debug
* If this is `true`, an exception will be thrown if invalid CSS is encountered.
* Otherwise the parser will try to do the best it can.
*/
public function __construct(string $css)
public function __construct(string $css, bool $debug)
{
$cssParser = new CssParser($css);
/** @var SabberwormCssDocument $sabberwormCssDocument */
$sabberwormCssDocument = $cssParser->parse();
$this->sabberwormCssDocument = $sabberwormCssDocument;
// CSS Parser currently throws exception with nested at-rules (like `@media`) in strict parsing mode
$parserSettings = ParserSettings::create()->withLenientParsing(!$debug || $this->hasNestedAtRule($css));
// CSS Parser currently throws exception with non-empty whitespace-only CSS in strict parsing mode, so `trim()`
// @see https://github.com/sabberworm/PHP-CSS-Parser/issues/349
$this->sabberwormCssDocument = (new CssParser(\trim($css), $parserSettings))->parse();
}
/**
* Tests if a string of CSS appears to contain an at-rule with nested rules
* (`@media`, `@supports`, `@keyframes`, `@document`,
* the latter two additionally with vendor prefixes that may commonly be used).
*
* @see https://github.com/sabberworm/PHP-CSS-Parser/issues/127
*/
private function hasNestedAtRule(string $css): bool
{
return \preg_match('/@(?:media|supports|(?:-webkit-|-moz-|-ms-|-o-)?+(keyframes|document))\\b/', $css) === 1;
}
/**
@@ -51,7 +69,7 @@ class CssDocument
*
* @param array<array-key, string> $allowedMediaTypes
*
* @return array<int, StyleRule>
* @return list<StyleRule>
*/
public function getStyleRulesData(array $allowedMediaTypes): array
{
@@ -86,7 +104,6 @@ class CssDocument
public function renderNonConditionalAtRules(): string
{
$this->isImportRuleAllowed = true;
/** @var array<int, CssRenderable> $cssContents */
$cssContents = $this->sabberwormCssDocument->getContents();
$atRules = \array_filter($cssContents, [$this, 'isValidAtRuleToRender']);
@@ -97,9 +114,7 @@ class CssDocument
$atRulesDocument = new SabberwormCssDocument();
$atRulesDocument->setContents($atRules);
/** @var string $renderedRules */
$renderedRules = $atRulesDocument->render();
return $renderedRules;
return $atRulesDocument->render();
}
/**
@@ -115,7 +130,6 @@ class CssDocument
$result = null;
if ($rule->atRuleName() === 'media') {
/** @var string $mediaQueryList */
$mediaQueryList = $rule->atRuleArgs();
[$mediaType] = \explode('(', $mediaQueryList, 2);
if (\trim($mediaType) !== '') {

View File

@@ -161,7 +161,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @param string $css the CSS to inline, must be UTF-8-encoded
*
* @return self fluent interface
* @return $this
*
* @throws ParseException in debug mode, if an invalid selector is encountered
* @throws \RuntimeException in debug mode, if an internal PCRE error occurs
@@ -179,7 +179,7 @@ class CssInliner extends AbstractHtmlProcessor
if ($this->isStyleBlocksParsingEnabled) {
$combinedCss .= $this->getCssFromAllStyleNodes();
}
$parsedCss = new CssDocument($combinedCss);
$parsedCss = new CssDocument($combinedCss, $this->debug);
$excludedNodes = $this->getNodesToExclude();
$cssRules = $this->collateCssRules($parsedCss);
@@ -218,7 +218,7 @@ class CssInliner extends AbstractHtmlProcessor
/**
* Disables the parsing of inline styles.
*
* @return self fluent interface
* @return $this
*/
public function disableInlineStyleAttributesParsing(): self
{
@@ -230,7 +230,7 @@ class CssInliner extends AbstractHtmlProcessor
/**
* Disables the parsing of `<style>` blocks.
*
* @return self fluent interface
* @return $this
*/
public function disableStyleBlocksParsing(): self
{
@@ -244,7 +244,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @param string $mediaName the media type name, e.g., "braille"
*
* @return self fluent interface
* @return $this
*/
public function addAllowedMediaType(string $mediaName): self
{
@@ -258,7 +258,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @param string $mediaName the tag name, e.g., "braille"
*
* @return self fluent interface
* @return $this
*/
public function removeAllowedMediaType(string $mediaName): self
{
@@ -276,7 +276,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @param string $selector the selector to exclude, e.g., ".editor"
*
* @return self fluent interface
* @return $this
*/
public function addExcludedSelector(string $selector): self
{
@@ -290,7 +290,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @param string $selector the selector to no longer exclude, e.g., ".editor"
*
* @return self fluent interface
* @return $this
*/
public function removeExcludedSelector(string $selector): self
{
@@ -306,7 +306,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @param bool $debug set to true to enable debug mode
*
* @return self fluent interface
* @return $this
*/
public function setDebug(bool $debug): self
{
@@ -463,7 +463,7 @@ class CssInliner extends AbstractHtmlProcessor
$properties = [];
foreach (\preg_split('/;(?!base64|charset)/', $cssDeclarationsBlock) as $declaration) {
/** @var array<int, string> $matches */
/** @var list<string> $matches */
$matches = [];
if (!\preg_match('/^([A-Za-z\\-]+)\\s*:\\s*(.+)$/s', \trim($declaration), $matches)) {
continue;
@@ -492,7 +492,9 @@ class CssInliner extends AbstractHtmlProcessor
$css = '';
foreach ($styleNodes as $styleNode) {
$css .= "\n\n" . $styleNode->nodeValue;
if (\is_string($styleNode->nodeValue)) {
$css .= "\n\n" . $styleNode->nodeValue;
}
$parentNode = $styleNode->parentNode;
if ($parentNode instanceof \DOMNode) {
$parentNode->removeChild($styleNode);
@@ -505,7 +507,7 @@ class CssInliner extends AbstractHtmlProcessor
/**
* Find the nodes that are not to be emogrified.
*
* @return array<int, \DOMElement>
* @return list<\DOMElement>
*
* @throws ParseException
* @throws \UnexpectedValueException
@@ -608,8 +610,8 @@ class CssInliner extends AbstractHtmlProcessor
\usort(
$cssRules['inlinable'],
/**
* @param array{selector: string, line: int} $first
* @param array{selector: string, line: int} $second
* @param array{selector: string, line: int, ...} $first
* @param array{selector: string, line: int, ...} $second
*/
function (array $first, array $second): int {
return $this->sortBySelectorPrecedence($first, $second);
@@ -667,8 +669,8 @@ class CssInliner extends AbstractHtmlProcessor
}
/**
* @param array{selector: string, line: int} $first
* @param array{selector: string, line: int} $second
* @param array{selector: string, line: int, ...} $first
* @param array{selector: string, line: int, ...} $second
*
* @return int
*/
@@ -1146,7 +1148,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* This method only supports strings, not arrays of strings.
*
* @param string $pattern
* @param non-empty-string $pattern
* @param string $replacement
* @param string $subject
*

View File

@@ -240,7 +240,7 @@ abstract class AbstractHtmlProcessor
{
$domDocument = new \DOMDocument();
$domDocument->strictErrorChecking = false;
$domDocument->formatOutput = true;
$domDocument->formatOutput = false;
$libXmlState = \libxml_use_internal_errors(true);
$domDocument->loadHTML($this->prepareHtmlForDomConversion($html));
\libxml_clear_errors();

View File

@@ -50,7 +50,7 @@ class CssToAttributeConverter extends AbstractHtmlProcessor
/**
* Maps the CSS from the style nodes to visual HTML attributes.
*
* @return self fluent interface
* @return $this
*/
public function convertCssToVisualAttributes(): self
{

View File

@@ -27,7 +27,7 @@ class HtmlPruner extends AbstractHtmlProcessor
/**
* Removes elements that have a "display: none;" style.
*
* @return self fluent interface
* @return $this
*/
public function removeElementsWithDisplayNone(): self
{
@@ -57,7 +57,7 @@ class HtmlPruner extends AbstractHtmlProcessor
*
* @param array<array-key, string> $classesToKeep names of classes that should not be removed
*
* @return self fluent interface
* @return $this
*/
public function removeRedundantClasses(array $classesToKeep = []): self
{
@@ -118,7 +118,7 @@ class HtmlPruner extends AbstractHtmlProcessor
*
* @param CssInliner $cssInliner object instance that performed the CSS inlining
*
* @return self fluent interface
* @return $this
*
* @throws \BadMethodCallException if `inlineCss` has not first been called on `$cssInliner`
*/