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