44
55namespace Sabberworm \CSS \RuleSet ;
66
7+ use Sabberworm \CSS \Comment \Comment ;
8+ use Sabberworm \CSS \Comment \Commentable ;
79use Sabberworm \CSS \CSSList \CSSList ;
810use Sabberworm \CSS \CSSList \KeyFrame ;
911use Sabberworm \CSS \OutputFormat ;
1315use Sabberworm \CSS \Parsing \UnexpectedTokenException ;
1416use Sabberworm \CSS \Property \KeyframeSelector ;
1517use Sabberworm \CSS \Property \Selector ;
18+ use Sabberworm \CSS \Renderable ;
19+ use Sabberworm \CSS \Rule \Rule ;
1620
1721/**
18- * This class represents a `RuleSet` constrained by a `Selector`.
22+ * This class includes a `RuleSet` constrained by a `Selector`.
1923 *
2024 * It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
2125 * matching elements.
2226 *
2327 * Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
2428 */
25- class DeclarationBlock extends RuleSet
29+ class DeclarationBlock implements Commentable, Renderable
2630{
2731 /**
2832 * @var array<Selector|string>
2933 */
3034 private $ selectors = [];
3135
36+ /**
37+ * @var RuleSet
38+ */
39+ private $ ruleSet ;
40+
41+ /**
42+ * @var int<0, max>
43+ */
44+ private $ lineNumber ;
45+
46+ /**
47+ * @var list<Comment>
48+ */
49+ private $ comments = [];
50+
51+ /**
52+ * @param int<0, max> $lineNumber
53+ */
54+ public function __construct (int $ lineNumber = 0 )
55+ {
56+ $ this ->lineNumber = $ lineNumber ;
57+ $ this ->ruleSet = new RuleSet ($ lineNumber );
58+ }
59+
3260 /**
3361 * @return DeclarationBlock|false
3462 *
@@ -69,10 +97,20 @@ public static function parse(ParserState $parserState, ?CSSList $list = null)
6997 }
7098 }
7199 $ result ->setComments ($ comments );
72- RuleSet::parseRuleSet ($ parserState , $ result );
100+
101+ RuleSet::parseRuleSet ($ parserState , $ result ->ruleSet );
102+
73103 return $ result ;
74104 }
75105
106+ /**
107+ * @return int<0, max>
108+ */
109+ public function getLineNo (): int
110+ {
111+ return $ this ->lineNumber ;
112+ }
113+
76114 /**
77115 * @param array<Selector|string>|string $selectors
78116 *
@@ -137,6 +175,63 @@ public function getSelectors(): array
137175 return $ this ->selectors ;
138176 }
139177
178+ public function getRuleSet (): RuleSet
179+ {
180+ return $ this ->ruleSet ;
181+ }
182+
183+ /**
184+ * @see RuleSet::addRule()
185+ */
186+ public function addRule (Rule $ ruleToAdd , ?Rule $ sibling = null ): void
187+ {
188+ $ this ->ruleSet ->addRule ($ ruleToAdd , $ sibling );
189+ }
190+
191+ /**
192+ * @see RuleSet::getRules()
193+ *
194+ * @param Rule|string|null $searchPattern
195+ *
196+ * @return array<int<0, max>, Rule>
197+ */
198+ public function getRules ($ searchPattern = null ): array
199+ {
200+ return $ this ->ruleSet ->getRules ($ searchPattern );
201+ }
202+
203+ /**
204+ * @see RuleSet::setRules()
205+ *
206+ * @param array<Rule> $rules
207+ */
208+ public function setRules (array $ rules ): void
209+ {
210+ $ this ->ruleSet ->setRules ($ rules );
211+ }
212+
213+ /**
214+ * @see RuleSet::getRulesAssoc()
215+ *
216+ * @param Rule|string|null $searchPattern
217+ *
218+ * @return array<string, Rule>
219+ */
220+ public function getRulesAssoc ($ searchPattern = null ): array
221+ {
222+ return $ this ->ruleSet ->getRulesAssoc ($ searchPattern );
223+ }
224+
225+ /**
226+ * @see RuleSet::removeRule()
227+ *
228+ * @param Rule|string|null $searchPattern
229+ */
230+ public function removeRule ($ searchPattern ): void
231+ {
232+ $ this ->ruleSet ->removeRule ($ searchPattern );
233+ }
234+
140235 /**
141236 * @throws OutputException
142237 */
@@ -155,10 +250,34 @@ public function render(OutputFormat $outputFormat): string
155250 );
156251 $ result .= $ outputFormat ->getContentAfterDeclarationBlockSelectors ();
157252 $ result .= $ formatter ->spaceBeforeOpeningBrace () . '{ ' ;
158- $ result .= $ this ->renderRules ($ outputFormat );
253+ $ result .= $ this ->ruleSet -> render ($ outputFormat );
159254 $ result .= '} ' ;
160255 $ result .= $ outputFormat ->getContentAfterDeclarationBlock ();
161256
162257 return $ result ;
163258 }
259+
260+ /**
261+ * @param list<Comment> $comments
262+ */
263+ public function addComments (array $ comments ): void
264+ {
265+ $ this ->comments = \array_merge ($ this ->comments , $ comments );
266+ }
267+
268+ /**
269+ * @return list<Comment>
270+ */
271+ public function getComments (): array
272+ {
273+ return $ this ->comments ;
274+ }
275+
276+ /**
277+ * @param list<Comment> $comments
278+ */
279+ public function setComments (array $ comments ): void
280+ {
281+ $ this ->comments = $ comments ;
282+ }
164283}
0 commit comments