|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sabberworm\CSS\Tests\Functional\Property; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sabberworm\CSS\CSSList\AtRuleBlockList; |
| 9 | +use Sabberworm\CSS\Parser; |
| 10 | +use Sabberworm\CSS\Property\AtRuleStatement; |
| 11 | +use Sabberworm\CSS\RuleSet\AtRuleSet; |
| 12 | +use Sabberworm\CSS\Settings; |
| 13 | + |
| 14 | +/** |
| 15 | + * @covers \Sabberworm\CSS\CSSList\CSSList |
| 16 | + * @covers \Sabberworm\CSS\Property\AtRuleStatement |
| 17 | + */ |
| 18 | +final class AtRuleStatementTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @return array<non-empty-string, array{0: non-empty-string, 1: non-empty-string, 2: string}> |
| 22 | + */ |
| 23 | + public static function provideAtRuleStatementParsingData(): array |
| 24 | + { |
| 25 | + return [ |
| 26 | + 'layer with single name' => [ |
| 27 | + '@layer reset;', |
| 28 | + 'layer', |
| 29 | + 'reset', |
| 30 | + ], |
| 31 | + 'layer with multiple names' => [ |
| 32 | + '@layer base, components;', |
| 33 | + 'layer', |
| 34 | + 'base, components', |
| 35 | + ], |
| 36 | + 'custom statement at-rule' => [ |
| 37 | + '@custom foo;', |
| 38 | + 'custom', |
| 39 | + 'foo', |
| 40 | + ], |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @test |
| 46 | + * |
| 47 | + * @param non-empty-string $css |
| 48 | + * @param non-empty-string $expectedName |
| 49 | + * |
| 50 | + * @dataProvider provideAtRuleStatementParsingData |
| 51 | + */ |
| 52 | + public function parsesAtRuleStatement(string $css, string $expectedName, string $expectedArgs): void |
| 53 | + { |
| 54 | + $contents = (new Parser($css))->parse()->getContents(); |
| 55 | + $atRuleStatement = $contents[0]; |
| 56 | + |
| 57 | + self::assertInstanceOf(AtRuleStatement::class, $atRuleStatement); |
| 58 | + self::assertSame($expectedName, $atRuleStatement->atRuleName()); |
| 59 | + self::assertSame($expectedArgs, $atRuleStatement->atRuleArgs()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @test |
| 64 | + * |
| 65 | + * @param non-empty-string $css |
| 66 | + * |
| 67 | + * @dataProvider provideAtRuleStatementParsingData |
| 68 | + */ |
| 69 | + public function parsesAtRuleStatementInStrictMode(string $css): void |
| 70 | + { |
| 71 | + $contents = (new Parser($css, Settings::create()->beStrict()))->parse()->getContents(); |
| 72 | + |
| 73 | + self::assertNotEmpty($contents, 'Failing CSS: `' . $css . '`'); |
| 74 | + self::assertInstanceOf(AtRuleStatement::class, $contents[0]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @test |
| 79 | + */ |
| 80 | + public function doesNotConsumeFollowingRulesWhenParsingLayerStatements(): void |
| 81 | + { |
| 82 | + $css = "@layer reset;\n" |
| 83 | + . "@layer base, components;\n" |
| 84 | + . "@property --tw-scale-x {\n" |
| 85 | + . " syntax: \"*\";\n" |
| 86 | + . " inherits: false;\n" |
| 87 | + . " initial-value: 1;\n" |
| 88 | + . "}\n" |
| 89 | + . "@property --tw-scale-y {\n" |
| 90 | + . " syntax: \"*\";\n" |
| 91 | + . " inherits: false;\n" |
| 92 | + . " initial-value: 1;\n" |
| 93 | + . "}\n"; |
| 94 | + |
| 95 | + $contents = (new Parser($css))->parse()->getContents(); |
| 96 | + |
| 97 | + self::assertCount(4, $contents); |
| 98 | + self::assertInstanceOf(AtRuleStatement::class, $contents[0]); |
| 99 | + self::assertSame('reset', $contents[0]->atRuleArgs()); |
| 100 | + self::assertInstanceOf(AtRuleStatement::class, $contents[1]); |
| 101 | + self::assertSame('base, components', $contents[1]->atRuleArgs()); |
| 102 | + self::assertInstanceOf(AtRuleSet::class, $contents[2]); |
| 103 | + self::assertSame('property', $contents[2]->atRuleName()); |
| 104 | + self::assertInstanceOf(AtRuleSet::class, $contents[3]); |
| 105 | + self::assertSame('property', $contents[3]->atRuleName()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @test |
| 110 | + */ |
| 111 | + public function parsesLayerStatementThenLayerBlockInTheSameDocument(): void |
| 112 | + { |
| 113 | + $css = '@layer reset; @layer theme { .button { color: blue; } }'; |
| 114 | + |
| 115 | + $contents = (new Parser($css))->parse()->getContents(); |
| 116 | + |
| 117 | + self::assertCount(2, $contents); |
| 118 | + self::assertInstanceOf(AtRuleStatement::class, $contents[0]); |
| 119 | + self::assertSame('reset', $contents[0]->atRuleArgs()); |
| 120 | + self::assertInstanceOf(AtRuleBlockList::class, $contents[1]); |
| 121 | + self::assertSame('theme', $contents[1]->atRuleArgs()); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @test |
| 126 | + */ |
| 127 | + public function rendersParsedLayerStatementWithTrailingSemicolon(): void |
| 128 | + { |
| 129 | + $rendered = (new Parser('@layer reset;'))->parse()->render(); |
| 130 | + |
| 131 | + self::assertStringContainsString('@layer reset;', $rendered); |
| 132 | + } |
| 133 | +} |
0 commit comments