Completed
Push — master ( 2794e7...6ff091 )
by Lars
04:16
created

CssToInlineStyles::createXPath()   D

Complexity

Conditions 23
Paths 2

Size

Total Lines 103
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 23.0028

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 103
ccs 56
cts 57
cp 0.9825
rs 4.6303
cc 23
eloc 50
nc 2
nop 2
crap 23.0028

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = '/<!--(?:\[if).*<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: html-comments without conditional comments
52
   *
53
   * @var string
54
   */
55
  private static $htmlCommentWithoutConditionalCommentRegEx = '|<!--(?!\[if).*?-->|isU';
56
57
  /**
58
   * regular expression: style-tag with 'cleanup'-css-class
59
   *
60
   * @var string
61
   */
62
  private static $styleTagWithCleanupClassRegEx = '|<style[^>]+class="cleanup"[^>]*>.*</style>|isU';
63
64
  /**
65
   * regular expression: css-comments
66
   *
67
   * @var string
68
   */
69
  private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU';
70
71
  /**
72
   * The CSS to use
73
   *
74
   * @var  string
75
   */
76
  private $css;
77
78
  /**
79
   * Should the generated HTML be cleaned
80
   *
81
   * @var  bool
82
   */
83
  private $cleanup = false;
84
85
  /**
86
   * The encoding to use.
87
   *
88
   * @var  string
89
   */
90
  private $encoding = 'UTF-8';
91
92
  /**
93
   * The HTML to process
94
   *
95
   * @var  string
96
   */
97
  private $html;
98
99
  /**
100
   * Use inline-styles block as CSS
101
   *
102
   * @var bool
103
   */
104
  private $useInlineStylesBlock = false;
105
106
  /**
107
   * Use link block reference as CSS
108
   *
109
   * @var bool
110
   */
111
  private $loadCSSFromHTML = false;
112
113
  /**
114
   * Strip original style tags
115
   *
116
   * @var bool
117
   */
118
  private $stripOriginalStyleTags = false;
119
120
  /**
121
   * Exclude conditional inline-style blocks
122
   *
123
   * @var bool
124
   */
125
  private $excludeConditionalInlineStylesBlock = 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 $excludeMediaQueries = true;
133
134
  /**
135
   * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks
136
   *
137
   * @var bool
138
   */
139
  private $excludeCssCharset = true;
140
141
  /**
142
   * Creates an instance, you could set the HTML and CSS here, or load it
143
   * later.
144
   *
145
   * @param  null|string $html The HTML to process.
146
   * @param  null|string $css  The CSS to use.
147
   */
148 55
  public function __construct($html = null, $css = null)
149
  {
150 55
    if (null !== $html) {
151 2
      $this->setHTML($html);
152 2
    }
153
154 55
    if (null !== $css) {
155 2
      $this->setCSS($css);
156 2
    }
157
158 55
    if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) {
159 55
      $this->cssConverter = new CssSelectorConverter();
160 55
    }
161 55
  }
162
163
  /**
164
   * Set HTML to process
165
   *
166
   * @param  string $html The HTML to process.
167
   *
168
   * @return $this
169
   */
170 53
  public function setHTML($html)
171
  {
172
    // strip style definitions, if we use css-class "cleanup" on a style-element
173 53
    $this->html = (string)preg_replace(self::$styleTagWithCleanupClassRegEx, ' ', $html);
174
175 53
    return $this;
176
  }
177
178
  /**
179
   * Set CSS to use
180
   *
181
   * @param  string $css The CSS to use.
182
   *
183
   * @return $this
184
   */
185 48
  public function setCSS($css)
186
  {
187 48
    $this->css = (string)$css;
188
189 48
    return $this;
190
  }
191
192
  /**
193
   * Sort an array on the specificity element in an ascending way
194
   * Lower specificity will be sorted to the beginning of the array
195
   *
196
   * @param Specificity[] $e1 The first element.
197
   * @param Specificity[] $e2 The second element.
198
   *
199
   * @return int
200
   */
201 22
  private static function sortOnSpecificity($e1, $e2)
