File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11<?php
2- require __DIR__ . '/../src/Northys/CssInliner/CssInliner.php ' ;
3- require __DIR__ . '/../vendor/autoload.php ' ;
4-
5- $ inliner = new Northys \CSSInliner ;
6- $ inliner ->addCSS (__DIR__ . '/example.css ' );
7- $ inliner ->render (file_get_contents (__DIR__ . '/example.html ' ));
2+ require __DIR__ . '/../src/Northys/CssInliner/CssInliner.php ' ;
3+ require __DIR__ . '/../vendor/autoload.php ' ;
4+
5+ $ inliner = new Northys \CSSInliner ;
6+ $ inliner ->addCSS (__DIR__ . '/example.css ' );
7+ echo $ inliner ->render (file_get_contents (__DIR__ . '/example.html ' ));
Original file line number Diff line number Diff line change 11<?php
2+
23namespace Northys ;
34
4- use Symfony \Component \CssSelector \CssSelector ,
5- Sabberworm \CSS ;
5+ use Sabberworm \CSS ;
6+ use Symfony \Component \CssSelector \CssSelector ;
7+
8+
69
710/**
8- * Description of Inliner
9- *
1011 * @author Northys
1112 */
12- class CSSInliner {
13- /** @var Sabberworm\CSS\Parser */
13+ class CSSInliner
14+ {
15+
16+ /**
17+ * @var CSS\CSSList\Document
18+ */
1419 private $ css ;
15-
16- public function addCSS ($ filename ) {
17- $ css = file_get_contents ($ filename );
18- if (!$ css ) {
20+
21+
22+
23+ public function addCSS ($ filename )
24+ {
25+ if ( ! $ css = @file_get_contents ($ filename )) {
1926 throw new \Exception ("Failed on loading CSS file. Check the file path you have provided! " , 1 );
2027 }
21- $ this ->css = new CSS \Parser ($ css );
22- $ this ->css = $ this ->css ->parse ();
28+
29+ $ parser = new CSS \Parser ($ css );
30+ $ this ->css = $ parser ->parse ();
2331 }
24-
25- public function render ($ html , $ return = FALSE ) {
32+
33+
34+
35+ public function render ($ html )
36+ {
2637 $ dom = new \DOMDocument ;
2738 $ dom ->loadHTML ($ html );
2839 $ finder = new \DOMXPath ($ dom );
29-
40+
3041 foreach ($ this ->css ->getAllRuleSets () as $ ruleSet ) {
3142 $ selector = $ ruleSet ->getSelector ();
3243 foreach ($ finder ->evaluate (CssSelector::toXPath ($ selector [0 ])) as $ node ) {
@@ -37,10 +48,7 @@ public function render ($html, $return = FALSE) {
3748 }
3849 }
3950 }
40- if ($ return == TRUE ) {
41- return $ dom ->saveHTML ();
42- } else {
43- echo $ dom ->saveHTML ();
44- }
51+
52+ return $ dom ->saveHTML ();
4553 }
4654}
You can’t perform that action at this time.
0 commit comments