1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace voku\CssToInlineStyles; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\CssSelector\CssSelectorConverter; |
6
|
|
|
use Symfony\Component\CssSelector\Exception\ExceptionInterface; |
7
|
|
|
use voku\helper\HtmlDomParser; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* CSS to Inline Styles class |
11
|
|
|
* |
12
|
|
|
* @author Tijs Verkoyen <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CssToInlineStyles |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* regular expression: css media queries |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $cssMediaQueriesRegEx = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* regular expression: css charset |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private static $cssCharsetRegEx = '/@charset [\'"][^\'"]+[\'"];/i'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* regular expression: conditional inline style tags |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*-->/isU'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* regular expression: inline style tags |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private static $styleTagRegEx = '|<style(.*)>(.*)</style>|isU'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* regular expression: css-comments |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The CSS to use |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $css; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Should the generated HTML be cleaned |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
private $cleanup = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The encoding to use. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $encoding = 'UTF-8'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The HTML to process |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private $html; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Use inline-styles block as CSS |
82
|
|
|
* |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
private $useInlineStylesBlock = false; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Use link block reference as CSS |
89
|
|
|
* |
90
|
|
|
* @var bool |
91
|
|
|
*/ |
92
|
|
|
private $loadCSSFromHTML = false; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Strip original style tags |
96
|
|
|
* |
97
|
|
|
* @var bool |
98
|
|
|
*/ |
99
|
|
|
private $stripOriginalStyleTags = false; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Exclude conditional inline-style blocks |
103
|
|
|
* |
104
|
|
|
* @var bool |
105
|
|
|
*/ |
106
|
|
|
private $excludeConditionalInlineStylesBlock = true; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
110
|
|
|
* |
111
|
|
|
* @var bool |
112
|
|
|
*/ |
113
|
|
|
private $excludeMediaQueries = true; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
117
|
|
|
* |
118
|
|
|
* @var bool |
119
|
|
|
*/ |
120
|
|
|
private $excludeCssCharset = true; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Creates an instance, you could set the HTML and CSS here, or load it |
124
|
|
|
* later. |
125
|
|
|
* |
126
|
|
|
* @param null|string $html The HTML to process. |
127
|
|
|
* @param null|string $css The CSS to use. |
128
|
|
|
*/ |
129
|
50 |
|
public function __construct($html = null, $css = null) |
130
|
|
|
{ |
131
|
50 |
|
if (null !== $html) { |
132
|
2 |
|
$this->setHTML($html); |
133
|
|
|
} |
134
|
|
|
|
135
|
50 |
|
if (null !== $css) { |
136
|
2 |
|
$this->setCSS($css); |
137
|
|
|
} |
138
|
50 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set HTML to process |
142
|
|
|
* |
143
|
|
|
* @param string $html The HTML to process. |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
48 |
|
public function setHTML($html) |
148
|
|
|
{ |
149
|
|
|
// strip style definitions, if we use css-class "cleanup" on a style-element |
150
|
48 |
|
$this->html = (string)preg_replace('/<style[^>]+class="cleanup"[^>]*>.*<\/style>/Usi', ' ', $html); |
151
|
|
|
|
152
|
48 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set CSS to use |
157
|
|
|
* |
158
|
|
|
* @param string $css The CSS to use. |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
44 |
|
public function setCSS($css) |
163
|
|
|
{ |
164
|
44 |
|
$this->css = (string)$css; |
165
|
|
|
|
166
|
44 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sort an array on the specificity element |
171
|
|
|
* |
172
|
|
|
* @return int |
173
|
|
|
* |
174
|
|
|
* @param Specificity[] $e1 The first element. |
175
|
|
|
* @param Specificity[] $e2 The second element. |
176
|
|
|
*/ |
177
|
17 |
|
private static function sortOnSpecificity($e1, $e2) |
178
|
|
|
{ |
179
|
|
|
// Compare the specificity |
180
|
17 |
|
$value = $e1['specificity']->compareTo($e2['specificity']); |
181
|
|
|
|
182
|
|
|
// if the specificity is the same, use the order in which the element appeared |
183
|
17 |
|
if (0 === $value) { |
184
|
12 |
|
$value = $e1['order'] - $e2['order']; |
185
|
|
|
} |
186
|
|
|
|
187
|
17 |
|
return $value; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
192
|
|
|
* |
193
|
|
|
* @param bool $outputXHTML [optional] Should we output valid XHTML? |
194
|
|
|
* @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
195
|
|
|
* you may also use the options parameter to specify additional |
196
|
|
|
* Libxml parameters. Recommend these options: |
197
|
|
|
* LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
198
|
|
|
* @param bool $path [optional] Set the path to your external css-files. |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
* |
202
|
|
|
* @throws Exception |
203
|
|
|
*/ |
204
|
48 |
|
public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
// redefine |
207
|
48 |
|
$outputXHTML = (bool)$outputXHTML; |
208
|
|
|
|
209
|
|
|
// validate |
210
|
48 |
|
if (!$this->html) { |
211
|
1 |
|
throw new Exception('No HTML provided.'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// use local variables |
215
|
47 |
|
$css = $this->css; |
216
|
|
|
|
217
|
|
|
// create new HtmlDomParser |
218
|
47 |
|
$dom = HtmlDomParser::str_get_html($this->html); |
|
|
|
|
219
|
|
|
|
220
|
|
|
// check if there is some link css reference |
221
|
47 |
|
if ($this->loadCSSFromHTML) { |
222
|
1 |
|
foreach ($dom->find('link') as $node) { |
223
|
|
|
|
224
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
225
|
1 |
|
$file = ($path ?: __DIR__) . '/' . $node->getAttribute('href'); |
226
|
|
|
|
227
|
1 |
|
if (file_exists($file)) { |
228
|
1 |
|
$css .= file_get_contents($file); |
229
|
|
|
|
230
|
|
|
// converting to inline css because we don't need/want to load css files, so remove the link |
231
|
1 |
|
$node->outertext = ''; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// should we use inline style-block |
237
|
47 |
|
if ($this->useInlineStylesBlock) { |
238
|
|
|
|
239
|
27 |
|
if (true === $this->excludeConditionalInlineStylesBlock) { |
240
|
23 |
|
$this->html = preg_replace(self::$excludeConditionalInlineStylesBlockRegEx, '', $this->html); |
241
|
|
|
} |
242
|
|
|
|
243
|
27 |
|
$css .= $this->getCssFromInlineHtmlStyleBlock($this->html); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// process css |
247
|
47 |
|
$cssRules = $this->processCSS($css); |
248
|
|
|
|
249
|
|
|
// create new XPath |
250
|
47 |
|
$xPath = $this->createXPath($dom->getDocument(), $cssRules); |
251
|
|
|
|
252
|
|
|
// strip original style tags if we need to |
253
|
47 |
|
if ($this->stripOriginalStyleTags === true) { |
254
|
13 |
|
$this->stripOriginalStyleTags($xPath); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// cleanup the HTML if we need to |
258
|
47 |
|
if (true === $this->cleanup) { |
259
|
3 |
|
$this->cleanupHTML($xPath); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// should we output XHTML? |
263
|
47 |
|
if (true === $outputXHTML) { |
264
|
6 |
|
return $dom->xml(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// just regular HTML 4.01 as it should be used in newsletters |
268
|
42 |
|
return $dom->html(); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* get css from inline-html style-block |
273
|
|
|
* |
274
|
|
|
* @param string $html |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
29 |
|
public function getCssFromInlineHtmlStyleBlock($html) |
279
|
|
|
{ |
280
|
|
|
// init var |
281
|
29 |
|
$css = ''; |
282
|
29 |
|
$matches = array(); |
283
|
|
|
|
284
|
|
|
// match the style blocks |
285
|
29 |
|
preg_match_all(self::$styleTagRegEx, $html, $matches); |
286
|
|
|
|
287
|
|
|
// any style-blocks found? |
288
|
29 |
|
if (!empty($matches[2])) { |
289
|
|
|
// add |
290
|
28 |
|
foreach ($matches[2] as $match) { |
291
|
28 |
|
$css .= trim($match) . "\n"; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
29 |
|
return $css; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Process the loaded CSS |
300
|
|
|
* |
301
|
|
|
* @param string $css |
302
|
|
|
* |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
47 |
|
private function processCSS($css) |
306
|
|
|
{ |
307
|
|
|
//reset current set of rules |
308
|
47 |
|
$cssRules = array(); |
309
|
|
|
|
310
|
|
|
// init vars |
311
|
47 |
|
$css = (string)$css; |
312
|
|
|
|
313
|
47 |
|
$css = $this->doCleanup($css); |
314
|
|
|
|
315
|
|
|
// rules are splitted by } |
316
|
47 |
|
$rules = (array)explode('}', $css); |
317
|
|
|
|
318
|
|
|
// init var |
319
|
47 |
|
$i = 1; |
320
|
|
|
|
321
|
|
|
// loop rules |
322
|
47 |
|
foreach ($rules as $rule) { |
323
|
|
|
// split into chunks |
324
|
47 |
|
$chunks = explode('{', $rule); |
325
|
|
|
|
326
|
|
|
// invalid rule? |
327
|
47 |
|
if (!isset($chunks[1])) { |
328
|
47 |
|
continue; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// set the selectors |
332
|
34 |
|
$selectors = trim($chunks[0]); |
333
|
|
|
|
334
|
|
|
// get cssProperties |
335
|
34 |
|
$cssProperties = trim($chunks[1]); |
336
|
|
|
|
337
|
|
|
// split multiple selectors |
338
|
34 |
|
$selectors = (array)explode(',', $selectors); |
339
|
|
|
|
340
|
|
|
// loop selectors |
341
|
34 |
|
foreach ($selectors as $selector) { |
342
|
|
|
// cleanup |
343
|
34 |
|
$selector = trim($selector); |
344
|
|
|
|
345
|
|
|
// build an array for each selector |
346
|
34 |
|
$ruleSet = array(); |
347
|
|
|
|
348
|
|
|
// store selector |
349
|
34 |
|
$ruleSet['selector'] = $selector; |
350
|
|
|
|
351
|
|
|
// process the properties |
352
|
34 |
|
$ruleSet['properties'] = $this->processCSSProperties($cssProperties); |
353
|
|
|
|
354
|
|
|
|
355
|
|
|
// calculate specificity |
356
|
34 |
|
$ruleSet['specificity'] = Specificity::fromSelector($selector); |
357
|
|
|
|
358
|
|
|
// remember the order in which the rules appear |
359
|
34 |
|
$ruleSet['order'] = $i; |
360
|
|
|
|
361
|
|
|
// add into rules |
362
|
34 |
|
$cssRules[] = $ruleSet; |
363
|
|
|
|
364
|
|
|
// increment |
365
|
34 |
|
$i++; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
// sort based on specificity |
370
|
47 |
|
if (0 !== count($cssRules)) { |
371
|
34 |
|
usort($cssRules, array(__CLASS__, 'sortOnSpecificity')); |
372
|
|
|
} |
373
|
|
|
|
374
|
47 |
|
return $cssRules; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param string $css |
379
|
|
|
* |
380
|
|
|
* @return string |
381
|
|
|
*/ |
382
|
47 |
|
private function doCleanup($css) |
383
|
|
|
{ |
384
|
|
|
// remove newlines & replace double quotes by single quotes |
385
|
47 |
|
$css = str_replace( |
386
|
47 |
|
array("\r", "\n", '"'), |
387
|
47 |
|
array('', '', '\''), |
388
|
|
|
$css |
389
|
|
|
); |
390
|
|
|
|
391
|
|
|
// remove comments |
392
|
47 |
|
$css = preg_replace(self::$styleCommentRegEx, '', $css); |
393
|
|
|
|
394
|
|
|
// remove spaces |
395
|
47 |
|
$css = preg_replace('/\s\s+/', ' ', $css); |
396
|
|
|
|
397
|
|
|
// remove css charset |
398
|
47 |
|
if (true === $this->excludeCssCharset) { |
399
|
47 |
|
$css = $this->stripeCharsetInCss($css); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
// remove css media queries |
403
|
47 |
|
if (true === $this->excludeMediaQueries) { |
404
|
46 |
|
$css = $this->stripeMediaQueries($css); |
405
|
|
|
} |
406
|
|
|
|
407
|
47 |
|
return (string)$css; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* remove css media queries from the string |
412
|
|
|
* |
413
|
|
|
* @param string $css |
414
|
|
|
* |
415
|
|
|
* @return string |
416
|
|
|
*/ |
417
|
46 |
|
private function stripeMediaQueries($css) |
418
|
|
|
{ |
419
|
|
|
// remove comments previously to matching media queries |
420
|
46 |
|
$css = preg_replace(self::$styleCommentRegEx, '', $css); |
421
|
|
|
|
422
|
46 |
|
return (string)preg_replace(self::$cssMediaQueriesRegEx, '', $css); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* remove charset from the string |
427
|
|
|
* |
428
|
|
|
* @param string $css |
429
|
|
|
* |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
47 |
|
private function stripeCharsetInCss($css) |
433
|
|
|
{ |
434
|
47 |
|
return (string)preg_replace(self::$cssCharsetRegEx, '', $css); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Process the CSS-properties |
439
|
|
|
* |
440
|
|
|
* @return array |
441
|
|
|
* |
442
|
|
|
* @param string $propertyString The CSS-properties. |
443
|
|
|
*/ |
444
|
34 |
|
private function processCSSProperties($propertyString) |
445
|
|
|
{ |
446
|
|
|
// split into chunks |
447
|
34 |
|
$properties = $this->splitIntoProperties($propertyString); |
448
|
|
|
|
449
|
|
|
// init var |
450
|
34 |
|
$pairs = array(); |
451
|
|
|
|
452
|
|
|
// loop properties |
453
|
34 |
|
foreach ($properties as $property) { |
454
|
|
|
// split into chunks |
455
|
34 |
|
$chunks = (array)explode(':', $property, 2); |
456
|
|
|
|
457
|
|
|
// validate |
458
|
34 |
|
if (!isset($chunks[1])) { |
459
|
28 |
|
continue; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
// cleanup |
463
|
33 |
|
$chunks[0] = trim($chunks[0]); |
464
|
33 |
|
$chunks[1] = trim($chunks[1]); |
465
|
|
|
|
466
|
|
|
// add to pairs array |
467
|
|
|
if ( |
468
|
33 |
|
!isset($pairs[$chunks[0]]) |
469
|
|
|
|| |
470
|
33 |
|
!in_array($chunks[1], $pairs[$chunks[0]], true) |
471
|
|
|
) { |
472
|
33 |
|
$pairs[$chunks[0]][] = $chunks[1]; |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
// sort the pairs |
477
|
34 |
|
ksort($pairs); |
478
|
|
|
|
479
|
|
|
// return |
480
|
34 |
|
return $pairs; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Split a style string into an array of properties. |
485
|
|
|
* The returned array can contain empty strings. |
486
|
|
|
* |
487
|
|
|
* @param string $styles ex: 'color:blue;font-size:12px;' |
488
|
|
|
* |
489
|
|
|
* @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
490
|
|
|
*/ |
491
|
34 |
|
private function splitIntoProperties($styles) |
492
|
|
|
{ |
493
|
34 |
|
$properties = (array)explode(';', $styles); |
494
|
34 |
|
$propertiesCount = count($properties); |
495
|
|
|
|
496
|
|
|
/** @noinspection ForeachInvariantsInspection */ |
497
|
34 |
|
for ($i = 0; $i < $propertiesCount; $i++) { |
498
|
|
|
// If next property begins with base64, |
499
|
|
|
// Then the ';' was part of this property (and we should not have split on it). |
500
|
|
|
if ( |
501
|
34 |
|
isset($properties[$i + 1]) |
502
|
|
|
&& |
503
|
34 |
|
strpos($properties[$i + 1], 'base64,') !== false |
504
|
|
|
) { |
505
|
1 |
|
$properties[$i] .= ';' . $properties[$i + 1]; |
506
|
1 |
|
$properties[$i + 1] = ''; |
507
|
1 |
|
++$i; |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
34 |
|
return $properties; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* create XPath |
516
|
|
|
* |
517
|
|
|
* @param \DOMDocument $document |
518
|
|
|
* @param array $cssRules |
519
|
|
|
* |
520
|
|
|
* @return \DOMXPath |
521
|
|
|
*/ |
522
|
47 |
|
private function createXPath(\DOMDocument $document, array $cssRules) |
523
|
|
|
{ |
524
|
47 |
|
$xPath = new \DOMXPath($document); |
525
|
|
|
|
526
|
|
|
// any rules? |
527
|
47 |
|
if (0 !== count($cssRules)) { |
528
|
|
|
// loop rules |
529
|
34 |
|
foreach ($cssRules as $rule) { |
530
|
|
|
|
531
|
34 |
|
$ruleSelector = $rule['selector']; |
532
|
34 |
|
$ruleProperties = $rule['properties']; |
533
|
|
|
|
534
|
34 |
|
if (!$ruleSelector || !$ruleProperties) { |
535
|
3 |
|
continue; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
try { |
539
|
33 |
|
$converter = new CssSelectorConverter(); |
540
|
33 |
|
$query = $converter->toXPath($ruleSelector); |
541
|
3 |
|
} catch (ExceptionInterface $e) { |
542
|
3 |
|
$query = null; |
543
|
|
|
} |
544
|
33 |
|
$converter = null; |
545
|
|
|
|
546
|
|
|
// validate query |
547
|
33 |
|
if (null === $query) { |
548
|
3 |
|
continue; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
// search elements |
552
|
32 |
|
$elements = $xPath->query($query); |
553
|
|
|
|
554
|
|
|
// validate elements |
555
|
32 |
|
if (false === $elements) { |
556
|
|
|
continue; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
// loop found elements |
560
|
32 |
|
foreach ($elements as $element) { |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @var $element \DOMElement |
564
|
|
|
*/ |
565
|
|
|
|
566
|
|
|
if ( |
567
|
32 |
|
$ruleSelector == '*' |
568
|
|
|
&& |
569
|
|
|
( |
570
|
1 |
|
$element->tagName == 'html' |
571
|
1 |
|
|| $element->tagName === 'title' |
572
|
1 |
|
|| $element->tagName == 'meta' |
573
|
1 |
|
|| $element->tagName == 'head' |
574
|
1 |
|
|| $element->tagName == 'style' |
575
|
1 |
|
|| $element->tagName == 'script' |
576
|
32 |
|
|| $element->tagName == 'link' |
577
|
|
|
) |
578
|
|
|
) { |
579
|
1 |
|
continue; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
// no styles stored? |
583
|
32 |
|
if (null === $element->attributes->getNamedItem('data-css-to-inline-styles-original-styles')) { |
584
|
|
|
|
585
|
|
|
// init var |
586
|
32 |
|
$originalStyle = ''; |
587
|
|
|
|
588
|
32 |
|
if (null !== $element->attributes->getNamedItem('style')) { |
589
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
590
|
9 |
|
$originalStyle = $element->attributes->getNamedItem('style')->value; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
// store original styles |
594
|
32 |
|
$element->setAttribute('data-css-to-inline-styles-original-styles', $originalStyle); |
595
|
|
|
|
596
|
|
|
// clear the styles |
597
|
32 |
|
$element->setAttribute('style', ''); |
598
|
|
|
} |
599
|
|
|
|
600
|
32 |
|
$propertiesString = $this->createPropertyChunks($element, $ruleProperties); |
601
|
|
|
|
602
|
|
|
// set attribute |
603
|
32 |
|
if ('' != $propertiesString) { |
604
|
32 |
|
$element->setAttribute('style', $propertiesString); |
605
|
|
|
} |
606
|
|
|
} |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
// reapply original styles |
610
|
|
|
// search elements |
611
|
34 |
|
$elements = $xPath->query('//*[@data-css-to-inline-styles-original-styles]'); |
612
|
|
|
|
613
|
|
|
// loop found elements |
614
|
34 |
|
foreach ($elements as $element) { |
615
|
|
|
// get the original styles |
616
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
617
|
32 |
|
$originalStyle = $element->attributes->getNamedItem('data-css-to-inline-styles-original-styles')->value; |
618
|
|
|
|
619
|
32 |
|
if ('' != $originalStyle) { |
620
|
9 |
|
$originalStyles = $this->splitIntoProperties($originalStyle); |
621
|
|
|
|
622
|
9 |
|
$originalProperties = $this->splitStyleIntoChunks($originalStyles); |
623
|
|
|
|
624
|
9 |
|
$propertiesString = $this->createPropertyChunks($element, $originalProperties); |
625
|
|
|
|
626
|
|
|
// set attribute |
627
|
9 |
|
if ('' != $propertiesString) { |
628
|
9 |
|
$element->setAttribute('style', $propertiesString); |
629
|
|
|
} |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
// remove placeholder |
633
|
32 |
|
$element->removeAttribute('data-css-to-inline-styles-original-styles'); |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
|
637
|
47 |
|
return $xPath; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @param \DOMElement $element |
642
|
|
|
* @param array $ruleProperties |
643
|
|
|
* |
644
|
|
|
* @return string |
645
|
|
|
*/ |
646
|
32 |
|
private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
647
|
|
|
{ |
648
|
|
|
// init var |
649
|
32 |
|
$properties = array(); |
650
|
|
|
|
651
|
|
|
// get current styles |
652
|
32 |
|
$stylesAttribute = $element->attributes->getNamedItem('style'); |
653
|
|
|
|
654
|
|
|
// any styles defined before? |
655
|
32 |
|
if (null !== $stylesAttribute) { |
656
|
|
|
// get value for the styles attribute |
657
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
658
|
32 |
|
$definedStyles = (string)$stylesAttribute->value; |
659
|
|
|
|
660
|
|
|
// split into properties |
661
|
32 |
|
$definedProperties = $this->splitIntoProperties($definedStyles); |
662
|
|
|
|
663
|
32 |
|
$properties = $this->splitStyleIntoChunks($definedProperties); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
// add new properties into the list |
667
|
32 |
|
foreach ($ruleProperties as $key => $value) { |
668
|
|
|
// If one of the rules is already set and is !important, don't apply it, |
669
|
|
|
// except if the new rule is also important. |
670
|
|
|
if ( |
671
|
32 |
|
!isset($properties[$key]) |
672
|
|
|
|| |
673
|
10 |
|
false === stripos($properties[$key], '!important') |
674
|
|
|
|| |
675
|
32 |
|
false !== stripos(implode('', (array)$value), '!important') |
676
|
|
|
) { |
677
|
32 |
|
$properties[$key] = $value; |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
// build string |
682
|
32 |
|
$propertyChunks = array(); |
683
|
|
|
|
684
|
|
|
// build chunks |
685
|
32 |
|
foreach ($properties as $key => $values) { |
686
|
32 |
|
foreach ((array)$values as $value) { |
687
|
32 |
|
$propertyChunks[] = $key . ': ' . $value . ';'; |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
|
691
|
32 |
|
return implode(' ', $propertyChunks); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* @param array $definedProperties |
696
|
|
|
* |
697
|
|
|
* @return array |
698
|
|
|
*/ |
699
|
32 |
|
private function splitStyleIntoChunks(array $definedProperties) |
700
|
|
|
{ |
701
|
|
|
// init var |
702
|
32 |
|
$properties = array(); |
703
|
|
|
|
704
|
|
|
// loop properties |
705
|
32 |
|
foreach ($definedProperties as $property) { |
706
|
|
|
// validate property |
707
|
|
|
if ( |
708
|
32 |
|
!$property |
709
|
|
|
|| |
710
|
32 |
|
strpos($property, ':') === false |
711
|
|
|
) { |
712
|
32 |
|
continue; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
// split into chunks |
716
|
17 |
|
$chunks = (array)explode(':', trim($property), 2); |
717
|
|
|
|
718
|
|
|
// validate |
719
|
17 |
|
if (!isset($chunks[1])) { |
720
|
|
|
continue; |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
// loop chunks |
724
|
17 |
|
$properties[$chunks[0]] = trim($chunks[1]); |
725
|
|
|
} |
726
|
|
|
|
727
|
32 |
|
return $properties; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* Strip style tags into the generated HTML. |
732
|
|
|
* |
733
|
|
|
* @param \DOMXPath $xPath The DOMXPath for the entire document. |
734
|
|
|
* |
735
|
|
|
* @return string |
736
|
|
|
*/ |
737
|
13 |
|
private function stripOriginalStyleTags(\DOMXPath $xPath) |
738
|
|
|
{ |
739
|
|
|
// get all style tags |
740
|
13 |
|
$nodes = $xPath->query('descendant-or-self::style'); |
741
|
13 |
|
foreach ($nodes as $node) { |
742
|
12 |
|
if ($this->excludeMediaQueries === true) { |
743
|
|
|
|
744
|
|
|
// remove comments previously to matching media queries |
745
|
11 |
|
$node->nodeValue = preg_replace(self::$styleCommentRegEx, '', $node->nodeValue); |
746
|
|
|
|
747
|
|
|
// search for Media Queries |
748
|
11 |
|
preg_match_all(self::$cssMediaQueriesRegEx, $node->nodeValue, $mqs); |
749
|
|
|
|
750
|
|
|
// replace the nodeValue with just the Media Queries |
751
|
11 |
|
$node->nodeValue = implode("\n", $mqs[0]); |
752
|
|
|
|
753
|
|
|
} else { |
754
|
|
|
// remove the entire style tag |
755
|
12 |
|
$node->parentNode->removeChild($node); |
756
|
|
|
} |
757
|
|
|
} |
758
|
13 |
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Remove id and class attributes. |
762
|
|
|
* |
763
|
|
|
* @param \DOMXPath $xPath The DOMXPath for the entire document. |
764
|
|
|
* |
765
|
|
|
* @return string |
766
|
|
|
*/ |
767
|
3 |
|
private function cleanupHTML(\DOMXPath $xPath) |
768
|
|
|
{ |
769
|
3 |
|
$nodes = $xPath->query('//@class | //@id'); |
770
|
3 |
|
foreach ($nodes as $node) { |
771
|
3 |
|
$node->ownerElement->removeAttributeNode($node); |
772
|
|
|
} |
773
|
3 |
|
} |
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* Should the IDs and classes be removed? |
777
|
|
|
* |
778
|
|
|
* @param bool $on Should we enable cleanup? |
779
|
|
|
* |
780
|
|
|
* @return $this |
781
|
|
|
*/ |
782
|
3 |
|
public function setCleanup($on = true) |
783
|
|
|
{ |
784
|
3 |
|
$this->cleanup = (bool)$on; |
785
|
|
|
|
786
|
3 |
|
return $this; |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* Set the encoding to use with the DOMDocument |
791
|
|
|
* |
792
|
|
|
* @param string $encoding The encoding to use. |
793
|
|
|
* |
794
|
|
|
* @return $this |
795
|
|
|
* |
796
|
|
|
* @deprecated Doesn't have any effect |
797
|
|
|
*/ |
798
|
|
|
public function setEncoding($encoding) |
799
|
|
|
{ |
800
|
|
|
$this->encoding = (string)$encoding; |
801
|
|
|
|
802
|
|
|
return $this; |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* Set use of inline styles block. |
807
|
|
|
* |
808
|
|
|
* Info: If this is enabled the class will use the style-block in the HTML. |
809
|
|
|
* |
810
|
|
|
* @param bool $on Should we process inline styles? |
811
|
|
|
* |
812
|
|
|
* @return $this |
813
|
|
|
*/ |
814
|
27 |
|
public function setUseInlineStylesBlock($on = true) |
815
|
|
|
{ |
816
|
27 |
|
$this->useInlineStylesBlock = (bool)$on; |
817
|
|
|
|
818
|
27 |
|
return $this; |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* Set use of inline link block. |
823
|
|
|
* |
824
|
|
|
* Info: If this is enabled the class will use the links reference in the HTML. |
825
|
|
|
* |
826
|
|
|
* @param bool [optional] $on Should we process link styles? |
827
|
|
|
* |
828
|
|
|
* @return $this |
829
|
|
|
*/ |
830
|
2 |
|
public function setLoadCSSFromHTML($on = true) |
831
|
|
|
{ |
832
|
2 |
|
$this->loadCSSFromHTML = (bool)$on; |
833
|
|
|
|
834
|
2 |
|
return $this; |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
/** |
838
|
|
|
* Set strip original style tags. |
839
|
|
|
* |
840
|
|
|
* Info: If this is enabled the class will remove all style tags in the HTML. |
841
|
|
|
* |
842
|
|
|
* @param bool $on Should we process inline styles? |
843
|
|
|
* |
844
|
|
|
* @return $this |
845
|
|
|
*/ |
846
|
17 |
|
public function setStripOriginalStyleTags($on = true) |
847
|
|
|
{ |
848
|
17 |
|
$this->stripOriginalStyleTags = (bool)$on; |
849
|
|
|
|
850
|
17 |
|
return $this; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
/** |
854
|
|
|
* Set exclude media queries. |
855
|
|
|
* |
856
|
|
|
* Info: If this is enabled the media queries will be removed before inlining the rules. |
857
|
|
|
* |
858
|
|
|
* WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
859
|
|
|
* |
860
|
|
|
* @param bool $on |
861
|
|
|
* |
862
|
|
|
* @return $this |
863
|
|
|
*/ |
864
|
14 |
|
public function setExcludeMediaQueries($on = true) |
865
|
|
|
{ |
866
|
14 |
|
$this->excludeMediaQueries = (bool)$on; |
867
|
|
|
|
868
|
14 |
|
return $this; |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
/** |
872
|
|
|
* Set exclude charset. |
873
|
|
|
* |
874
|
|
|
* @param bool $on |
875
|
|
|
* |
876
|
|
|
* @return $this |
877
|
|
|
*/ |
878
|
1 |
|
public function setExcludeCssCharset($on = true) |
879
|
|
|
{ |
880
|
1 |
|
$this->excludeCssCharset = (bool)$on; |
881
|
|
|
|
882
|
1 |
|
return $this; |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
/** |
886
|
|
|
* Set exclude conditional inline-style blocks. |
887
|
|
|
* |
888
|
|
|
* e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
889
|
|
|
* |
890
|
|
|
* @param bool $on |
891
|
|
|
* |
892
|
|
|
* @return $this |
893
|
|
|
*/ |
894
|
6 |
|
public function setExcludeConditionalInlineStylesBlock($on = true) |
895
|
|
|
{ |
896
|
6 |
|
$this->excludeConditionalInlineStylesBlock = (bool)$on; |
897
|
|
|
|
898
|
6 |
|
return $this; |
899
|
|
|
} |
900
|
|
|
} |
901
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.