202
  {
203
    // Compare the specificity
204 22
    $value = $e1['specificity']->compareTo($e2['specificity']);
205
206
    // if the specificity is the same, use the order in which the element appeared
207 22
    if (0 === $value) {
208 17
      $value = $e1['order'] - $e2['order'];
209 17
    }
210
211 22
    return $value;
212
  }
213
214
  /**
215
   * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS.
216
   *
217
   * @param bool $outputXHTML            [optional] Should we output valid XHTML?
218
   * @param int|null $libXMLExtraOptions [optional] $libXMLExtraOptions Since PHP 5.4.0 and Libxml 2.6.0,
219
   *                                     you may also use the options parameter to specify additional
220
   *                                     Libxml parameters.
221
   * @param bool $path                   [optional] Set the path to your external css-files.
222
   *
223
   * @return string
224
   *
225
   * @throws Exception
226
   */
227 53
  public function convert($outputXHTML = false, $libXMLExtraOptions = null, $path = false)
228
  {
229
    // init
230 53
    $outputXHTML = (bool)$outputXHTML;
231
232
    // validate
233 53
    if (!$this->html) {
234 1
      throw new Exception('No HTML provided.');
235
    }
236
237
    // use local variables
238 52
    $css = $this->css;
239
240
    // create new HtmlDomParser
241 52
    $dom = HtmlDomParser::str_get_html($this->html, $libXMLExtraOptions);
0 ignored issues
show
Unused Code introduced by
The call to HtmlDomParser::str_get_html() has too many arguments starting with $this->html.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
242
243
    // check if there is some link css reference
244 52
    if ($this->loadCSSFromHTML) {
245 1
      foreach ($dom->find('link') as $node) {
246
247 1
        $file = ($path ?: __DIR__) . '/' . $node->getAttribute('href');
248
249 1
        if (file_exists($file)) {
250 1
          $css .= file_get_contents($file);
251
252
          // converting to inline css because we don't need/want to load css files, so remove the link
253 1
          $node->outertext = '';
254 1
        }
255 1
      }
256 1
    }
257
258
    // should we use inline style-block
259 52
    if ($this->useInlineStylesBlock) {
260
261 30
      if (true === $this->excludeConditionalInlineStylesBlock) {
262 26
        $this->html = preg_replace(self::$excludeConditionalInlineStylesBlockRegEx, '', $this->html);
263 26
      }
264
265 30
      $css .= $this->getCssFromInlineHtmlStyleBlock($this->html);
266 30
    }
267
268
    // process css
269 52
    $cssRules = $this->processCSS($css);
270
271
    // create new XPath
272 52
    $xPath = $this->createXPath($dom->getDocument(), $cssRules);
273
274
    // strip original style tags if we need to
275 52
    if ($this->stripOriginalStyleTags === true) {
276 13
      $this->stripOriginalStyleTags($xPath);
277 13
    }
278
279
    // cleanup the HTML if we need to
280 52
    if (true === $this->cleanup) {
281 3
      $this->cleanupHTML($xPath);
282 3
    }
283
284
    // should we output XHTML?
285 52
    if (true === $outputXHTML) {
286 6
      return $dom->xml();
287
    }
288
289
    // just regular HTML 4.01 as it should be used in newsletters
290 47
    return $dom->html();
291
  }
292
293
  /**
294
   * get css from inline-html style-block
295
   *
296
   * @param string $html
297
   *
298
   * @return string
299
   */
300 32
  public function getCssFromInlineHtmlStyleBlock($html)
301
  {
302
    // init var
303 32
    $css = '';
304 32
    $matches = array();
305
306 32
    $htmlNoComments = preg_replace(self::$htmlCommentWithoutConditionalCommentRegEx, '', $html);
307
308
    // match the style blocks
309 32
    preg_match_all(self::$styleTagRegEx, $htmlNoComments, $matches);
310
311
    // any style-blocks found?
312 32
    if (!empty($matches[1])) {
313
      // add
314 31
      foreach ($matches[1] as $match) {
315 31
        $css .= trim($match) . "\n";
316 31
      }
317 31
    }
318
319 32
    return $css;
320
  }
