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
+ * @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
+
32
60
/**
33
61
* @return DeclarationBlock|false
34
62
*
@@ -69,10 +97,20 @@ public static function parse(ParserState $parserState, ?CSSList $list = null)
69
97
}
70
98
}
71
99
$ result ->setComments ($ comments );
72
- RuleSet::parseRuleSet ($ parserState , $ result );
100
+
101
+ RuleSet::parseRuleSet ($ parserState , $ result ->ruleSet );
102
+
73
103
return $ result ;
74
104
}
75
105
106
+ /**
107
+ * @return int<0, max>
108
+ */
109
+ public function getLineNo (): int
110
+ {
111
+ return $ this ->lineNumber ;
112
+ }
113
+
76
114
/**
77
115
* @param array<Selector|string>|string $selectors
78
116
*
@@ -137,6 +175,63 @@ public function getSelectors(): array
137
175
return $ this ->selectors ;
138
176
}
139
177
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
+
140
235
/**
141
236
* @throws OutputException
142
237
*/
@@ -155,10 +250,34 @@ public function render(OutputFormat $outputFormat): string
155
250
);
156
251
$ result .= $ outputFormat ->getContentAfterDeclarationBlockSelectors ();
157
252
$ result .= $ formatter ->spaceBeforeOpeningBrace () . '{ ' ;
158
- $ result .= $ this ->renderRules ($ outputFormat );
253
+ $ result .= $ this ->ruleSet -> render ($ outputFormat );
159
254
$ result .= '} ' ;
160
255
$ result .= $ outputFormat ->getContentAfterDeclarationBlock ();
161
256
162
257
return $ result ;
163
258
}
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
+ }
164
283
}
0 commit comments