114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
|
|
$root = dirname(__DIR__);
|
|
$main = file_get_contents($root . '/assets/css/main.css');
|
|
$page = file_get_contents($root . '/assets/css/page-semejnye-klassy.css');
|
|
|
|
function keeps(string $sel): bool
|
|
{
|
|
$s = trim($sel);
|
|
if ('' === $s) {
|
|
return false;
|
|
}
|
|
// база
|
|
if (in_array($s, array( ':root', 'html', 'body', 'main', '.main-page', '.ds-container', '.section-bg', '.section-bg::before', '.btn-primary', '.btn-primary:hover', '.btn-primary:focus-visible' ), true)) {
|
|
return true;
|
|
}
|
|
|
|
foreach (array( '.ds-hero__visual', '.ds-hero__photo', '.ds-hero__float', '.ds-hero__deco', '.ds-hero__sparkle', '.ds-hero__msg', '.ds-hero__messengers' ) as $skip) {
|
|
if (0 === strpos($s, $skip)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (0 === strpos($s, '.page-family-classes')) {
|
|
return ( false !== strpos($s, '.family-hero') || false !== strpos($s, 'main svg') );
|
|
}
|
|
|
|
foreach (array( '.promo-banner', '.header', '.top-header-row', '.bottom-header-row', '.menu-wrapper', '.add-menu', '.inter-lk', '.icon-links', '.icon-close-menu', '.btn-close-menu', '.btn-free-lesson-header', '.burger', '.mobile', '.desktop', '.header-tel', '.dk-icon', '.ds-hero', '.breadcrumbs', '.fa-home', '.family-hero', '.page-family-classes' ) as $prefix) {
|
|
if (0 === strpos($s, $prefix)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
function parse_css(string $css, $media = null): array
|
|
{
|
|
$out = array();
|
|
$i = 0;
|
|
$n = strlen($css);
|
|
$sel_start = 0;
|
|
while ($i < $n) {
|
|
$c = $css[$i];
|
|
if ('{' === $c) {
|
|
$sel = trim(substr($css, $sel_start, $i - $sel_start));
|
|
$depth = 1;
|
|
$j = $i + 1;
|
|
while ($j < $n && $depth > 0) {
|
|
if ('{' === $css[$j]) {
|
|
$depth++;
|
|
} elseif ('}' === $css[$j]) {
|
|
$depth--;
|
|
}
|
|
$j++;
|
|
}
|
|
$block = substr($css, $sel_start, $j - $sel_start);
|
|
if (0 === strpos($sel, '@media') || 0 === strpos($sel, '@supports')) {
|
|
$inner = substr($css, $i + 1, $j - $i - 2);
|
|
$out = array_merge($out, parse_css($inner, $sel));
|
|
} elseif (0 !== strpos($sel, '@')) {
|
|
$out[] = array( $media, $sel, $block );
|
|
}
|
|
$i = $j;
|
|
$sel_start = $i;
|
|
continue;
|
|
}
|
|
if ('}' === $c) {
|
|
$sel_start = $i + 1;
|
|
}
|
|
$i++;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
function build(array $rules): string
|
|
{
|
|
$out = array();
|
|
$open = null;
|
|
foreach ($rules as $r) {
|
|
list($media, $sel, $block) = $r;
|
|
|
|
$keep = false;
|
|
foreach (explode(',', $sel) as $one) {
|
|
if (keeps($one)) {
|
|
$keep = true;
|
|
break;
|
|
}
|
|
}
|
|
if (! $keep) {
|
|
continue;
|
|
}
|
|
if ($media !== $open) {
|
|
if ($open !== null) {
|
|
$out[] = '}';
|
|
}
|
|
if ($media !== null) {
|
|
$out[] = $media . ' {';
|
|
}
|
|
$open = $media;
|
|
}
|
|
$out[] = $block;
|
|
}
|
|
if ($open !== null) {
|
|
$out[] = '}';
|
|
}
|
|
return implode("\n", $out);
|
|
}
|
|
|
|
$css = build(array_merge(parse_css($main), parse_css($page)));
|
|
file_put_contents($root . '/assets/css/critical-family.css', $css);
|
|
echo 'critical-family.css: ' . strlen($css) . " bytes; gzip=" . strlen(gzencode($css, 9)) . "\n";
|