Skip to content

[TASK] Use delegation for DeclarationBlock -> RuleSet #1194

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JakeQZ
Copy link
Collaborator

@JakeQZ JakeQZ commented Mar 19, 2025

... rather than inheritance.

This will allow DeclarationBlock to instead extend CSSBlockList in order to support
CSS nesting.

This is a slightly-breaking change, since now CSSBlockList::getAllRuleSets() will include the RuleSet property of the DeclarationBlock instead of the DeclarationBlock itself.

Part of #1170.

@JakeQZ JakeQZ added cleanup css4 Relating to features introduced in CSS4 labels Mar 19, 2025
@JakeQZ JakeQZ self-assigned this Mar 19, 2025
@JakeQZ JakeQZ marked this pull request as draft March 19, 2025 00:56
@coveralls
Copy link

coveralls commented Mar 19, 2025

Coverage Status

coverage: 54.681% (-2.4%) from 57.038%
when pulling 4b3466d on task/declarationblock-delegate-ruleset
into bddb086 on main.

@JakeQZ JakeQZ force-pushed the task/declarationblock-delegate-ruleset branch from 7800603 to 73956e9 Compare March 19, 2025 02:06
@JakeQZ JakeQZ added css3 and removed css4 Relating to features introduced in CSS4 labels Mar 20, 2025
@JakeQZ JakeQZ force-pushed the task/declarationblock-delegate-ruleset branch 5 times, most recently from c1c27a9 to 3a278e0 Compare April 2, 2025 00:31
@JakeQZ JakeQZ force-pushed the task/declarationblock-delegate-ruleset branch 5 times, most recently from c935f2c to 1f908cf Compare April 14, 2025 15:25
@JakeQZ
Copy link
Collaborator Author

JakeQZ commented Apr 15, 2025

Would be good to resolve #1247 before moving on with this.

... rather than inheritance.

This will allow `DeclarationBlock` to instead extend `CSSBlockList` in order
to support
[CSS nesting](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_nesting).

This is a slightly-breaking change, since now `CSSBlockList::getAllRuleSets()`
will include the `RuleSet` property of the `DeclarationBlock` instead of the
`DeclarationBlock` itself.

Part of #1170.
@JakeQZ JakeQZ force-pushed the task/declarationblock-delegate-ruleset branch from 1f908cf to 1ce18b7 Compare May 6, 2025 17:40
@JakeQZ
Copy link
Collaborator Author

JakeQZ commented May 8, 2025

This is almost ready for review, but some tests need to be added. Before doing so, I'd appreciate a pre-review from you: @sabberworm, @oliverklee.

I'm not keen on the having the various chaining methods in DeclarationBlock, but to avoid breaking changes they are necessary. A trait can be used for testing them in the same way as their counterparts in RuleSet.

For DeclarationBlock to contain nested rules, it needs to extend CSSBlockList. RuleSet cannot be a trait; it needs to be a fully-fledged type for methods like getAllRuleSets().

This is the only realistic way forward I see for now, in an 'Agile' sense, which can deliver what is required now, without wholescale refactoring or breaking changes.

Copy link
Collaborator

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

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

I like this approach!

@@ -95,7 +97,7 @@ public function getAllValues(
);
}
}
} elseif ($element instanceof RuleSet) {
} elseif ($element instanceof RuleSet || $element instanceof DeclarationBlock) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can have an interface that both classes implement so we can check for the interface here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Interestingly, this resolves the PHPStan warning (because the parameter type is now required to match the interface).

@@ -30,7 +30,7 @@
*
* Note that `CSSListItem` extends both `Commentable` and `Renderable`, so those interfaces must also be implemented.
*/
class DeclarationBlock implements CSSElement, CSSListItem, Positionable
class DeclarationBlock implements CSSElement, CSSListItem, Positionable, RuleContainer
Copy link
Collaborator

Choose a reason for hiding this comment

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

We also should have tests that the two classes implement the interface.

@oliverklee
Copy link
Collaborator

Maybe as another spin-off PR (or prepatch), we can introduce the interface and have one class implement it.

JakeQZ added a commit that referenced this pull request May 9, 2025
This covers the maniplation of `Rule`s within the container,
and may be implemented by other classes in future (e.g. #1194).

Note that the naming is consistent with the current codebase,
rather than what the CSS entities are now called:
- `Rule` represents what is now called a "declaration";
- `RuleSet` represents what is now called a "declaration block";
- `DeclarationBlock` represents what is now called a "style rule";
- `CSSListItem` represents what is now generically called a "rule".

Renaming things is part of a longer-term plan touched on in #1189.
JakeQZ added a commit that referenced this pull request May 9, 2025
This covers the maniplation of `Rule`s within the container,
and may be implemented by other classes in future (e.g. #1194).

Note that the naming is consistent with the current codebase,
rather than what the CSS entities are now called:
- `Rule` represents what is now called a "declaration";
- `RuleSet` represents what is now called a "declaration block";
- `DeclarationBlock` represents what is now called a "style rule";
- `CSSListItem` (closely) represents what is now generically called a "rule".

Renaming things is part of a longer-term plan touched on in #1189.
@JakeQZ
Copy link
Collaborator Author

JakeQZ commented May 9, 2025

Maybe as another spin-off PR (or prepatch), we can introduce the interface and have one class implement it.

#1256.

JakeQZ added a commit that referenced this pull request May 10, 2025
This covers the maniplation of `Rule`s within the container,
and may be implemented by other classes in future (e.g. #1194).

Note that the naming is consistent with the current codebase,
rather than what the CSS entities are now called:
- `Rule` represents what is now called a "declaration";
- `RuleSet` represents what is now called a "declaration block";
- `DeclarationBlock` represents what is now called a "style rule";
- `CSSListItem` (closely) represents what is now generically called a "rule".

Renaming things is part of a longer-term plan touched on in #1189.
oliverklee pushed a commit that referenced this pull request May 10, 2025
This covers the maniplation of `Rule`s within the container,
and may be implemented by other classes in future (e.g. #1194).

Note that the naming is consistent with the current codebase,
rather than what the CSS entities are now called:
- `Rule` represents what is now called a "declaration";
- `RuleSet` represents what is now called a "declaration block";
- `DeclarationBlock` represents what is now called a "style rule";
- `CSSListItem` (closely) represents what is now generically called a "rule".

Renaming things is part of a longer-term plan touched on in #1189.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants