-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathDeclarationBlockTest.php
More file actions
135 lines (111 loc) · 4.58 KB
/
Copy pathDeclarationBlockTest.php
File metadata and controls
135 lines (111 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\RuleSet;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Property\Declaration;
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\Settings as ParserSettings;
use Sabberworm\CSS\Value\Size;
/**
* @covers \Sabberworm\CSS\RuleSet\DeclarationBlock
*/
final class DeclarationBlockTest extends TestCase
{
/**
* @test
*/
public function overrideRules(): void
{
$css = '.wrapper { left: 10px; text-align: left; }';
$parser = new Parser($css);
$document = $parser->parse();
$declaration = new Declaration('right');
$declaration->setValue('-10px');
$contents = $document->getContents();
$wrapper = $contents[0];
self::assertInstanceOf(DeclarationBlock::class, $wrapper);
self::assertCount(2, $wrapper->getRules());
$wrapper->setRules([$declaration]);
$declarations = $wrapper->getRules();
self::assertCount(1, $declarations);
self::assertSame('right', $declarations[0]->getPropertyName());
self::assertSame('-10px', $declarations[0]->getValue());
}
/**
* @test
*/
public function declarationInsertion(): void
{
$css = '.wrapper { left: 10px; text-align: left; }';
$parser = new Parser($css);
$document = $parser->parse();
$contents = $document->getContents();
$wrapper = $contents[0];
self::assertInstanceOf(DeclarationBlock::class, $wrapper);
$leftDeclarations = $wrapper->getRules('left');
self::assertCount(1, $leftDeclarations);
$firstLeftDeclaration = $leftDeclarations[0];
$textDeclarations = $wrapper->getRules('text-');
self::assertCount(1, $textDeclarations);
$firstTextDeclaration = $textDeclarations[0];
$leftPrefixDeclaration = new Declaration('left');
$leftPrefixDeclaration->setValue(new Size(16, 'em'));
$textAlignDeclaration = new Declaration('text-align');
$textAlignDeclaration->setValue(new Size(1));
$borderBottomDeclaration = new Declaration('border-bottom-width');
$borderBottomDeclaration->setValue(new Size(1, 'px'));
$wrapper->addRule($borderBottomDeclaration);
$wrapper->addRule($leftPrefixDeclaration, $firstLeftDeclaration);
$wrapper->addRule($textAlignDeclaration, $firstTextDeclaration);
$declarations = $wrapper->getRules();
self::assertSame($leftPrefixDeclaration, $declarations[0]);
self::assertSame($firstLeftDeclaration, $declarations[1]);
self::assertSame($textAlignDeclaration, $declarations[2]);
self::assertSame($firstTextDeclaration, $declarations[3]);
self::assertSame($borderBottomDeclaration, $declarations[4]);
self::assertSame(
'.wrapper {left: 16em;left: 10px;text-align: 1;text-align: left;border-bottom-width: 1px;}',
$document->render()
);
}
/**
* @return array<string, array{0: non-empty-string, 1: non-empty-string}>
*/
public static function declarationBlocksWithCommentsProvider(): array
{
return [
'CSS comments with one asterisk' => ['p {color: #000;/* black */}', 'p {color: #000;}'],
'CSS comments with two asterisks' => ['p {color: #000;/** black */}', 'p {color: #000;}'],
];
}
/**
* @test
* @dataProvider declarationBlocksWithCommentsProvider
*/
public function canRemoveCommentsFromDeclarationsUsingLenientParsing(
string $cssWithComments,
string $cssWithoutComments
): void {
$parserSettings = ParserSettings::create()->withLenientParsing(true);
$document = (new Parser($cssWithComments, $parserSettings))->parse();
$outputFormat = (new OutputFormat())->setRenderComments(false);
$renderedDocument = $document->render($outputFormat);
self::assertSame($cssWithoutComments, $renderedDocument);
}
/**
* @test
* @dataProvider declarationBlocksWithCommentsProvider
*/
public function canRemoveCommentsFromDeclarationsUsingStrictParsing(
string $cssWithComments,
string $cssWithoutComments
): void {
$parserSettings = ParserSettings::create()->withLenientParsing(false);
$document = (new Parser($cssWithComments, $parserSettings))->parse();
$outputFormat = (new OutputFormat())->setRenderComments(false);
$renderedDocument = $document->render($outputFormat);
self::assertSame($cssWithoutComments, $renderedDocument);
}
}