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