Skip to content

[TASK] Add interface RuleContainer for RuleSet #1256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Please also have a look at our

### Added

- Interface `RuleContainer` for `RuleSet` `Rule` manipulation methods (#1256)
- `RuleSet::removeMatchingRules()` method
(for the implementing classes `AtRuleSet` and `DeclarationBlock`) (#1249)
- `RuleSet::removeAllRules()` method
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@ classDiagram
class CSSListItem {
<<interface>>
}
class RuleContainer {
<<interface>>
}
class DeclarationBlock {
}
class RuleSet {
Expand Down Expand Up @@ -730,6 +733,7 @@ classDiagram
Positionable <|.. RuleSet: realization
CSSElement <|.. RuleSet: realization
CSSListItem <|.. RuleSet: realization
RuleContainer <|.. RuleSet: realization
RuleSet <|-- AtRuleSet: inheritance
AtRule <|.. AtRuleSet: realization
Renderable <|.. Selector: realization
Expand Down
3 changes: 2 additions & 1 deletion src/CSSList/CSSBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sabberworm\CSS\Property\Selector;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\RuleSet\RuleContainer;
use Sabberworm\CSS\RuleSet\RuleSet;
use Sabberworm\CSS\Value\CSSFunction;
use Sabberworm\CSS\Value\Value;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function getAllValues(
);
}
}
} elseif ($element instanceof RuleSet) {
} elseif ($element instanceof RuleContainer) {
foreach ($element->getRules($ruleSearchPattern) as $rule) {
$result = \array_merge(
$result,
Expand Down
36 changes: 36 additions & 0 deletions src/RuleSet/RuleContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\RuleSet;

use Sabberworm\CSS\Rule\Rule;

/**
* Represents a CSS item that contains `Rules`, defining the methods to manipulate them.
*/
interface RuleContainer
{
public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void;

public function removeRule(Rule $ruleToRemove): void;

public function removeMatchingRules(string $searchPattern): void;

public function removeAllRules(): void;

/**
* @param array<Rule> $rules
*/
public function setRules(array $rules): void;

/**
* @return array<int<0, max>, Rule>
*/
public function getRules(?string $searchPattern = null): array;

/**
* @return array<string, Rule>
*/
public function getRulesAssoc(?string $searchPattern = null): array;
}
2 changes: 1 addition & 1 deletion src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Note that `CSSListItem` extends both `Commentable` and `Renderable`,
* so those interfaces must also be implemented by concrete subclasses.
*/
abstract class RuleSet implements CSSElement, CSSListItem, Positionable
abstract class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
{
use CommentContainer;
use Position;
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sabberworm\CSS\CSSElement;
use Sabberworm\CSS\CSSList\CSSListItem;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\RuleSet\RuleContainer;
use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;

/**
Expand Down Expand Up @@ -41,6 +42,14 @@ public function implementsCSSListItem(): void
self::assertInstanceOf(CSSListItem::class, $this->subject);
}

/**
* @test
*/
public function implementsRuleContainer(): void
{
self::assertInstanceOf(RuleContainer::class, $this->subject);
}

/**
* @return array<string, array{0: list<Rule>, 1: string, 2: list<string>}>
*/
Expand Down