Skip to content

Commit c90dfe6

Browse files
committed
experimental: build css based on link tags and critical css
1 parent 0d2819b commit c90dfe6

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

src/HtmlStore.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PageSpecificCss;
4+
5+
use TijsVerkoyen\CssToInlineStyles\Css\Property\Property;
6+
7+
class HtmlStore
8+
{
9+
/** @var array Property objects, grouped by selector */
10+
private $snippets = [];
11+
12+
public function addHtmlSnippet($htmlSnippet)
13+
{
14+
$this->snippets = array_merge($this->snippets, $htmlSnippet);
15+
return $this;
16+
}
17+
18+
public function getSnippets()
19+
{
20+
return $this->snippets;
21+
}
22+
}

src/PageSpecificCss.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,32 @@ class PageSpecificCss extends CssToInlineStyles
2424
/** @var Rule[] */
2525
private $rules;
2626

27+
/** @var HtmlStore */
28+
private $htmlStore;
29+
2730
/**
2831
* PageSpecificCss constructor.
29-
* @param string $sourceCss path to the source css
3032
*/
31-
public function __construct($sourceCss)
33+
public function __construct()
3234
{
3335
parent::__construct();
3436

3537
$this->cssStore = new CssStore();
38+
$this->htmlStore = new HtmlStore();
3639
$this->processor = new Processor();
37-
$this->rules = $this->processor->getRules(file_get_contents($sourceCss));
3840
$this->cssConverter = new CssSelectorConverter();
39-
4041
}
4142

42-
public function getStore(){
43+
public function getCssStore()
44+
{
4345
return $this->cssStore;
4446
}
4547

48+
public function getHtmlStore()
49+
{
50+
return $this->htmlStore;
51+
}
52+
4653
/**
4754
* @param string $html the raw html
4855
*/
@@ -51,6 +58,23 @@ public function processHtmlToStore($html)
5158
$this->cssStore->addCssStyles($this->extractCss($html));
5259
}
5360

61+
/**
62+
* @param $sourceCss
63+
*/
64+
public function addBaseRules($sourceCss)
65+
{
66+
$this->rules = array_merge($this->rules, $this->processor->getRules($sourceCss));
67+
}
68+
69+
public function buildExtractedRuleSet()
70+
{
71+
foreach ($this->htmlStore->getSnippets() as $htmlSnippet) {
72+
$this->processHtmlToStore($htmlSnippet);
73+
}
74+
75+
return $this->cssStore->compileStyles();
76+
}
77+
5478
/**
5579
* @param $html
5680
*
@@ -82,8 +106,6 @@ public function extractCss($html)
82106

83107
$applicable_rules = $this->groupRulesBySelector($applicable_rules);
84108
return $applicable_rules;
85-
86-
87109
}
88110

89111
/**
@@ -106,4 +128,11 @@ private function groupRulesBySelector($applicable_rules)
106128

107129
return $grouped;
108130
}
131+
132+
public function addHtmlToStore($rawHtml)
133+
{
134+
$this->htmlStore->addHtmlSnippet($rawHtml);
135+
}
136+
137+
109138
}

src/Twig/Extension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public function __construct($sourceCss)
2323
$this->pageSpecificCssService = new PageSpecificCss($sourceCss);
2424
}
2525

26+
/**
27+
* @param $sourceCss
28+
*/
29+
public function addBaseRules($sourceCss)
30+
{
31+
$this->pageSpecificCssService->addBaseRules($sourceCss);
32+
}
33+
2634
public function getTokenParsers()
2735
{
2836
return [
@@ -32,12 +40,16 @@ public function getTokenParsers()
3240

3341
public function addCssToExtract($rawHtml)
3442
{
35-
$this->pageSpecificCssService->processHtmlToStore($rawHtml);
43+
$this->pageSpecificCssService->addHtmlToStore($rawHtml);
3644
return $rawHtml;
3745
}
3846

3947
public function getCriticalCss()
4048
{
41-
return $this->pageSpecificCssService->getStore()->compileStyles();
49+
return $this->pageSpecificCssService->getCssStore()->compileStyles();
50+
}
51+
public function buildCriticalCssFromSnippets()
52+
{
53+
return $this->pageSpecificCssService->buildExtractedRuleSet();
4254
}
4355
}

0 commit comments

Comments
 (0)