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