1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TijsVerkoyen\CssToInlineStyles; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\CssSelector\CssSelector; |
6
|
|
|
use Symfony\Component\CssSelector\CssSelectorConverter; |
7
|
|
|
use Symfony\Component\CssSelector\Exception\ExceptionInterface; |
8
|
|
|
use TijsVerkoyen\CssToInlineStyles\Css\Processor; |
9
|
|
|
use TijsVerkoyen\CssToInlineStyles\Css\Property\Processor as PropertyProcessor; |
10
|
|
|
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Processor as RuleProcessor; |
11
|
|
|
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Rule; |
12
|
|
|
|
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
|
|
|
* Inle 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 = $this->getInlineStyles($element); |
68
|
|
|
|
69
|
|
|
if (!empty($inlineProperties)) { |
70
|
|
|
foreach ($inlineProperties as $property) { |
71
|
|
|
$cssProperties[$property->getName()] = $property; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach ($properties as $property) { |
76
|
|
|
if (!isset($cssProperties[$property->getName()])) { |
77
|
|
|
$cssProperties[$property->getName()] = $property; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$rules = array(); |
82
|
|
|
foreach ($cssProperties as $property) { |
83
|
|
|
$rules[] = $property->toString(); |
84
|
|
|
} |
85
|
|
|
$element->setAttribute('style', implode(' ', $rules)); |
86
|
|
|
|
87
|
|
|
return $element; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the current inline styles for a given DOMElement |
92
|
|
|
* |
93
|
|
|
* @param \DOMElement $element |
94
|
|
|
* @return Css\Property\Property[] |
95
|
|
|
*/ |
96
|
|
|
public function getInlineStyles(\DOMElement $element) |
97
|
|
|
{ |
98
|
|
|
$processor = new PropertyProcessor(); |
99
|
|
|
|
100
|
|
|
return $processor->convertArrayToObjects( |
101
|
|
|
$processor->splitIntoSeparateProperties( |
102
|
|
|
$element->getAttribute('style') |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $html |
109
|
|
|
* @return \DOMDocument |
110
|
|
|
*/ |
111
|
|
|
protected function createDomDocumentFromHtml($html) |
112
|
|
|
{ |
113
|
|
|
$document = new \DOMDocument('1.0', 'UTF-8'); |
114
|
|
|
$internalErrors = libxml_use_internal_errors(true); |
115
|
|
|
$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); |
116
|
|
|
libxml_use_internal_errors($internalErrors); |
117
|
|
|
$document->formatOutput = true; |
118
|
|
|
|
119
|
|
|
return $document; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param \DOMDocument $document |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
protected function getHtmlFromDocument(\DOMDocument $document) |
127
|
|
|
{ |
128
|
|
|
// retrieve the document element |
129
|
|
|
// we do it this way to preserve the utf-8 encoding |
130
|
|
|
$htmlElement = $document->documentElement; |
131
|
|
|
$html = $document->saveHTML($htmlElement); |
132
|
|
|
$html = trim($html); |
133
|
|
|
|
134
|
|
|
// retrieve the doctype |
135
|
|
|
$document->removeChild($htmlElement); |
136
|
|
|
$doctype = $document->saveHTML(); |
137
|
|
|
$doctype = trim($doctype); |
138
|
|
|
|
139
|
|
|
// if it is the html5 doctype convert it to lowercase |
140
|
|
|
if ($doctype === '<!DOCTYPE html>') { |
141
|
|
|
$doctype = strtolower($doctype); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $doctype."\n".$html; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \DOMDocument $document |
149
|
|
|
* @param Css\Rule\Rule[] $rules |
150
|
|
|
* @return \DOMDocument |
151
|
|
|
*/ |
152
|
|
|
protected function inline(\DOMDocument $document, array $rules) |
153
|
|
|
{ |
154
|
|
|
if (empty($rules)) { |
155
|
|
|
return $document; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$xPath = new \DOMXPath($document); |
159
|
|
|
|
160
|
|
|
usort($rules, array(RuleProcessor::class, 'sortOnSpecificity')); |
161
|
|
|
|
162
|
|
|
foreach ($rules as $rule) { |
163
|
|
|
try { |
164
|
|
|
if (null !== $this->cssConverter) { |
165
|
|
|
$expression = $this->cssConverter->toXPath($rule->getSelector()); |
166
|
|
|
} else { |
167
|
|
|
// Compatibility layer for Symfony 2.7 and older |
168
|
|
|
$expression = CssSelector::toXPath($rule->getSelector()); |
169
|
|
|
} |
170
|
|
|
} catch (ExceptionInterface $e) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$elements = $xPath->query($expression); |
175
|
|
|
|
176
|
|
|
if ($elements === false) { |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
foreach ($elements as $element) { |
181
|
|
|
$this->calculatePropertiesToBeApplied($element, $rule->getProperties()); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$elements = $xPath->query('//*[@data-css-to-inline-styles]'); |
186
|
|
|
|
187
|
|
|
foreach ($elements as $element) { |
188
|
|
|
$propertiesToBeApplied = $element->attributes->getNamedItem('data-css-to-inline-styles'); |
189
|
|
|
$element->removeAttribute('data-css-to-inline-styles'); |
190
|
|
|
|
191
|
|
|
if ($propertiesToBeApplied !== null) { |
192
|
|
|
$properties = unserialize(base64_decode($propertiesToBeApplied->value)); |
193
|
|
|
$this->inlineCssOnElement($element, $properties); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $document; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Store the calculated values in a temporary data-attribute |
202
|
|
|
* |
203
|
|
|
* @param \DOMElement $element |
204
|
|
|
* @param Css\Property\Property[] $properties |
205
|
|
|
* @return \DOMElement |
206
|
|
|
*/ |
207
|
|
|
private function calculatePropertiesToBeApplied( |
208
|
|
|
\DOMElement $element, |
209
|
|
|
array $properties |
210
|
|
|
) { |
211
|
|
|
if (empty($properties)) { |
212
|
|
|
return $element; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$cssProperties = array(); |
216
|
|
|
$currentStyles = $element->attributes->getNamedItem('data-css-to-inline-styles'); |
217
|
|
|
|
218
|
|
|
if ($currentStyles !== null) { |
219
|
|
|
$currentProperties = unserialize( |
220
|
|
|
base64_decode( |
221
|
|
|
$currentStyles->value |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
foreach ($currentProperties as $property) { |
226
|
|
|
$cssProperties[$property->getName()] = $property; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
foreach ($properties as $property) { |
231
|
|
|
if (isset($cssProperties[$property->getName()])) { |
232
|
|
|
$existingProperty = $cssProperties[$property->getName()]; |
233
|
|
|
|
234
|
|
|
//skip check to overrule if existing property is important and current is not |
235
|
|
|
if ($existingProperty->isImportant() && !$property->isImportant()) { |
236
|
|
|
continue; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
//overrule if current property is important and existing is not, else check specificity |
240
|
|
|
$overrule = !$existingProperty->isImportant() && $property->isImportant(); |
241
|
|
|
if (!$overrule) { |
242
|
|
|
$overrule = $existingProperty->getOriginalSpecificity()->compareTo($property->getOriginalSpecificity()) <= 0; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if ($overrule) { |
246
|
|
|
unset($cssProperties[$property->getName()]); |
247
|
|
|
$cssProperties[$property->getName()] = $property; |
248
|
|
|
} |
249
|
|
|
} else { |
250
|
|
|
$cssProperties[$property->getName()] = $property; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$element->setAttribute( |
255
|
|
|
'data-css-to-inline-styles', |
256
|
|
|
base64_encode( |
257
|
|
|
serialize( |
258
|
|
|
array_values($cssProperties) |
259
|
|
|
) |
260
|
|
|
) |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
return $element; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|