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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Please also have a look at our

### Changed

- The array keys passed to `DeclarationBlock::setSelectors()` are no longer
preserved (#1407)

### Deprecated

### Removed
Expand Down
19 changes: 12 additions & 7 deletions src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DeclarationBlock implements CSSElement, CSSListItem, Positionable, RuleCon
use Position;

/**
* @var array<Selector|string>
* @var list<Selector>
*/
private $selectors = [];

Expand Down Expand Up @@ -146,11 +146,13 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
public function setSelectors($selectors, ?CSSList $list = null): void
{
if (\is_array($selectors)) {
$this->selectors = $selectors;
$selectorsToSet = $selectors;
} else {
$this->selectors = \explode(',', $selectors);
$selectorsToSet = \explode(',', $selectors);
}
foreach ($this->selectors as $key => $selector) {

// Convert all items to a `Selector` if not already
foreach ($selectorsToSet as $key => $selector) {
if (!($selector instanceof Selector)) {
if ($list === null || !($list instanceof KeyFrame)) {
if (!Selector::isValid($selector)) {
Expand All @@ -160,7 +162,7 @@ public function setSelectors($selectors, ?CSSList $list = null): void
'custom'
);
}
$this->selectors[$key] = new Selector($selector);
$selectorsToSet[$key] = new Selector($selector);
} else {
if (!KeyframeSelector::isValid($selector)) {
throw new UnexpectedTokenException(
Expand All @@ -169,10 +171,13 @@ public function setSelectors($selectors, ?CSSList $list = null): void
'custom'
);
}
$this->selectors[$key] = new KeyframeSelector($selector);
$selectorsToSet[$key] = new KeyframeSelector($selector);
}
}
}

// Discard the keys and reindex the array
$this->selectors = \array_values($selectorsToSet);
}

/**
Expand All @@ -195,7 +200,7 @@ public function removeSelector($selectorToRemove): bool
}

/**
* @return array<Selector>
* @return list<Selector>
*/
public function getSelectors(): array
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,19 @@ public function getRuleSetReturnsObjectWithLineNumberPassedToConstructor(?int $l

self::assertSame($lineNumber, $result->getLineNumber());
}

/**
* @test
*
* Any type of array may be passed to the method, but the resultant property should be a `list`.
*/
public function setSelectorsIgnoresKeys(): void
{
$subject = new DeclarationBlock();
$subject->setSelectors(['Bob' => 'html', 'Mary' => 'body']);

$result = $subject->getSelectors();

self::assertSame([0, 1], \array_keys($result));
}
}