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