321
322
  /**
323
   * Process the loaded CSS
324
   *
325
   * @param string $css
326
   *
327
   * @return array
328
   */
329 52
  private function processCSS($css)
330
  {
331
    //reset current set of rules
332 52
    $cssRules = array();
333
334
    // init vars
335 52
    $css = (string)$css;
336
337 52
    $css = $this->doCleanup($css);
338
339
    // rules are splitted by }
340 52
    $rules = (array)explode('}', $css);
341
342
    // init var
343 52
    $i = 1;
344
345
    // loop rules
346 52
    foreach ($rules as $rule) {
347
      // split into chunks
348 52
      $chunks = explode('{', $rule);
349
350
      // invalid rule?
351 52
      if (!isset($chunks[1])) {
352 52
        continue;
353
      }
354
355
      // set the selectors
356 39
      $selectors = trim($chunks[0]);
357
358
      // get css-properties
359 39
      $cssProperties = trim($chunks[1]);
360
361
      // split multiple selectors
362 39
      $selectors = (array)explode(',', $selectors);
363
364
      // loop selectors
365 39
      foreach ($selectors as $selector) {
366
        // cleanup
367 39
        $selector = trim($selector);
368
369
        // build an array for each selector
370 39
        $ruleSet = array();
371
372
        // store selector
373 39
        $ruleSet['selector'] = $selector;
374
375
        // process the properties
376 39
        $ruleSet['properties'] = $this->processCSSProperties($cssProperties);
377
378
        // calculate specificity
379 39
        $ruleSet['specificity'] = Specificity::fromSelector($selector);
380
381
        // remember the order in which the rules appear
382 39
        $ruleSet['order'] = $i;
383
384
        // add into rules
385 39
        $cssRules[] = $ruleSet;
386
387
        // increment
388 39
        $i++;
389 39
      }
390 52
    }
391
392
    // sort based on specificity
393 52
    if (0 !== count($cssRules)) {
394 39
      usort($cssRules, array(__CLASS__, 'sortOnSpecificity'));
395 39
    }
396
397 52
    return $cssRules;
398
  }
399
400
  /**
401
   * @param string $css
402
   *
403
   * @return string
404
   */
405 52
  private function doCleanup($css)
406
  {
407
    // remove newlines & replace double quotes by single quotes
408 52
    $css = str_replace(
409 52
        array("\r", "\n", '"'),
410 52
        array('', '', '\''),
411
        $css
412 52
    );
413
414
    // remove comments
415 52
    $css = preg_replace(self::$styleCommentRegEx, '', $css);
416
417
    // remove spaces
418 52
    $css = preg_replace('/\s\s+/', ' ', $css);
419
420
    // remove css charset
421 52
    if (true === $this->excludeCssCharset) {
422 52
      $css = $this->stripeCharsetInCss($css);
423 52
    }
424
425
    // remove css media queries
426 52
    if (true === $this->excludeMediaQueries) {
427 51
      $css = $this->stripeMediaQueries($css);
428 51
    }
429
430 52
    return (string)$css;
431
  }
432
433
  /**
434
   * remove css media queries from the string
435
   *
436
   * @param string $css
437
   *
438
   * @return string
439
   */
440 51
  private function stripeMediaQueries($css)
441
  {
442
    // remove comments previously to matching media queries
443 51
    $css = preg_replace(self::$styleCommentRegEx, '', $css);
444
445 51
    return (string)preg_replace(self::$cssMediaQueriesRegEx, '', $css);
446
  }
447
448
  /**
449
   * remove charset from the string
450
   *
451
   * @param string $css
452
   *
453
   * @return string
454
   */
455 52
  private function stripeCharsetInCss($css)
456
  {
457 52
    return (string)preg_replace(self::$cssCharsetRegEx, '', $css);
458
  }
459
460
  /**
461
   * Process the CSS-properties
462
   *
463
   * @return array
464
   *
465
   * @param  string $propertyString The CSS-properties.
466
   */
467 39
  private function processCSSProperties($propertyString)
