jandc / css-from-html-extractor
Php library which determines which css is used from html snippets.
Installs: 24 061
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 5
Forks: 2
Open Issues: 0
Requires
- php: ^5.5 || ^7
- doctrine/cache: ^1.7
- symfony/css-selector: ^2.7|~3.0|~4.0
- twig/twig: ^1.0|^2.0
README
Php library which determines which css is used from html snippets. It is used in jandc/critical-css to automatically and dynamically determine critical css on a per page basis.
Installation
composer require jandc/css-from-html-extractor
Usage
With Twig
Register Extension
use CSSFromHTMLExtractor\Twig\Extension as ExtractorExtension; $extension = new ExtractorExtension() $extension->addBaseRules('path/to/css'); /** @var Twig_Environment $twig */ $twig->addExtension($extension);
Mark the regions of your templates with the provided blocks
{% fold %} <div class="my-class"> ... </div> {% endfold %}
Retrieve the resulting css from the extension
$extension = $twigEnvironment->getExtension(ExtractorExtension::class); $extension->buildCriticalCssFromSnippets();
Handling raw HTML
$cssFromHTMLExtractor = new CssFromHTMLExtractor(); $cssFromHTMLExtractor->addBaseRules('path/to/css'); $cssFromHTMLExtractor->addHtmlToStore($rawHtml); $extractedCss = $cssFromHTMLExtractor->buildExtractedRuleSet();