Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/CSSList/CSSBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
*/
abstract class CSSBlockList extends CSSList
{
/**
* Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are.
*
* @return array<int, DeclarationBlock>
*/
public function getAllDeclarationBlocks(): array
{
/** @var array<int, DeclarationBlock> $result */
$result = [];
$this->allDeclarationBlocks($result);
return $result;
}

/**
* @param array<int, DeclarationBlock> $result
*/
Expand Down
14 changes: 0 additions & 14 deletions src/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<int, DeclarationBlock>
*/
public function getAllDeclarationBlocks(): array
{
/** @var array<int, DeclarationBlock> $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.
*
Expand Down
94 changes: 94 additions & 0 deletions tests/Unit/CSSList/CSSBlockListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
88 changes: 0 additions & 88 deletions tests/Unit/CSSList/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down