Skip to content

Commit

Permalink
Merge pull request #143 from tbela99/enforce_font_display
Browse files Browse the repository at this point in the history
add font-display in extracted web font declarations
  • Loading branch information
tbela99 authored Aug 22, 2021
2 parents 4a9d206 + 24a087d commit 6b0aed3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
29 changes: 23 additions & 6 deletions helpers/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
namespace Gzip\Helpers;

use Gzip\GZipHelper;
use TBela\CSS\Compiler;
//use TBela\CSS\Compiler;
use TBela\CSS\Element;
use TBela\CSS\Element\Stylesheet;
use TBela\CSS\Interfaces\ElementInterface;
use TBela\CSS\Interfaces\RuleListInterface;
use TBela\CSS\Parser;
use TBela\CSS\Renderer;
Expand Down Expand Up @@ -63,14 +64,19 @@ public function processHTML($html, array $options = [])

$fetchFonts = function ($node) use ($options, $headStyle) {

/**
* @var Element\AtRule $node
*/

if ((string)$node['name'] == 'font-face') {

$query = $node->query('./[@name=src]');

/**
* @var Element\AtRule $node
* @var ElementInterface $query
*/
$node->addDeclaration('font-display', $options['fontdisplay']);

$query = $node->query('./[@name=src]');
$query = end($query);

if ($query) {

Expand Down Expand Up @@ -105,7 +111,18 @@ public function processHTML($html, array $options = [])
});
}

$headStyle->append(end($query)->copy()->getRoot());
$copy = $query->copy();

$declaration = new Element\Declaration();
$declaration->setName('font-display');
$declaration->setValue($options['fontdisplay']);

/**
* @var RuleListInterface
*/
$copy->getParent()->insert($declaration, 0);

$headStyle->append($copy->getRoot());
}
}
};
Expand Down Expand Up @@ -376,7 +393,7 @@ public function processHTML($html, array $options = [])
}

$position = isset($attributes['data-position']) && $attributes['data-position'] == 'head' ? 'head' : 'body';
$links[$position]['style'][] = !empty($css_options['compress']) ? (new Compiler($css_options))->setContent($matches[2])->compile() : $matches[2];
$links[$position]['style'][] = !empty($css_options['compress']) ? (new Renderer($css_options))->renderAst(new Parser($matches[2])) : $matches[2];

return '';
}, $html);
Expand Down
1 change: 1 addition & 0 deletions helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public function postProcessHTML ($html, array $options = []) {
}

return $a['href'] < $b['href'] ? -1 : 1;

});


Expand Down

0 comments on commit 6b0aed3

Please sign in to comment.