Overview

Namespaces

  • Northys
    • CSSInliner
      • Exceptions
  • PHP

Classes

  • CSSInliner
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: 
 3: namespace Northys\CSSInliner;
 4: 
 5: use Northys\CSSInliner\Exceptions;
 6: use Sabberworm\CSS;
 7: use Symfony\Component\CssSelector\CssSelector;
 8: 
 9: 
10: /**
11:  * Class CSSInliner
12:  * @author Northys
13:  * @package Northys\CSSInliner
14:  */
15: class CSSInliner
16: {
17: 
18:     /**
19:      * @var CSS\CSSList\Document
20:      */
21:     private $css;
22: 
23:     /**
24:      * @var \DOMDocument
25:      */
26:     private $dom;
27: 
28:     /**
29:      * @var \DOMXPath
30:      */
31:     private $finder;
32: 
33: 
34:     /**
35:      * Provides you an option to add as many CSS files as you want
36:      * @param string $filename represents css file path
37:      * @throws \Exception
38:      */
39:     public function addCSS($filename)
40:     {
41:         if ( ! $css = @file_get_contents($filename)) {
42:             throw new Exceptions\InvalidCssFilePathException('Invalid css file path provided.');
43:         }
44:         // merge all CSS content into $this-css variable
45:         $this->css .= $css;
46:     }
47: 
48: 
49:     /**
50:      * Gets styles from <style> html tag - if there are any it will be merged with another styles added by addCss()
51:      * @return CSS\CSSList\Document
52:      * @throws \Exception
53:      */
54:     private function getCSS() {
55:         // get styles inside <style> tags in provided HTML
56:         foreach ($this->dom->getElementsByTagName('style') as $style) {
57:             $this->css .= $style->textContent;
58:         }
59:         $parser = new CSS\Parser($this->css);
60: 
61:         $css = $parser->parse();
62:         if (!$css) {
63:             throw new Exceptions\NoCssRulesException('There are no CSS rules provided.');
64:         }
65:         return $css;
66:     }
67: 
68: 
69:     /**
70:      * Prepares everything and inserts inline styles into html
71:      * @param string $html represents html document
72:      * @return string
73:      */
74:     public function render($html)
75:     {
76:         $this->dom = new \DOMDocument;
77:         $this->dom->loadHTML($html);
78:         $this->finder = new \DOMXPath($this->dom);
79:         $this->css = $this->getCSS();
80:         foreach ($this->css->getAllRuleSets() as $ruleSet) {
81:             $selector = $ruleSet->getSelector();
82:             foreach ($this->finder->evaluate(CssSelector::toXPath($selector[0])) as $node) {
83:                 if ($node->getAttribute('style')) {
84:                     $node->setAttribute('style', $node->getAttribute('style') . implode(' ', $ruleSet->getRules()));
85:                 } else {
86:                     $node->setAttribute('style', implode(' ', $ruleSet->getRules()));
87:                 }
88:             }
89:         }
90: 
91:         return $this->dom->saveHTML();
92:     }
93: 
94: }
95: 
API documentation generated by ApiGen 2.8.0