Skip to content

Commit f912e71

Browse files
authored
[TASK] Move up getAllRuleSets to CSSBlockList (#1047)
Move this method up without any changes to the method or its callers. We'll clean this up in later changes. Part of #994.
1 parent 713eec9 commit f912e71

File tree

4 files changed

+150
-156
lines changed

4 files changed

+150
-156
lines changed

src/CSSList/CSSBlockList.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ protected function allDeclarationBlocks(array &$result): void
5454
}
5555
}
5656

57+
/**
58+
* Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are.
59+
*
60+
* @return array<int, RuleSet>
61+
*/
62+
public function getAllRuleSets(): array
63+
{
64+
/** @var array<int, RuleSet> $result */
65+
$result = [];
66+
$this->allRuleSets($result);
67+
return $result;
68+
}
69+
5770
/**
5871
* @param array<int, RuleSet> $result
5972
*/

src/CSSList/Document.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@ public static function parse(ParserState $parserState): Document
3030
return $document;
3131
}
3232

33-
/**
34-
* Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are.
35-
*
36-
* @return array<int, RuleSet>
37-
*/
38-
public function getAllRuleSets(): array
39-
{
40-
/** @var array<int, RuleSet> $result */
41-
$result = [];
42-
$this->allRuleSets($result);
43-
return $result;
44-
}
45-
4633
/**
4734
* Returns all `Value` objects found recursively in `Rule`s in the tree.
4835
*

tests/Unit/CSSList/CSSBlockListTest.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Sabberworm\CSS\Property\Charset;
1212
use Sabberworm\CSS\Property\Import;
1313
use Sabberworm\CSS\Renderable;
14+
use Sabberworm\CSS\RuleSet\AtRuleSet;
1415
use Sabberworm\CSS\RuleSet\DeclarationBlock;
1516
use Sabberworm\CSS\Tests\Unit\CSSList\Fixtures\ConcreteCSSBlockList;
1617
use Sabberworm\CSS\Value\CSSString;
@@ -139,4 +140,140 @@ public function getAllDeclarationBlocksIgnoresCharset(): void
139140

140141
self::assertSame([], $result);
141142
}
143+
144+
/**
145+
* @test
146+
*/
147+
public function getAllRuleSetsWhenNoContentSetReturnsEmptyArray(): void
148+
{
149+
$subject = new ConcreteCSSBlockList();
150+
151+
self::assertSame([], $subject->getAllRuleSets());
152+
}
153+
154+
/**
155+
* @test
156+
*/
157+
public function getAllRuleSetsReturnsOneDeclarationBlockDirectlySetAsContent(): void
158+
{
159+
$subject = new ConcreteCSSBlockList();
160+
161+
$declarationBlock = new DeclarationBlock();
162+
$subject->setContents([$declarationBlock]);
163+
164+
$result = $subject->getAllRuleSets();
165+
166+
self::assertSame([$declarationBlock], $result);
167+
}
168+
169+
/**
170+
* @test
171+
*/
172+
public function getAllRuleSetsReturnsOneAtRuleSetDirectlySetAsContent(): void
173+
{
174+
$subject = new ConcreteCSSBlockList();
175+
176+
$atRuleSet = new AtRuleSet('media');
177+
$subject->setContents([$atRuleSet]);
178+
179+
$result = $subject->getAllRuleSets();
180+
181+
self::assertSame([$atRuleSet], $result);
182+
}
183+
184+
/**
185+
* @test
186+
*/
187+
public function getAllRuleSetsReturnsMultipleDeclarationBlocksDirectlySetAsContents(): void
188+
{
189+
$subject = new ConcreteCSSBlockList();
190+
191+
$declarationBlock1 = new DeclarationBlock();
192+
$declarationBlock2 = new DeclarationBlock();
193+
$subject->setContents([$declarationBlock1, $declarationBlock2]);
194+
195+
$result = $subject->getAllRuleSets();
196+
197+
self::assertSame([$declarationBlock1, $declarationBlock2], $result);
198+
}
199+
200+
/**
201+
* @test
202+
*/
203+
public function getAllRuleSetsReturnsMultipleAtRuleSetsDirectlySetAsContents(): void
204+
{
205+
$subject = new ConcreteCSSBlockList();
206+
207+
$atRuleSet1 = new AtRuleSet('media');
208+
$atRuleSet2 = new AtRuleSet('media');
209+
$subject->setContents([$atRuleSet1, $atRuleSet2]);
210+
211+
$result = $subject->getAllRuleSets();
212+
213+
self::assertSame([$atRuleSet1, $atRuleSet2], $result);
214+
}
215+
216+
/**
217+
* @test
218+
*/
219+
public function getAllRuleSetsReturnsDeclarationBlocksWithinAtRuleBlockList(): void
220+
{
221+
$subject = new ConcreteCSSBlockList();
222+
223+
$declarationBlock = new DeclarationBlock();
224+
$atRuleBlockList = new AtRuleBlockList('media');
225+
$atRuleBlockList->setContents([$declarationBlock]);
226+
$subject->setContents([$atRuleBlockList]);
227+
228+
$result = $subject->getAllRuleSets();
229+
230+
self::assertSame([$declarationBlock], $result);
231+
}
232+
233+
/**
234+
* @test
235+
*/
236+
public function getAllRuleSetsReturnsAtRuleSetsWithinAtRuleBlockList(): void
237+
{
238+
$subject = new ConcreteCSSBlockList();
239+
240+
$atRule = new AtRuleSet('media');
241+
$atRuleBlockList = new AtRuleBlockList('media');
242+
$atRuleBlockList->setContents([$atRule]);
243+
$subject->setContents([$atRuleBlockList]);
244+
245+
$result = $subject->getAllRuleSets();
246+
247+
self::assertSame([$atRule], $result);
248+
}
249+
250+
/**
251+
* @test
252+
*/
253+
public function getAllRuleSetsIgnoresImport(): void
254+
{
255+
$subject = new ConcreteCSSBlockList();
256+
257+
$import = new Import(new URL(new CSSString('https://www.example.com/')), '');
258+
$subject->setContents([$import]);
259+
260+
$result = $subject->getAllRuleSets();
261+
262+
self::assertSame([], $result);
263+
}
264+
265+
/**
266+
* @test
267+
*/
268+
public function getAllRuleSetsIgnoresCharset(): void
269+
{
270+
$subject = new ConcreteCSSBlockList();
271+
272+
$charset = new Charset(new CSSString('UTF-8'));
273+
$subject->setContents([$charset]);
274+
275+
$result = $subject->getAllRuleSets();
276+
277+
self::assertSame([], $result);
278+
}
142279
}

