Debian Codegraph @

vendor/sabberworm/php-css-parser/src/OutputFormat.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS;
6
7 final class OutputFormat
8 {
9 /**
10 * @var '"'|"'"
11 */
12 private $stringQuotingType = '"';
13
14 /**
15 * Output RGB colors in hash notation if possible
16 *
17 * @var bool
18 */
19 private $usesRgbHashNotation = true;
20
21 /**
22 * Declaration format
23 *
24 * Semicolon after the last rule of a declaration block can be omitted. To do that, set this false.
25 *
26 * @var bool
27 */
28 private $renderSemicolonAfterLastRule = true;
29
30 /**
31 * Spacing
32 * Note that these strings are not sanity-checked: the value should only consist of whitespace
33 * Any newline character will be indented according to the current level.
34 * The triples (After, Before, Between) can be set using a wildcard
35 * (e.g. `$outputFormat->set('Space*Rules', "\n");`)
36 *
37 * @var string
38 */
39 private $spaceAfterRuleName = ' ';
40
41 /**
42 * @var string
43 */
44 private $spaceBeforeRules = '';
45
46 /**
47 * @var string
48 */
49 private $spaceAfterRules = '';
50
51 /**
52 * @var string
53 */
54 private $spaceBetweenRules = '';
55
56 /**
57 * @var string
58 */
59 private $spaceBeforeBlocks = '';
60
61 /**
62 * @var string
63 */
64 private $spaceAfterBlocks = '';
65
66 /**
67 * @var string
68 */
69 private $spaceBetweenBlocks = "\n";
70
71 /**
72 * Content injected in and around at-rule blocks.
73 *
74 * @var string
75 */
76 private $contentBeforeAtRuleBlock = '';
77
78 /**
79 * @var string
80 */
81 private $contentAfterAtRuleBlock = '';
82
83 /**
84 * This is what’s printed before and after the comma if a declaration block contains multiple selectors.
85 *
86 * @var string
87 */
88 private $spaceBeforeSelectorSeparator = '';
89
90 /**
91 * @var string
92 */
93 private $spaceAfterSelectorSeparator = ' ';
94
95 /**
96 * @var string
97 */
98 private $spaceAroundSelectorCombinator = ' ';
99
100 /**
101 * This is what’s inserted before the separator in value lists, by default.
102 *
103 * @var string
104 */
105 private $spaceBeforeListArgumentSeparator = '';
106
107 /**
108 * Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
109 *
110 * @var array<non-empty-string, string>
111 */
112 private $spaceBeforeListArgumentSeparators = [];
113
114 /**
115 * This is what’s inserted after the separator in value lists, by default.
116 *
117 * @var string
118 */
119 private $spaceAfterListArgumentSeparator = '';
120
121 /**
122 * Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
123 *
124 * @var array<non-empty-string, string>
125 */
126 private $spaceAfterListArgumentSeparators = [];
127
128 /**
129 * @var string
130 */
131 private $spaceBeforeOpeningBrace = ' ';
132
133 /**
134 * Content injected in and around declaration blocks.
135 *
136 * @var string
137 */
138 private $contentBeforeDeclarationBlock = '';
139
140 /**
141 * @var string
142 */
143 private $contentAfterDeclarationBlockSelectors = '';
144
145 /**
146 * @var string
147 */
148 private $contentAfterDeclarationBlock = '';
149
150 /**
151 * Indentation character(s) per level. Only applicable if newlines are used in any of the spacing settings.
152 *
153 * @var string
154 */
155 private $indentation = "\t";
156
157 /**
158 * Output exceptions.
159 *
160 * @var bool
161 */
162 private $shouldIgnoreExceptions = false;
163
164 /**
165 * Render comments for lists and RuleSets
166 *
167 * @var bool
168 */
169 private $shouldRenderComments = false;
170
171 /**
172 * @var OutputFormatter|null
173 */
174 private $outputFormatter;
175
176 /**
177 * @var OutputFormat|null
178 */
179 private $nextLevelFormat;
180
181 /**
182 * @var int<0, max>
183 */
184 private $indentationLevel = 0;
185
186 /**
187 * @return '"'|"'"
188 *
189 * @internal
190 */
191 public function getStringQuotingType(): string
192 {
193 return $this->stringQuotingType;
194 }
195
196 /**
197 * @param '"'|"'" $quotingType
198 *
199 * @return $this fluent interface
200 */
201 public function setStringQuotingType(string $quotingType): self
202 {
203 $this->stringQuotingType = $quotingType;
204
205 return $this;
206 }
207
208 /**
209 * @internal
210 */
211 public function usesRgbHashNotation(): bool
212 {
213 return $this->usesRgbHashNotation;
214 }
215
216 /**
217 * @return $this fluent interface
218 */
219 public function setRGBHashNotation(bool $usesRgbHashNotation): self
220 {
221 $this->usesRgbHashNotation = $usesRgbHashNotation;
222
223 return $this;
224 }
225
226 /**
227 * @internal
228 */
229 public function shouldRenderSemicolonAfterLastRule(): bool
230 {
231 return $this->renderSemicolonAfterLastRule;
232 }
233
234 /**
235 * @return $this fluent interface
236 */
237 public function setSemicolonAfterLastRule(bool $renderSemicolonAfterLastRule): self
238 {
239 $this->renderSemicolonAfterLastRule = $renderSemicolonAfterLastRule;
240
241 return $this;
242 }
243
244 /**
245 * @internal
246 */
247 public function getSpaceAfterRuleName(): string
248 {
249 return $this->spaceAfterRuleName;
250 }
251
252 /**
253 * @return $this fluent interface
254 */
255 public function setSpaceAfterRuleName(string $whitespace): self
256 {
257 $this->spaceAfterRuleName = $whitespace;
258
259 return $this;
260 }
261
262 /**
263 * @internal
264 */
265 public function getSpaceBeforeRules(): string
266 {
267 return $this->spaceBeforeRules;
268 }
269
270 /**
271 * @return $this fluent interface
272 */
273 public function setSpaceBeforeRules(string $whitespace): self
274 {
275 $this->spaceBeforeRules = $whitespace;
276
277 return $this;
278 }
279
280 /**
281 * @internal
282 */
283 public function getSpaceAfterRules(): string
284 {
285 return $this->spaceAfterRules;
286 }
287
288 /**
289 * @return $this fluent interface
290 */
291 public function setSpaceAfterRules(string $whitespace): self
292 {
293 $this->spaceAfterRules = $whitespace;
294
295 return $this;
296 }
297
298 /**
299 * @internal
300 */
301 public function getSpaceBetweenRules(): string
302 {
303 return $this->spaceBetweenRules;
304 }
305
306 /**
307 * @return $this fluent interface
308 */
309 public function setSpaceBetweenRules(string $whitespace): self
310 {
311 $this->spaceBetweenRules = $whitespace;
312
313 return $this;
314 }
315
316 /**
317 * @internal
318 */
319 public function getSpaceBeforeBlocks(): string
320 {
321 return $this->spaceBeforeBlocks;
322 }
323
324 /**
325 * @return $this fluent interface
326 */
327 public function setSpaceBeforeBlocks(string $whitespace): self
328 {
329 $this->spaceBeforeBlocks = $whitespace;
330
331 return $this;
332 }
333
334 /**
335 * @internal
336 */
337 public function getSpaceAfterBlocks(): string
338 {
339 return $this->spaceAfterBlocks;
340 }
341
342 /**
343 * @return $this fluent interface
344 */
345 public function setSpaceAfterBlocks(string $whitespace): self
346 {
347 $this->spaceAfterBlocks = $whitespace;
348
349 return $this;
350 }
351
352 /**
353 * @internal
354 */
355 public function getSpaceBetweenBlocks(): string
356 {
357 return $this->spaceBetweenBlocks;
358 }
359
360 /**
361 * @return $this fluent interface
362 */
363 public function setSpaceBetweenBlocks(string $whitespace): self
364 {
365 $this->spaceBetweenBlocks = $whitespace;
366
367 return $this;
368 }
369
370 /**
371 * @internal
372 */
373 public function getContentBeforeAtRuleBlock(): string
374 {
375 return $this->contentBeforeAtRuleBlock;
376 }
377
378 /**
379 * @return $this fluent interface
380 */
381 public function setBeforeAtRuleBlock(string $content): self
382 {
383 $this->contentBeforeAtRuleBlock = $content;
384
385 return $this;
386 }
387
388 /**
389 * @internal
390 */
391 public function getContentAfterAtRuleBlock(): string
392 {
393 return $this->contentAfterAtRuleBlock;
394 }
395
396 /**
397 * @return $this fluent interface
398 */
399 public function setAfterAtRuleBlock(string $content): self
400 {
401 $this->contentAfterAtRuleBlock = $content;
402
403 return $this;
404 }
405
406 /**
407 * @internal
408 */
409 public function getSpaceBeforeSelectorSeparator(): string
410 {
411 return $this->spaceBeforeSelectorSeparator;
412 }
413
414 /**
415 * @return $this fluent interface
416 */
417 public function setSpaceBeforeSelectorSeparator(string $whitespace): self
418 {
419 $this->spaceBeforeSelectorSeparator = $whitespace;
420
421 return $this;
422 }
423
424 /**
425 * @internal
426 */
427 public function getSpaceAfterSelectorSeparator(): string
428 {
429 return $this->spaceAfterSelectorSeparator;
430 }
431
432 /**
433 * @return $this fluent interface
434 */
435 public function setSpaceAfterSelectorSeparator(string $whitespace): self
436 {
437 $this->spaceAfterSelectorSeparator = $whitespace;
438
439 return $this;
440 }
441
442 /**
443 * @internal
444 */
445 public function getSpaceAroundSelectorCombinator(): string
446 {
447 return $this->spaceAroundSelectorCombinator;
448 }
449
450 /**
451 * The spacing set is also used for the descendent combinator, which is whitespace only,
452 * unless an empty string is set, in which case a space will be used.
453 *
454 * @return $this fluent interface
455 */
456 public function setSpaceAroundSelectorCombinator(string $whitespace): self
457 {
458 $this->spaceAroundSelectorCombinator = $whitespace;
459
460 return $this;
461 }
462
463 /**
464 * @internal
465 */
466 public function getSpaceBeforeListArgumentSeparator(): string
467 {
468 return $this->spaceBeforeListArgumentSeparator;
469 }
470
471 /**
472 * @return $this fluent interface
473 */
474 public function setSpaceBeforeListArgumentSeparator(string $whitespace): self
475 {
476 $this->spaceBeforeListArgumentSeparator = $whitespace;
477
478 return $this;
479 }
480
481 /**
482 * @return array<non-empty-string, string>
483 *
484 * @internal
485 */
486 public function getSpaceBeforeListArgumentSeparators(): array
487 {
488 return $this->spaceBeforeListArgumentSeparators;
489 }
490
491 /**
492 * @param array<non-empty-string, string> $separatorSpaces
493 *
494 * @return $this fluent interface
495 */
496 public function setSpaceBeforeListArgumentSeparators(array $separatorSpaces): self
497 {
498 $this->spaceBeforeListArgumentSeparators = $separatorSpaces;
499
500 return $this;
501 }
502
503 /**
504 * @internal
505 */
506 public function getSpaceAfterListArgumentSeparator(): string
507 {
508 return $this->spaceAfterListArgumentSeparator;
509 }
510
511 /**
512 * @return $this fluent interface
513 */
514 public function setSpaceAfterListArgumentSeparator(string $whitespace): self
515 {
516 $this->spaceAfterListArgumentSeparator = $whitespace;
517
518 return $this;
519 }
520
521 /**
522 * @return array<non-empty-string, string>
523 *
524 * @internal
525 */
526 public function getSpaceAfterListArgumentSeparators(): array
527 {
528 return $this->spaceAfterListArgumentSeparators;
529 }
530
531 /**
532 * @param array<non-empty-string, string> $separatorSpaces
533 *
534 * @return $this fluent interface
535 */
536 public function setSpaceAfterListArgumentSeparators(array $separatorSpaces): self
537 {
538 $this->spaceAfterListArgumentSeparators = $separatorSpaces;
539
540 return $this;
541 }
542
543 /**
544 * @internal
545 */
546 public function getSpaceBeforeOpeningBrace(): string
547 {
548 return $this->spaceBeforeOpeningBrace;
549 }
550
551 /**
552 * @return $this fluent interface
553 */
554 public function setSpaceBeforeOpeningBrace(string $whitespace): self
555 {
556 $this->spaceBeforeOpeningBrace = $whitespace;
557
558 return $this;
559 }
560
561 /**
562 * @internal
563 */
564 public function getContentBeforeDeclarationBlock(): string
565 {
566 return $this->contentBeforeDeclarationBlock;
567 }
568
569 /**
570 * @return $this fluent interface
571 */
572 public function setBeforeDeclarationBlock(string $content): self
573 {
574 $this->contentBeforeDeclarationBlock = $content;
575
576 return $this;
577 }
578
579 /**
580 * @internal
581 */
582 public function getContentAfterDeclarationBlockSelectors(): string
583 {
584 return $this->contentAfterDeclarationBlockSelectors;
585 }
586
587 /**
588 * @return $this fluent interface
589 */
590 public function setAfterDeclarationBlockSelectors(string $content): self
591 {
592 $this->contentAfterDeclarationBlockSelectors = $content;
593
594 return $this;
595 }
596
597 /**
598 * @internal
599 */
600 public function getContentAfterDeclarationBlock(): string
601 {
602 return $this->contentAfterDeclarationBlock;
603 }
604
605 /**
606 * @return $this fluent interface
607 */
608 public function setAfterDeclarationBlock(string $content): self
609 {
610 $this->contentAfterDeclarationBlock = $content;
611
612 return $this;
613 }
614
615 /**
616 * @internal
617 */
618 public function getIndentation(): string
619 {
620 return $this->indentation;
621 }
622
623 /**
624 * @return $this fluent interface
625 */
626 public function setIndentation(string $indentation): self
627 {
628 $this->indentation = $indentation;
629
630 return $this;
631 }
632
633 /**
634 * @internal
635 */
636 public function shouldIgnoreExceptions(): bool
637 {
638 return $this->shouldIgnoreExceptions;
639 }
640
641 /**
642 * @return $this fluent interface
643 */
644 public function setIgnoreExceptions(bool $ignoreExceptions): self
645 {
646 $this->shouldIgnoreExceptions = $ignoreExceptions;
647
648 return $this;
649 }
650
651 /**
652 * @internal
653 */
654 public function shouldRenderComments(): bool
655 {
656 return $this->shouldRenderComments;
657 }
658
659 /**
660 * @return $this fluent interface
661 */
662 public function setRenderComments(bool $renderComments): self
663 {
664 $this->shouldRenderComments = $renderComments;
665
666 return $this;
667 }
668
669 /**
670 * @return int<0, max>
671 *
672 * @internal
673 */
674 public function getIndentationLevel(): int
675 {
676 return $this->indentationLevel;
677 }
678
679 /**
680 * @param int<1, max> $numberOfTabs
681 *
682 * @return $this fluent interface
683 */
684 public function indentWithTabs(int $numberOfTabs = 1): self
685 {
686 return $this->setIndentation(\str_repeat("\t", $numberOfTabs));
687 }
688
689 /**
690 * @param int<1, max> $numberOfSpaces
691 *
692 * @return $this fluent interface
693 */
694 public function indentWithSpaces(int $numberOfSpaces = 2): self
695 {
696 return $this->setIndentation(\str_repeat(' ', $numberOfSpaces));
697 }
698
699 /**
700 * @internal since V8.8.0
701 */
702 public function nextLevel(): self
703 {
704 if ($this->nextLevelFormat === null) {
705 $this->nextLevelFormat = clone $this;
706 $this->nextLevelFormat->indentationLevel++;
707 $this->nextLevelFormat->outputFormatter = null;
708 }
709 return $this->nextLevelFormat;
710 }
711
712 public function beLenient(): void
713 {
714 $this->shouldIgnoreExceptions = true;
715 }
716
717 /**
718 * @internal since 8.8.0
719 */
720 public function getFormatter(): OutputFormatter
721 {
722 if ($this->outputFormatter === null) {
723 $this->outputFormatter = new OutputFormatter($this);
724 }
725
726 return $this->outputFormatter;
727 }
728
729 /**
730 * Creates an instance of this class without any particular formatting settings.
731 */
732 public static function create(): self
733 {
734 return new OutputFormat();
735 }
736
737 /**
738 * Creates an instance of this class with a preset for compact formatting.
739 */
740 public static function createCompact(): self
741 {
742 $format = self::create();
743 $format
744 ->setSpaceBeforeRules('')
745 ->setSpaceBetweenRules('')
746 ->setSpaceAfterRules('')
747 ->setSpaceBeforeBlocks('')
748 ->setSpaceBetweenBlocks('')
749 ->setSpaceAfterBlocks('')
750 ->setSpaceAfterRuleName('')
751 ->setSpaceBeforeOpeningBrace('')
752 ->setSpaceAfterSelectorSeparator('')
753 ->setSpaceAroundSelectorCombinator('')
754 ->setSemicolonAfterLastRule(false)
755 ->setRenderComments(false);
756
757 return $format;
758 }
759
760 /**
761 * Creates an instance of this class with a preset for pretty formatting.
762 */
763 public static function createPretty(): self
764 {
765 $format = self::create();
766 $format
767 ->setSpaceBeforeRules("\n")
768 ->setSpaceBetweenRules("\n")
769 ->setSpaceAfterRules("\n")
770 ->setSpaceBeforeBlocks("\n")
771 ->setSpaceBetweenBlocks("\n\n")
772 ->setSpaceAfterBlocks("\n")
773 ->setSpaceAfterListArgumentSeparators([',' => ' '])
774 ->setRenderComments(true);
775
776 return $format;
777 }
778 }