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