aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/scssphp/scssphp/src/Formatter/Nested.php
blob: d5ed85cc2615f1aaad4a75645464618c2adbc3be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php

/**
 * SCSSPHP
 *
 * @copyright 2012-2020 Leaf Corcoran
 *
 * @license http://opensource.org/licenses/MIT MIT
 *
 * @link http://scssphp.github.io/scssphp
 */

namespace ScssPhp\ScssPhp\Formatter;

use ScssPhp\ScssPhp\Formatter;
use ScssPhp\ScssPhp\Type;

/**
 * Nested formatter
 *
 * @author Leaf Corcoran <leafot@gmail.com>
 *
 * @deprecated since 1.4.0. Use the Expanded formatter instead.
 *
 * @internal
 */
class Nested extends Formatter
{
    /**
     * @var int
     */
    private $depth;

    /**
     * {@inheritdoc}
     */
    public function __construct()
    {
        @trigger_error('The Nested formatter is deprecated since 1.4.0. Use the Expanded formatter instead.', E_USER_DEPRECATED);

        $this->indentLevel = 0;
        $this->indentChar = '  ';
        $this->break = "\n";
        $this->open = ' {';
        $this->close = ' }';
        $this->tagSeparator = ', ';
        $this->assignSeparator = ': ';
        $this->keepSemicolons = true;
    }

    /**
     * {@inheritdoc}
     */
    protected function indentStr()
    {
        $n = $this->depth - 1;

        return str_repeat($this->indentChar, max($this->indentLevel + $n, 0));
    }

    /**
     * {@inheritdoc}
     */
    protected function blockLines(OutputBlock $block)
    {
        $inner = $this->indentStr();
        $glue  = $this->break . $inner;

        foreach ($block->lines as $index => $line) {
            if (substr($line, 0, 2) === '/*') {
                $replacedLine = preg_replace('/\r\n?|\n|\f/', $this->break, $line);
                assert($replacedLine !== null);
                $block->lines[$index] = $replacedLine;
            }
        }

        $this->write($inner . implode($glue, $block->lines));
    }

    /**
     * {@inheritdoc}
     */
    protected function block(OutputBlock $block)
    {
        static $depths;
        static $downLevel;
        static $closeBlock;
        static $previousEmpty;
        static $previousHasSelector;

        if ($block->type === 'root') {
            $depths = [ 0 ];
            $downLevel = '';
            $closeBlock = '';
            $this->depth = 0;
            $previousEmpty = false;
            $previousHasSelector = false;
        }

        $isMediaOrDirective = \in_array($block->type, [Type::T_DIRECTIVE, Type::T_MEDIA]);
        $isSupport = ($block->type === Type::T_DIRECTIVE
            && $block->selectors && strpos(implode('', $block->selectors), '@supports') !== false);

        while ($block->depth < end($depths) || ($block->depth == 1 && end($depths) == 1)) {
            array_pop($depths);
            $this->depth--;

            if (
                ! $this->depth && ($block->depth <= 1 || (! $this->indentLevel && $block->type === Type::T_COMMENT)) &&
                (($block->selectors && ! $isMediaOrDirective) || $previousHasSelector)
            ) {
                $downLevel = $this->break;
            }

            if (empty($block->lines) && empty($block->children)) {
                $previousEmpty = true;
            }
        }

        if (empty($block->lines) && empty($block->children)) {
            return;
        }

        $this->currentBlock = $block;

        if (! empty($block->lines) || (! empty($block->children) && ($this->depth < 1 || $isSupport))) {
            if ($block->depth > end($depths)) {
                if (! $previousEmpty || $this->depth < 1) {
                    $this->depth++;

                    $depths[] = $block->depth;
                } else {
                    // keep the current depth unchanged but take the block depth as a new reference for following blocks
                    array_pop($depths);

                    $depths[] = $block->depth;
                }
            }
        }

        $previousEmpty = ($block->type === Type::T_COMMENT);
        $previousHasSelector = false;

        if (! empty($block->selectors)) {
            if ($closeBlock) {
                $this->write($closeBlock);
                $closeBlock = '';
            }

            if ($downLevel) {
                $this->write($downLevel);
                $downLevel = '';
            }

            $this->blockSelectors($block);

            $this->indentLevel++;
        }

        if (! empty($block->lines)) {
            if ($closeBlock) {
                $this->write($closeBlock);
                $closeBlock = '';
            }

            if ($downLevel) {
                $this->write($downLevel);
                $downLevel = '';
            }

            $this->blockLines($block);

            $closeBlock = $this->break;
        }

        if (! empty($block->children)) {
            if ($this->depth > 0 && ($isMediaOrDirective || ! $this->hasFlatChild($block))) {
                array_pop($depths);

                $this->depth--;
                $this->blockChildren($block);
                $this->depth++;

                $depths[] = $block->depth;
            } else {
                $this->blockChildren($block);
            }
        }

        // reclear to not be spoiled by children if T_DIRECTIVE
        if ($block->type === Type::T_DIRECTIVE) {
            $previousHasSelector = false;
        }

        if (! empty($block->selectors)) {
            $this->indentLevel--;

            if (! $this->keepSemicolons) {
                $this->strippedSemicolon = '';
            }

            $this->write($this->close);

            $closeBlock = $this->break;

            if ($this->depth > 1 && ! empty($block->children)) {
                array_pop($depths);
                $this->depth--;
            }

            if (! $isMediaOrDirective) {
                $previousHasSelector = true;
            }
        }

        if ($block->type === 'root') {
            $this->write($this->break);
        }
    }

    /**
     * Block has flat child
     *
     * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block
     *
     * @return bool
     */
    private function hasFlatChild($block)
    {
        foreach ($block->children as $child) {
            if (empty($child->selectors)) {
                return true;
            }
        }

        return false;
    }
}