mailpoet
/
vendor-prefixed
/
pelago
/
emogrifier
/
src
/
HtmlProcessor
/
CssToAttributeConverter.php
AbstractHtmlProcessor.php
2 years ago
CssToAttributeConverter.php
2 years ago
HtmlNormalizer.php
2 years ago
HtmlPruner.php
2 years ago
index.php
2 years ago
CssToAttributeConverter.php
141 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Pelago\Emogrifier\HtmlProcessor; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | class CssToAttributeConverter extends AbstractHtmlProcessor |
| 6 | { |
| 7 | private $cssToHtmlMap = ['background-color' => ['attribute' => 'bgcolor'], 'text-align' => ['attribute' => 'align', 'nodes' => ['p', 'div', 'td', 'th'], 'values' => ['left', 'right', 'center', 'justify']], 'float' => ['attribute' => 'align', 'nodes' => ['table', 'img'], 'values' => ['left', 'right']], 'border-spacing' => ['attribute' => 'cellspacing', 'nodes' => ['table']]]; |
| 8 | private static $parsedCssCache = []; |
| 9 | public function convertCssToVisualAttributes() : self |
| 10 | { |
| 11 | foreach ($this->getAllNodesWithStyleAttribute() as $node) { |
| 12 | $inlineStyleDeclarations = $this->parseCssDeclarationsBlock($node->getAttribute('style')); |
| 13 | $this->mapCssToHtmlAttributes($inlineStyleDeclarations, $node); |
| 14 | } |
| 15 | return $this; |
| 16 | } |
| 17 | private function getAllNodesWithStyleAttribute() : \DOMNodeList |
| 18 | { |
| 19 | return $this->getXPath()->query('//*[@style]'); |
| 20 | } |
| 21 | private function parseCssDeclarationsBlock(string $cssDeclarationsBlock) : array |
| 22 | { |
| 23 | if (isset(self::$parsedCssCache[$cssDeclarationsBlock])) { |
| 24 | return self::$parsedCssCache[$cssDeclarationsBlock]; |
| 25 | } |
| 26 | $properties = []; |
| 27 | foreach (\preg_split('/;(?!base64|charset)/', $cssDeclarationsBlock) as $declaration) { |
| 28 | $matches = []; |
| 29 | if (!\preg_match('/^([A-Za-z\\-]+)\\s*:\\s*(.+)$/s', \trim($declaration), $matches)) { |
| 30 | continue; |
| 31 | } |
| 32 | $propertyName = \strtolower($matches[1]); |
| 33 | $propertyValue = $matches[2]; |
| 34 | $properties[$propertyName] = $propertyValue; |
| 35 | } |
| 36 | self::$parsedCssCache[$cssDeclarationsBlock] = $properties; |
| 37 | return $properties; |
| 38 | } |
| 39 | private function mapCssToHtmlAttributes(array $styles, \DOMElement $node) : void |
| 40 | { |
| 41 | foreach ($styles as $property => $value) { |
| 42 | // Strip !important indicator |
| 43 | $value = \trim(\str_replace('!important', '', $value)); |
| 44 | $this->mapCssToHtmlAttribute($property, $value, $node); |
| 45 | } |
| 46 | } |
| 47 | private function mapCssToHtmlAttribute(string $property, string $value, \DOMElement $node) : void |
| 48 | { |
| 49 | if (!$this->mapSimpleCssProperty($property, $value, $node)) { |
| 50 | $this->mapComplexCssProperty($property, $value, $node); |
| 51 | } |
| 52 | } |
| 53 | private function mapSimpleCssProperty(string $property, string $value, \DOMElement $node) : bool |
| 54 | { |
| 55 | if (!isset($this->cssToHtmlMap[$property])) { |
| 56 | return \false; |
| 57 | } |
| 58 | $mapping = $this->cssToHtmlMap[$property]; |
| 59 | $nodesMatch = !isset($mapping['nodes']) || \in_array($node->nodeName, $mapping['nodes'], \true); |
| 60 | $valuesMatch = !isset($mapping['values']) || \in_array($value, $mapping['values'], \true); |
| 61 | $canBeMapped = $nodesMatch && $valuesMatch; |
| 62 | if ($canBeMapped) { |
| 63 | $node->setAttribute($mapping['attribute'], $value); |
| 64 | } |
| 65 | return $canBeMapped; |
| 66 | } |
| 67 | private function mapComplexCssProperty(string $property, string $value, \DOMElement $node) : void |
| 68 | { |
| 69 | switch ($property) { |
| 70 | case 'background': |
| 71 | $this->mapBackgroundProperty($node, $value); |
| 72 | break; |
| 73 | case 'width': |
| 74 | // intentional fall-through |
| 75 | case 'height': |
| 76 | $this->mapWidthOrHeightProperty($node, $value, $property); |
| 77 | break; |
| 78 | case 'margin': |
| 79 | $this->mapMarginProperty($node, $value); |
| 80 | break; |
| 81 | case 'border': |
| 82 | $this->mapBorderProperty($node, $value); |
| 83 | break; |
| 84 | default: |
| 85 | } |
| 86 | } |
| 87 | private function mapBackgroundProperty(\DOMElement $node, string $value) : void |
| 88 | { |
| 89 | // parse out the color, if any |
| 90 | $styles = \explode(' ', $value, 2); |
| 91 | $first = $styles[0]; |
| 92 | if (\is_numeric($first[0]) || \strncmp($first, 'url', 3) === 0) { |
| 93 | return; |
| 94 | } |
| 95 | // as this is not a position or image, assume it's a color |
| 96 | $node->setAttribute('bgcolor', $first); |
| 97 | } |
| 98 | private function mapWidthOrHeightProperty(\DOMElement $node, string $value, string $property) : void |
| 99 | { |
| 100 | // only parse values in px and %, but not values like "auto" |
| 101 | if (!\preg_match('/^(\\d+)(\\.(\\d+))?(px|%)$/', $value)) { |
| 102 | return; |
| 103 | } |
| 104 | $number = \preg_replace('/[^0-9.%]/', '', $value); |
| 105 | $node->setAttribute($property, $number); |
| 106 | } |
| 107 | private function mapMarginProperty(\DOMElement $node, string $value) : void |
| 108 | { |
| 109 | if (!$this->isTableOrImageNode($node)) { |
| 110 | return; |
| 111 | } |
| 112 | $margins = $this->parseCssShorthandValue($value); |
| 113 | if ($margins['left'] === 'auto' && $margins['right'] === 'auto') { |
| 114 | $node->setAttribute('align', 'center'); |
| 115 | } |
| 116 | } |
| 117 | private function mapBorderProperty(\DOMElement $node, string $value) : void |
| 118 | { |
| 119 | if (!$this->isTableOrImageNode($node)) { |
| 120 | return; |
| 121 | } |
| 122 | if ($value === 'none' || $value === '0') { |
| 123 | $node->setAttribute('border', '0'); |
| 124 | } |
| 125 | } |
| 126 | private function isTableOrImageNode(\DOMElement $node) : bool |
| 127 | { |
| 128 | return $node->nodeName === 'table' || $node->nodeName === 'img'; |
| 129 | } |
| 130 | private function parseCssShorthandValue(string $value) : array |
| 131 | { |
| 132 | $values = \preg_split('/\\s+/', $value); |
| 133 | $css = []; |
| 134 | $css['top'] = $values[0]; |
| 135 | $css['right'] = \count($values) > 1 ? $values[1] : $css['top']; |
| 136 | $css['bottom'] = \count($values) > 2 ? $values[2] : $css['top']; |
| 137 | $css['left'] = \count($values) > 3 ? $values[3] : $css['right']; |
| 138 | return $css; |
| 139 | } |
| 140 | } |
| 141 |