1 | <?php |
||
13 | class CssToInlineStyles |
||
14 | { |
||
15 | private $cssConverter; |
||
16 | |||
17 | public function __construct() |
||
18 | { |
||
19 | if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) { |
||
20 | $this->cssConverter = new CssSelectorConverter(); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Will inline the $css into the given $html |
||
26 | * |
||
27 | * Remark: if the html contains <style>-tags those will be used, the rules |
||
28 | * in $css will be appended. |
||
29 | * |
||
30 | * @param string $html |
||
31 | * @param string $css |
||
32 | * @return string |
||
33 | */ |
||
34 | public function convert($html, $css = null) |
||
35 | { |
||
36 | $document = $this->createDomDocumentFromHtml($html); |
||
37 | $processor = new Processor(); |
||
38 | |||
39 | // get all styles from the style-tags |
||
40 | $rules = $processor->getRules( |
||
41 | $processor->getCssFromStyleTags($html) |
||
42 | ); |
||
43 | |||
44 | if ($css !== null) { |
||
45 | $rules = $processor->getRules($css, $rules); |
||
46 | } |
||
47 | |||
48 | $document = $this->inline($document, $rules); |
||
49 | |||
50 | return $this->getHtmlFromDocument($document); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Inline the given properties on an given DOMElement |
||
55 | * |
||
56 | * @param \DOMElement $element |
||
57 | * @param Css\Property\Property[] $properties |
||
58 | * @return \DOMElement |
||
59 | */ |
||
60 | public function inlineCssOnElement(\DOMElement $element, array $properties) |
||
61 | { |
||
62 | if (empty($properties)) { |
||
63 | return $element; |
||
64 | } |
||
65 | |||
66 | $cssProperties = array(); |
||
67 | $inlineProperties = array(); |
||
68 | |||
69 | foreach ($this->getInlineStyles($element) as $property) { |
||
70 | $inlineProperties[$property->getName()] = $property; |
||
71 | } |
||
72 | |||
73 | foreach ($properties as $property) { |
||
74 | if (!isset($inlineProperties[$property->getName()])) { |
||
75 | $cssProperties[$property->getName()] = $property; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | $rules = array(); |
||
80 | foreach (array_merge($cssProperties, $inlineProperties) as $property) { |
||
81 | $rules[] = $property->toString(); |
||
82 | } |
||
83 | $element->setAttribute('style', implode(' ', $rules)); |
||
84 | |||
85 | return $element; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get the current inline styles for a given DOMElement |
||
90 | * |
||
91 | * @param \DOMElement $element |
||
92 | * @return Css\Property\Property[] |
||
93 | */ |
||
94 | public function getInlineStyles(\DOMElement $element) |
||
104 | |||
105 | /** |
||
106 | * @param string $html |
||
107 | * @return \DOMDocument |
||
108 | */ |
||
109 | protected function createDomDocumentFromHtml($html) |
||
119 | |||
120 | /** |
||
121 | * @param \DOMDocument $document |
||
122 | * @return string |
||
123 | */ |
||
124 | protected function getHtmlFromDocument(\DOMDocument $document) |
||
144 | |||
145 | /** |
||
146 | * @param \DOMDocument $document |
||
147 | * @param Css\Rule\Rule[] $rules |
||
148 | * @return \DOMDocument |
||
149 | */ |
||
150 | protected function inline(\DOMDocument $document, array $rules) |
||
194 | |||
195 | /** |
||
196 | * Merge the CSS rules to determine the applied properties. |
||
197 | * |
||
198 | * @param Css\Property\Property[] $properties |
||
199 | * @param Css\Property\Property[] $cssProperties existing applied properties indexed by name |
||
200 | * |
||
201 | * @return Css\Property\Property[] updated properties, indexed by name |
||
202 | */ |
||
203 | private function calculatePropertiesToBeApplied(array $properties, array $cssProperties) |
||
235 | } |
||
236 |