Skip to content

Commit defbc9e

Browse files
committed
[BUGFIX] Parse @font-face src property as comma-delimited list
Fixes #789. Also adds an initial `TestCase` for `Rule/Rule`.
1 parent d783f0e commit defbc9e

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Please also have a look at our
4242

4343
### Fixed
4444

45+
- Parse `@font-face` `src` property as comma-delimited list (#790)
4546
- Fix type errors in PHP strict mode (#664)
4647
- Fix undefined local variable in `CalcFunction::parse()` (#593)
4748
- Fix PHP notice caused by parsing invalid color values having less than 6

src/Rule/Rule.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,21 @@ public static function parse(ParserState $oParserState): Rule
117117
* @param string $sRule
118118
*
119119
* @return array<int, string>
120+
* The first item is the innermost separator (or, put another way, the highest-precedence operator).
121+
* The sequence continues to the outermost separator (or lowest-precedence operator).
120122
*/
121123
private static function listDelimiterForRule($sRule): array
122124
{
123125
if (\preg_match('/^font($|-)/', $sRule)) {
124126
return [',', '/', ' '];
125127
}
126-
return [',', ' ', '/'];
128+
129+
switch ($sRule) {
130+
case 'src':
131+
return [' ', ','];
132+
default:
133+
return [',', ' ', '/'];
134+
}
127135
}
128136

129137
/**

tests/Rule/RuleTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Value;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\Parsing\ParserState;
9+
use Sabberworm\CSS\Settings;
10+
use Sabberworm\CSS\Rule\Rule;
11+
use Sabberworm\CSS\Value\RuleValueList;
12+
use Sabberworm\CSS\Value\ValueList;
13+
14+
/**
15+
* @covers \Sabberworm\CSS\Rule\Rule
16+
*/
17+
final class RuleTest extends TestCase
18+
{
19+
/**
20+
* @return array<string, array{0: string, 1: array<int, string>}>
21+
*/
22+
public static function provideRulesAndExpectedParsedValueListTypes(): array
23+
{
24+
return [
25+
'src (e.g. in @font-face)' => [
26+
"
27+
src: url('../fonts/open-sans-italic-300.woff2') format('woff2'),
28+
url('../fonts/open-sans-italic-300.ttf') format('truetype');
29+
",
30+
[RuleValueList::class, RuleValueList::class],
31+
],
32+
];
33+
}
34+
35+
/**
36+
* @test
37+
*
38+
* @param array<int, string> $expectedTypeClassnames
39+
*
40+
* @dataProvider provideRulesAndExpectedParsedValueListTypes
41+
*/
42+
public function parsesValuesIntoExpectedTypeList(string $rule, array $expectedTypeClassnames): void
43+
{
44+
$subject = Rule::parse(new ParserState($rule, Settings::create()));
45+
46+
$value = $subject->getValue();
47+
self::assertInstanceOf(ValueList::class, $value);
48+
49+
$actualClassnames = \array_map(
50+
function ($component): string {
51+
return \get_class($component);
52+
},
53+
$value->getListComponents()
54+
);
55+
56+
self::assertSame($expectedTypeClassnames, $actualClassnames);
57+
}
58+
}

0 commit comments

Comments
 (0)