Skip to content

[BUGFIX] Parse @font-face src property as comma-delimited list #794

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

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

- Parse `@font-face` `src` property as comma-delimited list (#794)

## 8.7.0: Add support for PHP 8.4

### Added
Expand Down
14 changes: 12 additions & 2 deletions src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,26 @@ public static function parse(ParserState $oParserState)
}

/**
* Returns a list of delimiters (or separators).
* The first item is the innermost separator (or, put another way, the highest-precedence operator).
* The sequence continues to the outermost separator (or lowest-precedence operator).
*
* @param string $sRule
*
* @return array<int, string>
* @return list<non-empty-string>
*/
private static function listDelimiterForRule($sRule)
{
if (preg_match('/^font($|-)/', $sRule)) {
return [',', '/', ' '];
}
return [',', ' ', '/'];

switch ($sRule) {
case 'src':
return [' ', ','];
default:
return [',', ' ', '/'];
}
}

/**
Expand Down
62 changes: 62 additions & 0 deletions tests/Unit/Rule/RuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Sabberworm\CSS\Tests\Unit\Rule;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Settings;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\Value\RuleValueList;
use Sabberworm\CSS\Value\Value;
use Sabberworm\CSS\Value\ValueList;

/**
* @covers \Sabberworm\CSS\Rule\Rule
*/
final class RuleTest extends TestCase
{
/**
* @return array<string, array{0: string, 1: list<class-string>}>
*/
public static function provideRulesAndExpectedParsedValueListTypes()
{
return [
'src (e.g. in @font-face)' => [
"
src: url('../fonts/open-sans-italic-300.woff2') format('woff2'),
url('../fonts/open-sans-italic-300.ttf') format('truetype');
",
[RuleValueList::class, RuleValueList::class],
],
];
}

/**
* @test
*
* @param string $rule
* @param list<class-string> $expectedTypeClassnames
*
* @dataProvider provideRulesAndExpectedParsedValueListTypes
*/
public function parsesValuesIntoExpectedTypeList($rule, array $expectedTypeClassnames)
{
$subject = Rule::parse(new ParserState($rule, Settings::create()));

$value = $subject->getValue();
self::assertInstanceOf(ValueList::class, $value);

$actualClassnames = \array_map(
/**
* @param Value|string $component
* @return string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a feeling that we may not be able to use some type hints in backports, so feel vindicated re #790 (comment) :)

*/
static function ($component) {
return \is_string($component) ? 'string' : \get_class($component);
},
$value->getListComponents()
);

self::assertSame($expectedTypeClassnames, $actualClassnames);
}
}