Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract font-face rules and inject into head <styles> element #870

Merged
Prev Previous commit
Next Next commit
test: added tests for @font-face extraction
  • Loading branch information
Aaron Gerig committed May 25, 2020
commit 51196ccc93675a38a9f891c0457c317ca8b17adb
113 changes: 113 additions & 0 deletions tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,119 @@ public function inlineCssNotCopiesInlinableRuleAfterImportRuleToStyleElement()
self::assertNotContainsCss($cssAfter, $subject->render());
}

/**
* @return string[][]
*/
public function provideValidFontFaceRules(): array
{
return [
'single @font-face' => [
'before' => '',
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => '',
],
'uppercase @FONT-FACE' => [
'before' => '',
'@font-face' => '@FONT-FACE { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => '',
],
'mixed case @FoNt-FaCe' => [
'before' => '',
'@font-face' => '@FoNt-FaCe { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => '',
],
'2 @font-faces' => [
'before' => '',
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }' . "\n" . '@font-face { font-family: "Bar Sans"; src: url("/bar-sans.woff2") format("woff2"); }',
'after' => '',
],
'2 @font-faces, minified' => [
'before' => '',
'@font-face' => '@font-face{font-family:"Foo Sans";src:url(/foo-sans.woff2) format("woff2")}@font-face{font-family:"Bar Sans";src:url(/bar-sans.woff2) format("woff2")}',
'after' => '',
],
'@font-face followed by matching inlinable rule' => [
'before' => '',
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => "\n" . 'p { color: green; }',
],
'@font-face followed by matching uninlinable rule' => [
'before' => '',
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => "\n" . 'p:hover { color: green; }',
],
'@font-face followed by matching @media rule' => [
'before' => '',
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => "\n" . '@media (max-width: 640px) { p { color: green; } }',
],
'@font-face preceded by matching inlinable rule' => [
'before' => 'p { color: green; }' . "\n",
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => '',
],
'@font-face preceded by matching uninlinable rule' => [
'before' => 'p:hover { color: green; }' . "\n",
aarongerig marked this conversation as resolved.
Show resolved Hide resolved
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => '',
],
'@font-face preceded by matching @media rule' => [
'before' => '@media (max-width: 640px) { p { color: green; } }' . "\n",
'@font-face' => '@font-face { font-family: "Foo Sans"; src: url("/foo-sans.woff2") format("woff2"); }',
'after' => '',
],
];
}

/**
* @test
*
* @param string $cssBefore
* @param string $cssFontFaces
* @param string $cssAfter
*
* @dataProvider provideValidFontFaceRules
*/
public function inlineCssPreservesValidFontFaceRules(string $cssBefore, string $cssFontFaces, string $cssAfter)
{
$subject = $this->buildDebugSubject('<html><p>foo</p></html>');

$subject->inlineCss($cssBefore . $cssFontFaces . $cssAfter);

self::assertContains($cssFontFaces, $subject->render());
}

/**
* @return string[][]
*/
public function provideInvalidFontFaceRules(): array
{
return [
'@font-face without font-family descriptor' => [
'@font-face { src: url("/foo-sans.woff2") format("woff2"); }',
],
'@font-face without src descriptor' => [
'@font-face { font-family: "Foo Sans"; }',
],
];
}

/**
* @test
*
* @param string $css
*
* @dataProvider provideInvalidFontFaceRules
*/
public function inlineCssRemovesInvalidFontFaceRules(string $css)
{
$subject = $this->buildDebugSubject('<html></html>');

$subject->inlineCss($css);

self::assertNotContains('@font-face', $subject->render());
}

/**
* @test
*/
Expand Down