Skip to content

Commit 9e1c576

Browse files
committed
Further renamed our classes and updated the docs
1 parent cb2fff8 commit 9e1c576

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
1-
# page-specific-css
2-
Page-specific-css is a library with extracts css selectors from html snippets and matches them with css rules to extract the used rules.
1+
# CSS from HTML extractor
32

4-
This effectively enables you to create critical css.
3+
Php library which determines which css is used from html snippets.
4+
It is used in jandc/critical-css to automatically and dynamically determine critical css on a per page basis.
55

6-
The bulk of it's code is based on, and expanded from, Thijs Verkoyen's inline css library.
76

7+
### Installation
8+
``
9+
composer require jandc/css-from-html-extractor
10+
``
811
### Usage
9-
This library is used as a twig post processing module in JanDC/critical-css-processor. But you could of course make your own implementation.
1012

11-
To get a complete instruction in how to use this library (in conjunction with a twig wrapper) please refer to https://github.com/JanDC/page-specific-css-silex for the reference implementation or the critical css processor for a more generic implementation.
13+
#### With Twig
14+
###### Register Extension
15+
```
16+
use CSSFromHTMLExtractor\Twig\Extension as ExtractorExtension;
17+
18+
$extension = new ExtractorExtension()
19+
$extension->addBaseRules('path/to/css');
20+
21+
/** @var Twig_Environment $twig */
22+
$twig->addExtension($extension);
23+
```
24+
###### Mark the regions of your templates with the provided blocks
25+
```
26+
{% fold %}
27+
<div class="my-class">
28+
...
29+
</div>
30+
{% endfold %}
31+
```
32+
33+
###### Retrieve the resulting css from the extension
34+
35+
```
36+
$extension = $twigEnvironment->getExtension(ExtractorExtension::class);
37+
$extension->buildCriticalCssFromSnippets();
38+
```
39+
40+
41+
#### Handling raw HTML
42+
```
43+
$cssFromHTMLExtractor = new CssFromHTMLExtractor();
44+
$cssFromHTMLExtractor->addBaseRules('path/to/css');
45+
$cssFromHTMLExtractor->addHtmlToStore($rawHtml);
46+
$extractedCss = $cssFromHTMLExtractor->buildExtractedRuleSet();
47+
```

src/PageSpecificCss.php renamed to src/CssFromHTMLExtractor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use CSSFromHTMLExtractor\Css\Rule\Rule;
1111
use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
1212

13-
class PageSpecificCss
13+
class CssFromHTMLExtractor
1414
{
1515

1616
/** @var CssSelectorConverter */
@@ -29,7 +29,7 @@ class PageSpecificCss
2929
private $htmlStore;
3030

3131
/**
32-
* PageSpecificCss constructor.
32+
* CssFromHTMLExtractor constructor.
3333
*/
3434
public function __construct()
3535
{

src/Twig/Extension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
namespace CSSFromHTMLExtractor\Twig;
44

5-
use CSSFromHTMLExtractor\PageSpecificCss;
5+
use CSSFromHTMLExtractor\CssFromHTMLExtractor;
66
use CSSFromHTMLExtractor\Twig\TokenParsers\FoldTokenParser;
77
use Twig_Extension;
88
use Twig_ExtensionInterface;
99

1010
class Extension extends Twig_Extension implements Twig_ExtensionInterface
1111
{
1212

13-
/** @var PageSpecificCss */
13+
/** @var CssFromHTMLExtractor */
1414
private $pageSpecificCssService;
1515

1616
/**
1717
* Extension constructor.
1818
*/
1919
public function __construct()
2020
{
21-
$this->pageSpecificCssService = new PageSpecificCss();
21+
$this->pageSpecificCssService = new CssFromHTMLExtractor();
2222
}
2323

2424
/**
@@ -46,6 +46,7 @@ public function getCriticalCss()
4646
{
4747
return $this->pageSpecificCssService->getCssStore()->compileStyles();
4848
}
49+
4950
public function buildCriticalCssFromSnippets()
5051
{
5152
return $this->pageSpecificCssService->buildExtractedRuleSet();

0 commit comments

Comments
 (0)