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
11 changes: 10 additions & 1 deletion src/Property/Declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\Position\Position;
use Sabberworm\CSS\Position\Positionable;
use Sabberworm\CSS\ShortClassNameProvider;
use Sabberworm\CSS\Value\RuleValueList;
use Sabberworm\CSS\Value\Value;

Expand All @@ -28,6 +29,7 @@ class Declaration implements Commentable, CSSElement, Positionable
{
use CommentContainer;
use Position;
use ShortClassNameProvider;

/**
* @var non-empty-string
Expand Down Expand Up @@ -226,6 +228,13 @@ public function render(OutputFormat $outputFormat): string
*/
public function getArrayRepresentation(): array
{
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
return [
'class' => $this->getShortClassName(),
'propertyName' => $this->propertyName,
// We're using the term "property value" here to match the wording used in the specs:
// https://www.w3.org/TR/CSS22/syndata.html#declaration
'propertyValue' => $this->value,
'important' => $this->isImportant,
Comment on lines +233 to +237
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take all the Git Blame for this possible slight misnomer in the class. I lifted the terminology from MDN which currently still refers to the CSSOM properties for a 'CSS Declaration' as "property name" and "value". Though it does refer to isImportant as "important flag".

I think we are close enough to the mark to avoid confusion, and the official nomenclature may change in future W3C revisions anyway.

];
}
}
61 changes: 57 additions & 4 deletions tests/Unit/Property/DeclarationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,65 @@ static function ($component): string {
/**
* @test
*/
public function getArrayRepresentationThrowsException(): void
public function getArrayRepresentationIncludesClassName(): void
{
$this->expectException(\BadMethodCallException::class);
$subject = new Declaration('line-height');

$subject = new Declaration('todo');
$result = $subject->getArrayRepresentation();

$subject->getArrayRepresentation();
self::assertSame('Declaration', $result['class']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesPropertyName(): void
{
$propertyName = 'font-weight';
$subject = new Declaration($propertyName);

$result = $subject->getArrayRepresentation();

self::assertSame($propertyName, $result['propertyName']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesPropertyValue(): void
{
$subject = new Declaration('font-weight');
$propertyValue = 'bold';
$subject->setValue($propertyValue);

$result = $subject->getArrayRepresentation();

self::assertSame($propertyValue, $result['propertyValue']);
}

/**
* @return array<non-empty-string, array{0: bool}>
*/
public static function provideBooleans(): array
{
return [
'true' => [true],
'false' => [false],
];
}

/**
* @test
*
* @dataProvider provideBooleans
*/
public function getArrayRepresentationIncludesImportantFlag(bool $important): void
{
$subject = new Declaration('font-weight');
$subject->setIsImportant($important);

$result = $subject->getArrayRepresentation();

self::assertSame($important, $result['important']);
}
}