View source
<?php
class SassPropertyNode extends SassNode {
const MATCH_PROPERTY_NEW = '/^([^\\s=:"]+)\\s*(?:(= )|:)(.*?)$/';
const MATCH_PROPERTY_OLD = '/^:([^\\s=:]+)(?:\\s*(=)\\s*|\\s+|$)(.*)/';
const MATCH_PSUEDO_SELECTOR = '/^:?\\w[-\\w]+\\(?/i';
const MATCH_INTERPOLATION = '/^#\\{(.*?)\\}/i';
const NAME = 1;
const SCRIPT = 2;
const VALUE = 3;
const IS_SCRIPT = '= ';
private static $psuedoSelectors = array(
'root',
'nth-child(',
'nth-last-child(',
'nth-of-type(',
'nth-last-of-type(',
'first-child',
'last-child',
'first-of-type',
'last-of-type',
'only-child',
'only-of-type',
'empty',
'link',
'visited',
'active',
'hover',
'focus',
'target',
'lang(',
'enabled',
'disabled',
'checked',
':first-line',
':first-letter',
':before',
':after',
'first-line',
'first-letter',
'before',
'after',
);
private $name;
private $value;
public function __construct($token, $syntax = 'new') {
parent::__construct($token);
$matches = self::match($token, $syntax);
$this->name = $matches[self::NAME];
$this->value = $matches[self::VALUE];
if ($matches[self::SCRIPT] === self::IS_SCRIPT) {
$this
->addWarning('Setting CSS properties with "=" is deprecated; use "{name}: {value};"', array(
'{name}' => $this->name,
'{value}' => $this->value,
));
}
}
public function parse($context) {
$return = array();
if ($this->value !== "") {
$node = clone $this;
$node->name = ($this
->inNamespace() ? "{$this->namespace}-" : '') . $this
->interpolate($this->name, $context);
$node->value = $this
->evaluate($this
->interpolate($this->value, $context), $context, SassScriptParser::CSS_PROPERTY)
->toString();
if (array_key_exists($node->name, $this->vendor_properties)) {
foreach ($this->vendor_properties[$node->name] as $vendorProperty) {
$_node = clone $node;
$_node->name = $vendorProperty;
$return[] = $_node;
}
}
$return[] = $node;
}
if ($this->children) {
$return = array_merge($return, $this
->parseChildren($context));
}
return $return;
}
public function render() {
return $this->renderer
->renderProperty($this);
}
public function inNamespace() {
$parent = $this->parent;
do {
if ($parent instanceof SassPropertyNode) {
return true;
}
$parent = $parent->parent;
} while (is_object($parent));
return false;
}
protected function getNamespace() {
$namespace = array();
$parent = $this->parent;
do {
if ($parent instanceof SassPropertyNode) {
$namespace[] = $parent->name;
}
$parent = $parent->parent;
} while (is_object($parent));
return join('-', array_reverse($namespace));
}
public function getName() {
return $this->name;
}
public function getValue() {
return $this->value;
}
public static function isa($token) {
if (!is_array($token)) {
$syntax = 'old';
}
else {
$syntax = $token['syntax'];
$token = $token['token'];
}
$matches = self::match($token, $syntax);
if (!empty($matches)) {
if (isset($matches[self::VALUE]) && self::isPseudoSelector($matches[self::VALUE])) {
return false;
}
if ($token->level === 0) {
throw new SassPropertyNodeException('Properties can not be assigned at root level', array(), null);
}
else {
return true;
}
}
else {
return false;
}
}
public static function match($token, $syntax) {
switch ($syntax) {
case 'new':
preg_match(self::MATCH_PROPERTY_NEW, $token->source, $matches);
break;
case 'old':
preg_match(self::MATCH_PROPERTY_OLD, $token->source, $matches);
break;
default:
if (preg_match(self::MATCH_PROPERTY_NEW, $token->source, $matches) == 0) {
preg_match(self::MATCH_PROPERTY_OLD, $token->source, $matches);
}
break;
}
return $matches;
}
private static function isPseudoSelector($string) {
preg_match(self::MATCH_PSUEDO_SELECTOR, $string, $matches);
return isset($matches[0]) && in_array($matches[0], self::$psuedoSelectors) || preg_match(self::MATCH_INTERPOLATION, $string);
}
}