4
4
5
5
namespace Sabberworm \CSS \RuleSet ;
6
6
7
+ use Sabberworm \CSS \Comment \Comment ;
8
+ use Sabberworm \CSS \Comment \Commentable ;
7
9
use Sabberworm \CSS \CSSList \CSSList ;
8
10
use Sabberworm \CSS \CSSList \KeyFrame ;
9
11
use Sabberworm \CSS \OutputFormat ;
13
15
use Sabberworm \CSS \Parsing \UnexpectedTokenException ;
14
16
use Sabberworm \CSS \Property \KeyframeSelector ;
15
17
use Sabberworm \CSS \Property \Selector ;
18
+ use Sabberworm \CSS \Renderable ;
19
+ use Sabberworm \CSS \Rule \Rule ;
16
20
17
21
/**
18
- * This class represents a `RuleSet` constrained by a `Selector`.
22
+ * This class includes a `RuleSet` constrained by a `Selector`.
19
23
*
20
24
* It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
21
25
* matching elements.
22
26
*
23
27
* Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
24
28
*/
25
- class DeclarationBlock extends RuleSet
29
+ class DeclarationBlock implements Commentable, Renderable
26
30
{
27
31
/**
28
32
* @var array<Selector|string>
29
33
*/
30
34
private $ selectors = [];
31
35
36
+ /**
37
+ * @var RuleSet
38
+ */
39
+ private $ ruleSet ;
40
+
41
+ /**
42
+ * @var int<0, max>
43
+ */
44
+ private $ lineNumber ;
45
+
46
+ /**
47
+ * @param int<0, max> $lineNumber
48
+ */
49
+ public function __construct (int $ lineNumber = 0 )
50
+ {
51
+ $ this ->lineNumber = $ lineNumber ;
52
+ $ this ->ruleSet = new RuleSet ($ lineNumber );
53
+ }
54
+
32
55
/**
33
56
* @throws UnexpectedTokenException
34
57
* @throws UnexpectedEOFException
@@ -67,10 +90,20 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
67
90
}
68
91
}
69
92
$ result ->setComments ($ comments );
70
- RuleSet::parseRuleSet ($ parserState , $ result );
93
+
94
+ RuleSet::parseRuleSet ($ parserState , $ result ->ruleSet );
95
+
71
96
return $ result ;
72
97
}
73
98
99
+ /**
100
+ * @return int<0, max>
101
+ */
102
+ public function getLineNo (): int
103
+ {
104
+ return $ this ->lineNumber ;
105
+ }
106
+
74
107
/**
75
108
* @param array<Selector|string>|string $selectors
76
109
*
@@ -135,6 +168,63 @@ public function getSelectors(): array
135
168
return $ this ->selectors ;
136
169
}
137
170
171
+ public function getRuleSet (): RuleSet
172
+ {
173
+ return $ this ->ruleSet ;
174
+ }
175
+
176
+ /**
177
+ * @see RuleSet::addRule()
178
+ */
179
+ public function addRule (Rule $ ruleToAdd , ?Rule $ sibling = null ): void
180
+ {
181
+ $ this ->ruleSet ->addRule ($ ruleToAdd , $ sibling );
182
+ }
183
+
184
+ /**
185
+ * @see RuleSet::getRules()
186
+ *
187
+ * @param Rule|string|null $searchPattern
188
+ *
189
+ * @return array<int<0, max>, Rule>
190
+ */
191
+ public function getRules ($ searchPattern = null ): array
192
+ {
193
+ return $ this ->ruleSet ->getRules ($ searchPattern );
194
+ }
195
+
196
+ /**
197
+ * @see RuleSet::setRules()
198
+ *
199
+ * @param array<Rule> $rules
200
+ */
201
+ public function setRules (array $ rules ): void
202
+ {
203
+ $ this ->ruleSet ->setRules ($ rules );
204
+ }
205
+
206
+ /**
207
+ * @see RuleSet::getRulesAssoc()
208
+ *
209
+ * @param Rule|string|null $searchPattern
210
+ *
211
+ * @return array<string, Rule>
212
+ */
213
+ public function getRulesAssoc ($ searchPattern = null ): array
214
+ {
215
+ return $ this ->ruleSet ->getRulesAssoc ($ searchPattern );
216
+ }
217
+
218
+ /**
219
+ * @see RuleSet::removeRule()
220
+ *
221
+ * @param Rule|string|null $searchPattern
222
+ */
223
+ public function removeRule ($ searchPattern ): void
224
+ {
225
+ $ this ->ruleSet ->removeRule ($ searchPattern );
226
+ }
227
+
138
228
/**
139
229
* @return non-empty-string
140
230
*
@@ -155,10 +245,34 @@ public function render(OutputFormat $outputFormat): string
155
245
);
156
246
$ result .= $ outputFormat ->getContentAfterDeclarationBlockSelectors ();
157
247
$ result .= $ formatter ->spaceBeforeOpeningBrace () . '{ ' ;
158
- $ result .= $ this ->renderRules ($ outputFormat );
248
+ $ result .= $ this ->ruleSet -> render ($ outputFormat );
159
249
$ result .= '} ' ;
160
250
$ result .= $ outputFormat ->getContentAfterDeclarationBlock ();
161
251
162
252
return $ result ;
163
253
}
254
+
255
+ /**
256
+ * @param list<Comment> $comments
257
+ */
258
+ public function addComments (array $ comments ): void
259
+ {
260
+ $ this ->comments = \array_merge ($ this ->comments , $ comments );
261
+ }
262
+
263
+ /**
264
+ * @return list<Comment>
265
+ */
266
+ public function getComments (): array
267
+ {
268
+ return $ this ->comments ;
269
+ }
270
+
271
+ /**
272
+ * @param list<Comment> $comments
273
+ */
274
+ public function setComments (array $ comments ): void
275
+ {
276
+ $ this ->comments = $ comments ;
277
+ }
164
278
}
0 commit comments