| 1 |
<?php |
| 2 |
/** |
| 3 |
* SCSSPHP |
| 4 |
* |
| 5 |
* @copyright 2012-2014 Leaf Corcoran |
| 6 |
* |
| 7 |
* @license http://opensource.org/licenses/gpl-license GPL-3.0 |
| 8 |
* @license http://opensource.org/licenses/MIT MIT |
| 9 |
* |
| 10 |
* @link http://leafo.net/scssphp |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Leafo\ScssPhp\Formatter; |
| 14 |
|
| 15 |
use Leafo\ScssPhp\Formatter; |
| 16 |
|
| 17 |
/** |
| 18 |
* SCSS nested formatter |
| 19 |
* |
| 20 |
* @author Leaf Corcoran <leafot@gmail.com> |
| 21 |
*/ |
| 22 |
class Nested extends Formatter |
| 23 |
{ |
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
$this->indentLevel = 0; |
| 27 |
$this->indentChar = ' '; |
| 28 |
$this->break = "\n"; |
| 29 |
$this->open = ' {'; |
| 30 |
$this->close = ' }'; |
| 31 |
$this->tagSeparator = ', '; |
| 32 |
$this->assignSeparator = ': '; |
| 33 |
} |
| 34 |
|
| 35 |
// adjust the depths of all children, depth first |
| 36 |
public function adjustAllChildren($block) |
| 37 |
{ |
| 38 |
// flatten empty nested blocks |
| 39 |
$children = array(); |
| 40 |
foreach ($block->children as $i => $child) { |
| 41 |
if (empty($child->lines) && empty($child->children)) { |
| 42 |
if (isset($block->children[$i + 1])) { |
| 43 |
$block->children[$i + 1]->depth = $child->depth; |
| 44 |
} |
| 45 |
continue; |
| 46 |
} |
| 47 |
$children[] = $child; |
| 48 |
} |
| 49 |
|
| 50 |
$count = count($children); |
| 51 |
for ($i = 0; $i < $count; $i++) { |
| 52 |
$depth = $children[$i]->depth; |
| 53 |
$j = $i + 1; |
| 54 |
if (isset($children[$j]) && $depth < $children[$j]->depth) { |
| 55 |
$childDepth = $children[$j]->depth; |
| 56 |
for (; $j < $count; $j++) { |
| 57 |
if ($depth < $children[$j]->depth && $childDepth >= $children[$j]->depth) { |
| 58 |
$children[$j]->depth = $depth + 1; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$block->children = $children; |
| 65 |
|
| 66 |
// make relative to parent |
| 67 |
foreach ($block->children as $child) { |
| 68 |
$this->adjustAllChildren($child); |
| 69 |
$child->depth = $child->depth - $block->depth; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
protected function blockLines($inner, $block) |
| 74 |
{ |
| 75 |
$glue = $this->break . $inner; |
| 76 |
|
| 77 |
foreach ($block->lines as $index => $line) { |
| 78 |
if (substr($line, 0, 2) === '/*') { |
| 79 |
$block->lines[$index] = preg_replace('/(\r|\n)+/', $glue, $line); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
echo $inner . implode($glue, $block->lines); |
| 84 |
|
| 85 |
if (!empty($block->children)) { |
| 86 |
echo $this->break; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
protected function block($block) |
| 91 |
{ |
| 92 |
if ($block->type == 'root') { |
| 93 |
$this->adjustAllChildren($block); |
| 94 |
} |
| 95 |
|
| 96 |
$inner = $pre = $this->indentStr($block->depth - 1); |
| 97 |
if (!empty($block->selectors)) { |
| 98 |
echo $pre . |
| 99 |
implode($this->tagSeparator, $block->selectors) . |
| 100 |
$this->open . $this->break; |
| 101 |
$this->indentLevel++; |
| 102 |
$inner = $this->indentStr($block->depth - 1); |
| 103 |
} |
| 104 |
|
| 105 |
if (!empty($block->lines)) { |
| 106 |
$this->blockLines($inner, $block); |
| 107 |
} |
| 108 |
|
| 109 |
foreach ($block->children as $i => $child) { |
| 110 |
// echo "*** block: ".$block->depth." child: ".$child->depth."\n"; |
| 111 |
$this->block($child); |
| 112 |
if ($i < count($block->children) - 1) { |
| 113 |
echo $this->break; |
| 114 |
|
| 115 |
if (isset($block->children[$i + 1])) { |
| 116 |
$next = $block->children[$i + 1]; |
| 117 |
if ($next->depth == max($block->depth, 1) && $child->depth >= $next->depth) { |
| 118 |
echo $this->break; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (!empty($block->selectors)) { |
| 125 |
$this->indentLevel--; |
| 126 |
echo $this->close; |
| 127 |
} |
| 128 |
|
| 129 |
if ($block->type == 'root') { |
| 130 |
echo $this->break; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|