Skip to content

Commit

Permalink
[BUGFIX] Parse @font-face src property as comma-delimited list
Browse files Browse the repository at this point in the history
Fixes #789.

Also adds an initial `TestCase` for `Rule/Rule`.
  • Loading branch information
Jake Hotson committed Jan 17, 2025
1 parent d783f0e commit defbc9e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Please also have a look at our

### Fixed

- Parse `@font-face` `src` property as comma-delimited list (#790)
- Fix type errors in PHP strict mode (#664)
- Fix undefined local variable in `CalcFunction::parse()` (#593)
- Fix PHP notice caused by parsing invalid color values having less than 6
Expand Down
10 changes: 9 additions & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,21 @@ public static function parse(ParserState $oParserState): Rule
* @param string $sRule
*
* @return array<int, string>
* 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).
*/
private static function listDelimiterForRule($sRule): array
{
if (\preg_match('/^font($|-)/', $sRule)) {
return [',', '/', ' '];
}
return [',', ' ', '/'];

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

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

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Value;

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\ValueList;

/**
* @covers \Sabberworm\CSS\Rule\Rule
*/
final class RuleTest extends TestCase
{
/**
* @return array<string, array{0: string, 1: array<int, string>}>
*/
public static function provideRulesAndExpectedParsedValueListTypes(): array
{
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 array<int, string> $expectedTypeClassnames
*
* @dataProvider provideRulesAndExpectedParsedValueListTypes
*/
public function parsesValuesIntoExpectedTypeList(string $rule, array $expectedTypeClassnames): void
{
$subject = Rule::parse(new ParserState($rule, Settings::create()));

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

$actualClassnames = \array_map(
function ($component): string {
return \get_class($component);
},
$value->getListComponents()
);

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

0 comments on commit defbc9e

Please sign in to comment.