CSSList
8 months ago
Comment
3 years ago
Parsing
2 years ago
Property
2 years ago
Rule
2 years ago
RuleSet
8 months ago
Value
2 years ago
OutputFormat.php
2 years ago
OutputFormatter.php
2 years ago
Parser.php
8 months ago
Renderable.php
4 years ago
Settings.php
4 years ago
index.php
3 years ago
OutputFormatter.php
149 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Sabberworm\CSS\Comment\Commentable; |
| 5 | use MailPoetVendor\Sabberworm\CSS\Parsing\OutputException; |
| 6 | class OutputFormatter |
| 7 | { |
| 8 | private $oFormat; |
| 9 | public function __construct(OutputFormat $oFormat) |
| 10 | { |
| 11 | $this->oFormat = $oFormat; |
| 12 | } |
| 13 | public function space($sName, $sType = null) |
| 14 | { |
| 15 | $sSpaceString = $this->oFormat->get("Space{$sName}"); |
| 16 | // If $sSpaceString is an array, we have multiple values configured |
| 17 | // depending on the type of object the space applies to |
| 18 | if (\is_array($sSpaceString)) { |
| 19 | if ($sType !== null && isset($sSpaceString[$sType])) { |
| 20 | $sSpaceString = $sSpaceString[$sType]; |
| 21 | } else { |
| 22 | $sSpaceString = \reset($sSpaceString); |
| 23 | } |
| 24 | } |
| 25 | return $this->prepareSpace($sSpaceString); |
| 26 | } |
| 27 | public function spaceAfterRuleName() |
| 28 | { |
| 29 | return $this->space('AfterRuleName'); |
| 30 | } |
| 31 | public function spaceBeforeRules() |
| 32 | { |
| 33 | return $this->space('BeforeRules'); |
| 34 | } |
| 35 | public function spaceAfterRules() |
| 36 | { |
| 37 | return $this->space('AfterRules'); |
| 38 | } |
| 39 | public function spaceBetweenRules() |
| 40 | { |
| 41 | return $this->space('BetweenRules'); |
| 42 | } |
| 43 | public function spaceBeforeBlocks() |
| 44 | { |
| 45 | return $this->space('BeforeBlocks'); |
| 46 | } |
| 47 | public function spaceAfterBlocks() |
| 48 | { |
| 49 | return $this->space('AfterBlocks'); |
| 50 | } |
| 51 | public function spaceBetweenBlocks() |
| 52 | { |
| 53 | return $this->space('BetweenBlocks'); |
| 54 | } |
| 55 | public function spaceBeforeSelectorSeparator() |
| 56 | { |
| 57 | return $this->space('BeforeSelectorSeparator'); |
| 58 | } |
| 59 | public function spaceAfterSelectorSeparator() |
| 60 | { |
| 61 | return $this->space('AfterSelectorSeparator'); |
| 62 | } |
| 63 | public function spaceBeforeListArgumentSeparator($sSeparator) |
| 64 | { |
| 65 | return $this->space('BeforeListArgumentSeparator', $sSeparator); |
| 66 | } |
| 67 | public function spaceAfterListArgumentSeparator($sSeparator) |
| 68 | { |
| 69 | return $this->space('AfterListArgumentSeparator', $sSeparator); |
| 70 | } |
| 71 | public function spaceBeforeOpeningBrace() |
| 72 | { |
| 73 | return $this->space('BeforeOpeningBrace'); |
| 74 | } |
| 75 | public function safely($cCode) |
| 76 | { |
| 77 | if ($this->oFormat->get('IgnoreExceptions')) { |
| 78 | // If output exceptions are ignored, run the code with exception guards |
| 79 | try { |
| 80 | return $cCode(); |
| 81 | } catch (OutputException $e) { |
| 82 | return null; |
| 83 | } |
| 84 | // Do nothing |
| 85 | } else { |
| 86 | // Run the code as-is |
| 87 | return $cCode(); |
| 88 | } |
| 89 | } |
| 90 | public function implode($sSeparator, array $aValues, $bIncreaseLevel = \false) |
| 91 | { |
| 92 | $sResult = ''; |
| 93 | $oFormat = $this->oFormat; |
| 94 | if ($bIncreaseLevel) { |
| 95 | $oFormat = $oFormat->nextLevel(); |
| 96 | } |
| 97 | $bIsFirst = \true; |
| 98 | foreach ($aValues as $mValue) { |
| 99 | if ($bIsFirst) { |
| 100 | $bIsFirst = \false; |
| 101 | } else { |
| 102 | $sResult .= $sSeparator; |
| 103 | } |
| 104 | if ($mValue instanceof Renderable) { |
| 105 | $sResult .= $mValue->render($oFormat); |
| 106 | } else { |
| 107 | $sResult .= $mValue; |
| 108 | } |
| 109 | } |
| 110 | return $sResult; |
| 111 | } |
| 112 | public function removeLastSemicolon($sString) |
| 113 | { |
| 114 | if ($this->oFormat->get('SemicolonAfterLastRule')) { |
| 115 | return $sString; |
| 116 | } |
| 117 | $sString = \explode(';', $sString); |
| 118 | if (\count($sString) < 2) { |
| 119 | return $sString[0]; |
| 120 | } |
| 121 | $sLast = \array_pop($sString); |
| 122 | $sNextToLast = \array_pop($sString); |
| 123 | \array_push($sString, $sNextToLast . $sLast); |
| 124 | return \implode(';', $sString); |
| 125 | } |
| 126 | public function comments(Commentable $oCommentable) |
| 127 | { |
| 128 | if (!$this->oFormat->bRenderComments) { |
| 129 | return ''; |
| 130 | } |
| 131 | $sResult = ''; |
| 132 | $aComments = $oCommentable->getComments(); |
| 133 | $iLastCommentIndex = \count($aComments) - 1; |
| 134 | foreach ($aComments as $i => $oComment) { |
| 135 | $sResult .= $oComment->render($this->oFormat); |
| 136 | $sResult .= $i === $iLastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks(); |
| 137 | } |
| 138 | return $sResult; |
| 139 | } |
| 140 | private function prepareSpace($sSpaceString) |
| 141 | { |
| 142 | return \str_replace("\n", "\n" . $this->indent(), $sSpaceString); |
| 143 | } |
| 144 | private function indent() |
| 145 | { |
| 146 | return \str_repeat($this->oFormat->sIndentation, $this->oFormat->level()); |
| 147 | } |
| 148 | } |
| 149 |