diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index 76216279..0f2817c3 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -20,6 +20,19 @@ */ abstract class CSSBlockList extends CSSList { + /** + * Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are. + * + * @return array + */ + public function getAllDeclarationBlocks(): array + { + /** @var array $result */ + $result = []; + $this->allDeclarationBlocks($result); + return $result; + } + /** * @param array $result */ diff --git a/src/CSSList/Document.php b/src/CSSList/Document.php index 00d4cabd..06a8064f 100644 --- a/src/CSSList/Document.php +++ b/src/CSSList/Document.php @@ -8,7 +8,6 @@ use Sabberworm\CSS\Parsing\ParserState; use Sabberworm\CSS\Parsing\SourceException; use Sabberworm\CSS\Property\Selector; -use Sabberworm\CSS\RuleSet\DeclarationBlock; use Sabberworm\CSS\RuleSet\RuleSet; use Sabberworm\CSS\Value\Value; @@ -30,19 +29,6 @@ public static function parse(ParserState $parserState): Document return $oDocument; } - /** - * Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are. - * - * @return array - */ - public function getAllDeclarationBlocks(): array - { - /** @var array $result */ - $result = []; - $this->allDeclarationBlocks($result); - return $result; - } - /** * Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are. * diff --git a/tests/Unit/CSSList/CSSBlockListTest.php b/tests/Unit/CSSList/CSSBlockListTest.php index 9d7ef076..b44f7114 100644 --- a/tests/Unit/CSSList/CSSBlockListTest.php +++ b/tests/Unit/CSSList/CSSBlockListTest.php @@ -6,9 +6,15 @@ use PHPUnit\Framework\TestCase; use Sabberworm\CSS\Comment\Commentable; +use Sabberworm\CSS\CSSList\AtRuleBlockList; use Sabberworm\CSS\CSSList\CSSList; +use Sabberworm\CSS\Property\Charset; +use Sabberworm\CSS\Property\Import; use Sabberworm\CSS\Renderable; +use Sabberworm\CSS\RuleSet\DeclarationBlock; use Sabberworm\CSS\Tests\Unit\CSSList\Fixtures\ConcreteCSSBlockList; +use Sabberworm\CSS\Value\CSSString; +use Sabberworm\CSS\Value\URL; /** * @covers \Sabberworm\CSS\CSSList\CSSBlockList @@ -45,4 +51,92 @@ public function isCSSList(): void self::assertInstanceOf(CSSList::class, $subject); } + + /** + * @test + */ + public function getAllDeclarationBlocksForNoContentsReturnsEmptyArray(): void + { + $subject = new ConcreteCSSBlockList(); + + self::assertSame([], $subject->getAllDeclarationBlocks()); + } + + /** + * @test + */ + public function getAllDeclarationBlocksCanReturnOneDirectDeclarationBlockContent(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock = new DeclarationBlock(); + $subject->setContents([$declarationBlock]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksCanReturnMultipleDirectDeclarationBlockContents(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock1 = new DeclarationBlock(); + $declarationBlock2 = new DeclarationBlock(); + $subject->setContents([$declarationBlock1, $declarationBlock2]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([$declarationBlock1, $declarationBlock2], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksReturnsDeclarationBlocksWithinAtRuleBlockList(): void + { + $subject = new ConcreteCSSBlockList(); + + $declarationBlock = new DeclarationBlock(); + $atRuleBlockList = new AtRuleBlockList('media'); + $atRuleBlockList->setContents([$declarationBlock]); + $subject->setContents([$atRuleBlockList]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([$declarationBlock], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksIgnoresImport(): void + { + $subject = new ConcreteCSSBlockList(); + + $import = new Import(new URL(new CSSString('https://www.example.com/')), ''); + $subject->setContents([$import]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([], $result); + } + + /** + * @test + */ + public function getAllDeclarationBlocksIgnoresCharset(): void + { + $subject = new ConcreteCSSBlockList(); + + $charset = new Charset(new CSSString('UTF-8')); + $subject->setContents([$charset]); + + $result = $subject->getAllDeclarationBlocks(); + + self::assertSame([], $result); + } } diff --git a/tests/Unit/CSSList/DocumentTest.php b/tests/Unit/CSSList/DocumentTest.php index 07afcf1b..509b4b9d 100644 --- a/tests/Unit/CSSList/DocumentTest.php +++ b/tests/Unit/CSSList/DocumentTest.php @@ -61,94 +61,6 @@ public function isCSSList(): void self::assertInstanceOf(CSSList::class, $subject); } - /** - * @test - */ - public function getAllDeclarationBlocksForNoContentsReturnsEmptyArray(): void - { - $subject = new Document(); - - self::assertSame([], $subject->getAllDeclarationBlocks()); - } - - /** - * @test - */ - public function getAllDeclarationBlocksCanReturnOneDirectDeclarationBlockContent(): void - { - $subject = new Document(); - - $declarationBlock = new DeclarationBlock(); - $subject->setContents([$declarationBlock]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([$declarationBlock], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksCanReturnMultipleDirectDeclarationBlockContents(): void - { - $subject = new Document(); - - $declarationBlock1 = new DeclarationBlock(); - $declarationBlock2 = new DeclarationBlock(); - $subject->setContents([$declarationBlock1, $declarationBlock2]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([$declarationBlock1, $declarationBlock2], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksReturnsDeclarationBlocksWithinAtRuleBlockList(): void - { - $subject = new Document(); - - $declarationBlock = new DeclarationBlock(); - $atRuleBlockList = new AtRuleBlockList('media'); - $atRuleBlockList->setContents([$declarationBlock]); - $subject->setContents([$atRuleBlockList]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([$declarationBlock], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksIgnoresImport(): void - { - $subject = new Document(); - - $import = new Import(new URL(new CSSString('https://www.example.com/')), ''); - $subject->setContents([$import]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([], $result); - } - - /** - * @test - */ - public function getAllDeclarationBlocksIgnoresCharset(): void - { - $subject = new Document(); - - $charset = new Charset(new CSSString('UTF-8')); - $subject->setContents([$charset]); - - $result = $subject->getAllDeclarationBlocks(); - - self::assertSame([], $result); - } - /** * @test */