updated streamline-setup v2

This commit is contained in:
2025-01-15 08:53:49 -08:00
committed by alec.turner
parent a2ce9248f0
commit 4b569f81b0
20228 changed files with 2932048 additions and 63204 deletions
@@ -2,22 +2,23 @@
namespace TijsVerkoyen\CssToInlineStyles;
use Symfony\Component\CssSelector\CssSelector;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\CssSelector\Exception\ExceptionInterface;
use TijsVerkoyen\CssToInlineStyles\Css\Processor;
use TijsVerkoyen\CssToInlineStyles\Css\Property\Processor as PropertyProcessor;
use TijsVerkoyen\CssToInlineStyles\Css\Property\Property;
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Processor as RuleProcessor;
class CssToInlineStyles
{
/**
* @var CssSelectorConverter
*/
private $cssConverter;
public function __construct()
{
if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) {
$this->cssConverter = new CssSelectorConverter();
}
$this->cssConverter = new CssSelectorConverter();
}
/**
@@ -51,10 +52,10 @@ class CssToInlineStyles
}
/**
* Inline the given properties on an given DOMElement
* Inline the given properties on a given DOMElement
*
* @param \DOMElement $element
* @param Css\Property\Property[] $properties
* @param Property[] $properties
*
* @return \DOMElement
*/
@@ -91,7 +92,7 @@ class CssToInlineStyles
*
* @param \DOMElement $element
*
* @return Css\Property\Property[]
* @return Property[]
*/
public function getInlineStyles(\DOMElement $element)
{
@@ -130,12 +131,25 @@ class CssToInlineStyles
// retrieve the document element
// we do it this way to preserve the utf-8 encoding
$htmlElement = $document->documentElement;
if ($htmlElement === null) {
throw new \RuntimeException('Failed to get HTML from empty document.');
}
$html = $document->saveHTML($htmlElement);
if ($html === false) {
throw new \RuntimeException('Failed to get HTML from document.');
}
$html = trim($html);
// retrieve the doctype
$document->removeChild($htmlElement);
$doctype = $document->saveHTML();
if ($doctype === false) {
$doctype = '';
}
$doctype = trim($doctype);
// if it is the html5 doctype convert it to lowercase
@@ -158,6 +172,7 @@ class CssToInlineStyles
return $document;
}
/** @var \SplObjectStorage<\DOMElement, array<string, Property>> $propertyStorage */
$propertyStorage = new \SplObjectStorage();
$xPath = new \DOMXPath($document);
@@ -166,12 +181,7 @@ class CssToInlineStyles
foreach ($rules as $rule) {
try {
if (null !== $this->cssConverter) {
$expression = $this->cssConverter->toXPath($rule->getSelector());
} else {
// Compatibility layer for Symfony 2.7 and older
$expression = CssSelector::toXPath($rule->getSelector());
}
$expression = $this->cssConverter->toXPath($rule->getSelector());
} catch (ExceptionInterface $e) {
continue;
}
@@ -183,6 +193,7 @@ class CssToInlineStyles
}
foreach ($elements as $element) {
\assert($element instanceof \DOMElement);
$propertyStorage[$element] = $this->calculatePropertiesToBeApplied(
$rule->getProperties(),
$propertyStorage->contains($element) ? $propertyStorage[$element] : array()
@@ -200,12 +211,12 @@ class CssToInlineStyles
/**
* Merge the CSS rules to determine the applied properties.
*
* @param Css\Property\Property[] $properties
* @param Css\Property\Property[] $cssProperties existing applied properties indexed by name
* @param Property[] $properties
* @param array<string, Property> $cssProperties existing applied properties indexed by name
*
* @return Css\Property\Property[] updated properties, indexed by name
* @return array<string, Property> updated properties, indexed by name
*/
private function calculatePropertiesToBeApplied(array $properties, array $cssProperties)
private function calculatePropertiesToBeApplied(array $properties, array $cssProperties): array
{
if (empty($properties)) {
return $cssProperties;
@@ -223,6 +234,8 @@ class CssToInlineStyles
//overrule if current property is important and existing is not, else check specificity
$overrule = !$existingProperty->isImportant() && $property->isImportant();
if (!$overrule) {
\assert($existingProperty->getOriginalSpecificity() !== null, 'Properties created for parsed CSS always have their associated specificity.');
\assert($property->getOriginalSpecificity() !== null, 'Properties created for parsed CSS always have their associated specificity.');
$overrule = $existingProperty->getOriginalSpecificity()->compareTo($property->getOriginalSpecificity()) <= 0;
}