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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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>
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make this more specific as we're touching the return value anyway?

Suggested change
* @return array<int, string>
* @return list<non-empty-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).
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer this text to be part of the general description of this method instead of the @return annotation as the description is what I'd read first (as a human).

*/
private static function listDelimiterForRule($sRule): array
{
if (\preg_match('/^font($|-)/', $sRule)) {
return [',', '/', ' '];
}
return [',', ' ', '/'];

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

Choose a reason for hiding this comment

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

Maybe we can integrate the preg matching into the switch as switch also allows general conditions: https://www.php.net/manual/en/control-structures.switch.php

}
}

/**
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>}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @return array<string, array{0: string, 1: array<int, string>}>
* @return array<string, array{0: string, 1: list<class-string>}>

See:
https://phpstan.org/writing-php-code/phpdoc-types#lists
https://phpstan.org/writing-php-code/phpdoc-types#class-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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param array<int, string> $expectedTypeClassnames
* @param list<class-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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can make the function static and add a type declaration for $component.

return \get_class($component);
},
$value->getListComponents()
);

self::assertSame($expectedTypeClassnames, $actualClassnames);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we maybe use assertContainsOnlyInstancesOf instead and avoid the anonymous function?

}
}