/** * Root SassNode constructor. * @param SassParser Sass parser * @return SassNode */ public function __construct($parser) { parent::__construct((object) array('source' => '', 'level' => -1, 'filename' => $parser->filename, 'line' => 0)); $this->parser = $parser; $this->script = new SassScriptParser(); $this->renderer = SassRenderer::getRenderer($parser->style); $this->root = $this; }
/** * Root SassNode constructor. * @param array options for the tree * @return SassNode */ public function __construct($options) { $this->root = $this; $this->options = $options; $this->parser = new SassScriptParser(); $this->renderer = SassRenderer::getRenderer($this->options['style']); $this->line = array('indentLevel' => -1); }
/** * Renders a property. * @param SassNode the node being rendered * @return string the rendered property */ public function renderProperty($node) { return $this->getIndent($node) . parent::renderProperty($node); }
/** * Renders a rule. * @param SassNode the node being rendered * @param array rule properties * @param string rendered rules * @return string the rendered directive */ public function renderRule($node, $properties, $rules) { return parent::renderRule($node, $properties, str_replace("\n\n", "\n", $rules)) . "\n"; }
/** * Render Sass code * * @param string Filename * @param string SassRenderer type * @return string */ public function render($file = null, $type = null) { if (!is_null($file)) { $this->setFile($file); } if (!is_null($type)) { $this->setRenderer($type); } $cache = new CommonCache($this->getTmp(), 'css', $this->getSource()); if (!$cache->isCached()) { // BEGIN: Flat to tree structure $lines = explode(self::TOKEN_LINE, $this->getSource()); $last = array(-1 => null); $attributes = array(); $attributeLevel = 0; $namespace = null; $namespaceLevel = 0; foreach ($lines as $line) { $stop = true; $cleaned = $this->cleanLine($line); $level = $this->countLevel($line); // Check for constant definition if (preg_match('/^' . preg_quote(self::TOKEN_CONSTANT) . '(.+?)[ ]?' . preg_quote(self::TOKEN_CONSTANT_VALUE) . '[ ]?["]?(.+)["]?/', $cleaned, $matches)) { $this->setConstant($matches[1], $matches[2]); } else { // Check for dynamic attribute definition if (preg_match('/^' . preg_quote(self::TOKEN_ATTRIBUTE) . '(.+?)[ ]?' . preg_quote(self::TOKEN_ATTRIBUTE_CALCULATE) . '[ ]?(.+)/', $cleaned, $matches)) { if ($namespaceLevel + 1 != $level) { $namespace = null; } $attributes[empty($namespace) ? $matches[1] : "{$namespace}-{$matches[1]}"] = $this->calculate($matches[2]); $attributeLevel = $level - (empty($namespace) ? 1 : 2); } else { // Check for attribute definition if (preg_match('/^' . preg_quote(self::TOKEN_ATTRIBUTE) . '(.+?) (.+)/', $cleaned, $matches)) { if ($namespaceLevel + 1 != $level) { $namespace = null; } $attributes[empty($namespace) ? $matches[1] : "{$namespace}-{$matches[1]}"] = $matches[2]; $attributeLevel = $level - (empty($namespace) ? 1 : 2); } else { // Check for attribute namespace if (preg_match('/^' . preg_quote(self::TOKEN_ATTRIBUTE_NAMESPACE) . '(.+)/', $cleaned, $matches)) { $namespace = $matches[1]; $namespaceLevel = $level; } else { // Check for comment if (preg_match('|^' . preg_quote(self::TOKEN_COMMENT) . '|', $cleaned)) { $stop = true; } else { $stop = false; $namespace = null; } } } } } // Remove blank lines if (empty($cleaned) || $stop) { continue; } // Assign attributes if (!empty($attributes) && $last[$attributeLevel] instanceof SassElement) { foreach ($attributes as $name => $value) { $last[$attributeLevel]->setAttribute($name, $value); } $attributeLevel = 0; $attributes = array(); } // Get parent element $parent = $last[$level - 1]; // Create new element $element = new SassElement($parent, $cleaned); // Mark as parent $last[$level] = $element; if ($level == 0) { $this->addElement($element); } } // Assign attributes if (!empty($attributes) && $last[$attributeLevel] instanceof SassElement) { foreach ($attributes as $name => $value) { $last[$attributeLevel]->setAttribute($name, $value); } $attributeLevel = 0; $attributes = array(); } // END: Flat to tree structure // Render to CSS return $cache->setCached(SassRenderer::getInstance($this->getElements(), $this->getRenderer())->render())->cacheIt()->getCached(); } else { return $cache->getCached(); } }
/** * Renders a property. * @param SassNode the node being rendered * @return string the rendered property */ public function renderProperty($node) { return self::INDENT . ($node->inDirective() ? self::INDENT : '') . parent::renderProperty($node); }