468
  {
469
    // split into chunks
470 39
    $properties = $this->splitIntoProperties($propertyString);
471
472
    // init var
473 39
    $pairs = array();
474
475
    // loop properties
476 39
    foreach ($properties as $property) {
477
      // split into chunks
478 39
      $chunks = (array)explode(':', $property, 2);
479
480
      // validate
481 39
      if (!isset($chunks[1])) {
482 33
        continue;
483
      }
484
485
      // cleanup
486 38
      $chunks[0] = trim($chunks[0]);
487 38
      $chunks[1] = trim($chunks[1]);
488
489
      // add to pairs array
490
      if (
491 38
          !isset($pairs[$chunks[0]])
492 38
          ||
493 5
          !in_array($chunks[1], $pairs[$chunks[0]], true)
494 38
      ) {
495 38
        $pairs[$chunks[0]][] = $chunks[1];
496 38
      }
497 39
    }
498
499
    // sort the pairs
500 39
    ksort($pairs);
501
502
    // return
503 39
    return $pairs;
504
  }
505
506
  /**
507
   * Split a style string into an array of properties.
508
   * The returned array can contain empty strings.
509
   *
510
   * @param string $styles ex: 'color:blue;font-size:12px;'
511
   *
512
   * @return array an array of strings containing css property ex: array('color:blue','font-size:12px')
513
   */
514 39
  private function splitIntoProperties($styles)
515
  {
516 39
    $properties = (array)explode(';', $styles);
517 39
    $propertiesCount = count($properties);
518
519
    /** @noinspection ForeachInvariantsInspection */
520 39
    for ($i = 0; $i < $propertiesCount; $i++) {
521
      // If next property begins with base64,
522
      // Then the ';' was part of this property (and we should not have split on it).
523
      if (
524 39
          isset($properties[$i + 1])
525 39
          &&
526 32
          strpos($properties[$i + 1], 'base64,') !== false
527 39
      ) {
528 1
        $properties[$i] .= ';' . $properties[$i + 1];
529 1
        $properties[$i + 1] = '';
530 1
        ++$i;
531 1
      }
532 39
    }
533
534 39
    return $properties;
535
  }
536
537
  /**
538
   * create XPath
539
   *
540
   * @param \DOMDocument $document
541
   * @param array        $cssRules
542
   *
543
   * @return \DOMXPath
544
   */
545 52
  private function createXPath(\DOMDocument $document, array $cssRules)
546
  {
547 52
    $propertyStorage = new \SplObjectStorage();
548 52
    $xPath = new \DOMXPath($document);
549
550
    // any rules?
551 52
    if (0 !== count($cssRules)) {
552
      // loop rules
553 39
      foreach ($cssRules as $rule) {
554
555 39
        $ruleSelector = $rule['selector'];
556 39
        $ruleProperties = $rule['properties'];
557
558 39
        if (!$ruleSelector || !$ruleProperties) {
559 3
          continue;
560
        }
561
562
        try {
563 38
          $query = $this->cssConverter->toXPath($ruleSelector);
564 38
        } catch (ExceptionInterface $e) {
565 5
          $query = null;
566
        }
567
568
        // validate query
569 38
        if (null === $query) {
570 5
          continue;
571
        }
572
573
        // search elements
574 37
        $elements = $xPath->query($query);
575
576
        // validate elements
577 37
        if (false === $elements) {
578
          continue;
579
        }
580
581
        // loop found elements
582 37
        foreach ($elements as $element) {
583
584
          /**
585
           * @var $element \DOMElement
586
           */
587
588
          if (
589
              $ruleSelector == '*'
590 37
              &&
591
              (
592 1
                  $element->tagName == 'html'
593 1
                  || $element->tagName === 'title'
594 1
                  || $element->tagName == 'meta'
595 1
                  || $element->tagName == 'head'
596 1
                  || $element->tagName == 'style'
597 1
                  || $element->tagName == 'script'
598 1
                  || $element->tagName == 'link'
599 1
              )
600 37
          ) {
601 1
            continue;
602
          }
603
604
          // no styles stored?
605 37
          if (!isset($propertyStorage[$element])) {
606
607
            // init var
608 37
            $originalStyle = $element->attributes->getNamedItem('style');
609
610 37
            if ($originalStyle) {
611 12
              $originalStyle = $originalStyle->value;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in DOMNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
612 12
            } else {
613 35
              $originalStyle = '';
614
            }
615
616
            // store original styles
617 37
            $propertyStorage->attach($element, $originalStyle);
618
619
            // clear the styles
620 37
            $element->setAttribute('style', '');
621 37
          }
622
623
          // set attribute
624 37
          $propertiesString = $this->createPropertyChunks($element, $ruleProperties);
625 37
          if ($propertiesString) {
626 37
            $element->setAttribute('style', $propertiesString);
627 37
          }
628 37
        }
629 39
      }
630
631 39
      foreach ($propertyStorage as $element) {
632 37
        $originalStyle = $propertyStorage->getInfo();
633 37
        if ($originalStyle) {
634 12
          $originalStyles = $this->splitIntoProperties($originalStyle);
635 12
          $originalProperties = $this->splitStyleIntoChunks($originalStyles);
636
637
          // set attribute
638 12
          $propertiesString = $this->createPropertyChunks($element, $originalProperties);
639 12
          if ($propertiesString) {
640 12
            $element->setAttribute('style', $propertiesString);
641 12
          }
642 12
        }
643 39
      }
644 39
    }
645
646 52
    return $xPath;
647
  }
