PluginProbe
MaxButtons – Create buttons / 7.1.1
MaxButtons – Create buttons v7.1.1
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / assets / libraries / scssphp / src / Formatter / Nested.php

Nested.php in MaxButtons – Create buttons 7.1.1, at assets/libraries/scssphp/src/Formatter/Nested.php

198 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SCSSPHP
4 *
5 * @copyright 2012-2015 Leaf Corcoran
6 *
7 * @license http://opensource.org/licenses/MIT MIT
8 *
9 * @link http://leafo.github.io/scssphp
10 */
11
12 namespace Leafo\ScssPhp\Formatter;
13
14 use Leafo\ScssPhp\Formatter;
15 use Leafo\ScssPhp\Formatter\OutputBlock;
16
17 /**
18 * SCSS nested formatter
19 *
20 * @author Leaf Corcoran <leafot@gmail.com>
21 */
22 class Nested extends Formatter
23 {
24 /**
25 * @var integer
26 */
27 private $depth;
28
29 /**
30 * {@inheritdoc}
31 */
32 public function __construct()
33 {
34 $this->indentLevel = 0;
35 $this->indentChar = ' ';
36 $this->break = "\n";
37 $this->open = ' {';
38 $this->close = ' }';
39 $this->tagSeparator = ', ';
40 $this->assignSeparator = ': ';
41 }
42
43 /**
44 * {@inheritdoc}
45 */
46 protected function indentStr()
47 {
48 $n = $this->depth - 1;
49
50 return str_repeat($this->indentChar, max($this->indentLevel + $n, 0));
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 protected function blockLines(OutputBlock $block)
57 {
58 $inner = $this->indentStr();
59
60 $glue = $this->break . $inner;
61
62 foreach ($block->lines as $index => $line) {
63 if (substr($line, 0, 2) === '/*') {
64 $block->lines[$index] = preg_replace('/(\r|\n)+/', $glue, $line);
65 }
66 }
67
68 echo $inner . implode($glue, $block->lines);
69
70 if (! empty($block->children)) {
71 echo $this->break;
72 }
73 }
74
75 /**
76 * {@inheritdoc}
77 */
78 protected function blockSelectors(OutputBlock $block)
79 {
80 $inner = $this->indentStr();
81
82 echo $inner
83 . implode($this->tagSeparator, $block->selectors)
84 . $this->open . $this->break;
85 }
86
87 /**
88 * {@inheritdoc}
89 */
90 protected function blockChildren(OutputBlock $block)
91 {
92 foreach ($block->children as $i => $child) {
93 $this->block($child);
94
95 if ($i < count($block->children) - 1) {
96 echo $this->break;
97
98 if (isset($block->children[$i + 1])) {
99 $next = $block->children[$i + 1];
100
101 if ($next->depth === max($block->depth, 1) && $child->depth >= $next->depth) {
102 echo $this->break;
103 }
104 }
105 }
106 }
107 }
108
109 /**
110 * {@inheritdoc}
111 */
112 protected function block(OutputBlock $block)
113 {
114 if ($block->type === 'root') {
115 $this->adjustAllChildren($block);
116 }
117
118 if (empty($block->lines) && empty($block->children)) {
119 return;
120 }
121
122 $this->depth = $block->depth;
123
124 if (! empty($block->selectors)) {
125 $this->blockSelectors($block);
126
127 $this->indentLevel++;
128 }
129
130 if (! empty($block->lines)) {
131 $this->blockLines($block);
132 }
133
134 if (! empty($block->children)) {
135 $this->blockChildren($block);
136 }
137
138 if (! empty($block->selectors)) {
139 $this->indentLevel--;
140
141 echo $this->close;
142 }
143
144 if ($block->type === 'root') {
145 echo $this->break;
146 }
147 }
148
149 /**
150 * Adjust the depths of all children, depth first
151 *
152 * @param \Leafo\ScssPhp\Formatter\OutputBlock $block
153 */
154 private function adjustAllChildren(OutputBlock $block)
155 {
156 // flatten empty nested blocks
157 $children = array();
158
159 foreach ($block->children as $i => $child) {
160 if (empty($child->lines) && empty($child->children)) {
161 if (isset($block->children[$i + 1])) {
162 $block->children[$i + 1]->depth = $child->depth;
163 }
164
165 continue;
166 }
167
168 $children[] = $child;
169 }
170
171 $count = count($children);
172
173 for ($i = 0; $i < $count; $i++) {
174 $depth = $children[$i]->depth;
175 $j = $i + 1;
176
177 if (isset($children[$j]) && $depth < $children[$j]->depth) {
178 $childDepth = $children[$j]->depth;
179
180 for (; $j < $count; $j++) {
181 if ($depth < $children[$j]->depth && $childDepth >= $children[$j]->depth) {
182 $children[$j]->depth = $depth + 1;
183 }
184 }
185 }
186 }
187
188 $block->children = $children;
189
190 // make relative to parent
191 foreach ($block->children as $child) {
192 $this->adjustAllChildren($child);
193
194 $child->depth = $child->depth - $block->depth;
195 }
196 }
197 }
198