tests/Unit/CSSList/DocumentTest.php

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\Comment\Commentable;
9-
use Sabberworm\CSS\CSSList\AtRuleBlockList;
109
use Sabberworm\CSS\CSSList\CSSBlockList;
1110
use Sabberworm\CSS\CSSList\CSSList;
1211
use Sabberworm\CSS\CSSList\Document;
13-
use Sabberworm\CSS\Property\Charset;
14-
use Sabberworm\CSS\Property\Import;
1512
use Sabberworm\CSS\Renderable;
16-
use Sabberworm\CSS\RuleSet\AtRuleSet;
17-
use Sabberworm\CSS\RuleSet\DeclarationBlock;
18-
use Sabberworm\CSS\Value\CSSString;
19-
use Sabberworm\CSS\Value\URL;
2013

2114
/**
2215
* @covers \Sabberworm\CSS\CSSList\CSSBlockList
@@ -61,142 +54,6 @@ public function isCSSList(): void
6154
self::assertInstanceOf(CSSList::class, $subject);
6255
}
6356

64-
/**
65-
* @test
66-
*/
67-
public function getAllRuleSetsWhenNoContentSetReturnsEmptyArray(): void
68-
{
69-
$subject = new Document();
70-
71-
self::assertSame([], $subject->getAllRuleSets());
72-
}
73-
74-
/**
75-
* @test
76-
*/
77-
public function getAllRuleSetsReturnsOneDeclarationBlockDirectlySetAsContent(): void
78-
{
79-
$subject = new Document();
80-
81-
$declarationBlock = new DeclarationBlock();
82-
$subject->setContents([$declarationBlock]);
83-
84-
$result = $subject->getAllRuleSets();
85-
86-
self::assertSame([$declarationBlock], $result);
87-
}
88-
89-
/**
90-
* @test
91-
*/
92-
public function getAllRuleSetsReturnsOneAtRuleSetDirectlySetAsContent(): void
93-
{
94-
$subject = new Document();
95-
96-
$atRuleSet = new AtRuleSet('media');
97-
$subject->setContents([$atRuleSet]);
98-
99-
$result = $subject->getAllRuleSets();
100-
101-
self::assertSame([$atRuleSet], $result);
102-
}
103-
104-
/**
105-
* @test
106-
*/
107-
public function getAllRuleSetsReturnsMultipleDeclarationBlocksDirectlySetAsContents(): void
108-
{
109-
$subject = new Document();
110-
111-
$declarationBlock1 = new DeclarationBlock();
112-
$declarationBlock2 = new DeclarationBlock();
113-
$subject->setContents([$declarationBlock1, $declarationBlock2]);
114-
115-
$result = $subject->getAllRuleSets();
116-
117-
self::assertSame([$declarationBlock1, $declarationBlock2], $result);
118-
}
119-
120-
/**
121-
* @test
122-
*/
123-
public function getAllRuleSetsReturnsMultipleAtRuleSetsDirectlySetAsContents(): void
124-
{
125-
$subject = new Document();
126-
127-
$atRuleSet1 = new AtRuleSet('media');
128-
$atRuleSet2 = new AtRuleSet('media');
129-
$subject->setContents([$atRuleSet1, $atRuleSet2]);
130-
131-
$result = $subject->getAllRuleSets();
132-
133-
self::assertSame([$atRuleSet1, $atRuleSet2], $result);
134-
}
135-
136-
/**
137-
* @test
138-
*/
139-
public function getAllRuleSetsReturnsDeclarationBlocksWithinAtRuleBlockList(): void
140-
{
141-
$subject = new Document();
142-
143-
$declarationBlock = new DeclarationBlock();
144-
$atRuleBlockList = new AtRuleBlockList('media');
145-
$atRuleBlockList->setContents([$declarationBlock]);
146-
$subject->setContents([$atRuleBlockList]);
147-
148-
$result = $subject->getAllRuleSets();
149-
150-
self::assertSame([$declarationBlock], $result);
151-
}
152-
153-
/**
154-
* @test
155-
*/
156-
public function getAllRuleSetsReturnsAtRuleSetsWithinAtRuleBlockList(): void
157-
{
158-
$subject = new Document();
159-
160-
$atRule = new AtRuleSet('media');
161-
$atRuleBlockList = new AtRuleBlockList('media');
162-
$atRuleBlockList->setContents([$atRule]);
163-
$subject->setContents([$atRuleBlockList]);
164-
165-
$result = $subject->getAllRuleSets();
166-
167-
self::assertSame([$atRule], $result);
168-
}
169-
170-
/**
171-
* @test
172-
*/
173-
public function getAllRuleSetsIgnoresImport(): void
174-
{
175-
$subject = new Document();
176-
177-
$import = new Import(new URL(new CSSString('https://www.example.com/')), '');
178-
$subject->setContents([$import]);
179-
180-
$result = $subject->getAllRuleSets();
181-
182-
self::assertSame([], $result);
183-
}
184-
185-
/**
186-
* @test
187-
*/
188-
public function getAllRuleSetsIgnoresCharset(): void
189-
{
190-
$subject = new Document();
191-
192-
$charset = new Charset(new CSSString('UTF-8'));
193-
$subject->setContents([$charset]);
194-
195-
$result = $subject->getAllRuleSets();
196-
197-
self::assertSame([], $result);
198-
}
199-
20057
/**
20158
* @test
20259
*/

0 commit comments

Comments
 (0)