648
649
  /**
650
   * @param \DOMElement $element
651
   * @param array       $ruleProperties
652
   *
653
   * @return string
654
   */
655 37
  private function createPropertyChunks(\DOMElement $element, array $ruleProperties)
656
  {
657
    // init var
658 37
    $properties = array();
659
660
    // get current styles
661 37
    $stylesAttribute = $element->attributes->getNamedItem('style');
662
663
    // any styles defined before?
664 37
    if (null !== $stylesAttribute) {
665
      // get value for the styles attribute
666 37
      $definedStyles = (string)$stylesAttribute->value;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in DOMNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
667
668
      // split into properties
669 37
      $definedProperties = $this->splitIntoProperties($definedStyles);
670
671 37
      $properties = $this->splitStyleIntoChunks($definedProperties);
672 37
    }
673
674
    // add new properties into the list
675 37
    foreach ($ruleProperties as $key => $value) {
676
      // If one of the rules is already set and is !important, don't apply it,
677
      // except if the new rule is also important.
678
      if (
679 37
          !isset($properties[$key])
680 37
          ||
681 12
          false === stripos($properties[$key], '!important')
682 12
          ||
683 7
          false !== stripos(implode('', (array)$value), '!important')
684 37
      ) {
685 37
        unset($properties[$key]);
686 37
        $properties[$key] = $value;
687 37
      }
688 37
    }
689
690
    // build string
691 37
    $propertyChunks = array();
692
693
    // build chunks
694 37
    foreach ($properties as $key => $values) {
695 37
      foreach ((array)$values as $value) {
696 37
        $propertyChunks[] = $key . ': ' . $value . ';';
697 37
      }
698 37
    }
699
700 37
    return implode(' ', $propertyChunks);
701
  }
702
703
  /**
704
   * @param array $definedProperties
705
   *
706
   * @return array
707
   */
708 37
  private function splitStyleIntoChunks(array $definedProperties)
709
  {
710
    // init var
711 37
    $properties = array();
712
713
    // loop properties
714 37
    foreach ($definedProperties as $property) {
715
      // validate property
716
      if (
717
          !$property
718 37
          ||
719 19
          strpos($property, ':') === false
720 37
      ) {
721 37
        continue;
722
      }
723
724
      // split into chunks
725 19
      $chunks = (array)explode(':', trim($property), 2);
726
727
      // validate
728 19
      if (!isset($chunks[1])) {
729
        continue;
730
      }
731
732
      // loop chunks
733 19
      $properties[$chunks[0]] = trim($chunks[1]);
734 37
    }
735
736 37
    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 30
  public function setUseInlineStylesBlock($on = true)
820
  {
821 30
    $this->useInlineStylesBlock = (bool)$on;
822
823 30
    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