You are here

class SassNode in Sassy 7

Same name and namespace in other branches
  1. 7.3 phpsass/tree/SassNode.php \SassNode

SassNode class. Base class for all Sass nodes. @package PHamlP @subpackage Sass.tree

Hierarchy

Expanded class hierarchy of SassNode

File

phamlp/sass/tree/SassNode.php, line 37

View source
class SassNode {

  /**
   * @var SassNode parent of this node
   */
  protected $parent;

  /**
   * @var SassNode root node
   */
  protected $root;

  /**
   * @var array children of this node
   */
  protected $children = array();

  /**
   * @var object source token
   */
  protected $token;

  /**
   * Constructor.
   * @param object source token
   * @return SassNode
   */
  public function __construct($token) {
    $this->token = $token;
  }

  /**
   * Getter.
   * @param string name of property to get
   * @return mixed return value of getter function
   */
  public function __get($name) {
    $getter = 'get' . ucfirst($name);
    if (method_exists($this, $getter)) {
      return $this
        ->{$getter}();
    }
    throw new SassNodeException('No getter function for {what}', array(
      '{what}' => $name,
    ), $this);
  }

  /**
   * Setter.
   * @param string name of property to set
   * @return mixed value of property
   * @return SassNode this node
   */
  public function __set($name, $value) {
    $setter = 'set' . ucfirst($name);
    if (method_exists($this, $setter)) {
      $this
        ->{$setter}($value);
      return $this;
    }
    throw new SassNodeException('No setter function for {what}', array(
      '{what}' => $name,
    ), $this);
  }

  /**
   * Resets children when cloned
   * @see parse
   */
  public function __clone() {
    $this->children = array();
  }

  /**
   * Return a value indicating if this node has a parent
   * @return array the node's parent
   */
  public function hasParent() {
    return !empty($this->parent);
  }

  /**
   * Returns the node's parent
   * @return array the node's parent
   */
  public function getParent() {
    return $this->parent;
  }

  /**
   * Adds a child to this node.
   * @return SassNode the child to add
   */
  public function addChild($child) {
    if ($child instanceof SassElseNode) {
      if (!$this->lastChild instanceof SassIfNode) {
        throw new SassException('@else(if) directive must come after @(else)if', array(), $child);
      }
      $this->lastChild
        ->addElse($child);
    }
    else {
      $this->children[] = $child;
      $child->parent = $this;
      $child->root = $this->root;
    }

    // The child will have children if a debug node has been added
    foreach ($child->children as $grandchild) {
      $grandchild->root = $this->root;
    }
  }

  /**
   * Returns a value indicating if this node has children
   * @return boolean true if the node has children, false if not
   */
  public function hasChildren() {
    return !empty($this->children);
  }

  /**
   * Returns the node's children
   * @return array the node's children
   */
  public function getChildren() {
    return $this->children;
  }

  /**
   * Returns a value indicating if this node is a child of the passed node.
   * This just checks the levels of the nodes. If this node is at a greater
   * level than the passed node if is a child of it.
   * @return boolean true if the node is a child of the passed node, false if not
   */
  public function isChildOf($node) {
    return $this->level > $node->level;
  }

  /**
   * Returns the last child node of this node.
   * @return SassNode the last child node of this node
   */
  public function getLastChild() {
    return $this->children[count($this->children) - 1];
  }

  /**
   * Returns the level of this node.
   * @return integer the level of this node
   */
  private function getLevel() {
    return $this->token->level;
  }

  /**
   * Returns the source for this node
   * @return string the source for this node
   */
  private function getSource() {
    return $this->token->source;
  }

  /**
   * Returns the debug_info option setting for this node
   * @return boolean the debug_info option setting for this node
   */
  private function getDebug_info() {
    return $this->parser->debug_info;
  }

  /**
   * Returns the line number for this node
   * @return string the line number for this node
   */
  private function getLine() {
    return $this->token->line;
  }

  /**
   * Returns the line_numbers option setting for this node
   * @return boolean the line_numbers option setting for this node
   */
  private function getLine_numbers() {
    return $this->parser->line_numbers;
  }

  /**
   * Returns vendor specific properties
   * @return array vendor specific properties
   */
  private function getVendor_properties() {
    return $this->parser->vendor_properties;
  }

  /**
   * Returns the filename for this node
   * @return string the filename for this node
   */
  private function getFilename() {
    return $this->token->filename;
  }

  /**
   * Returns the Sass parser.
   * @return SassParser the Sass parser
   */
  public function getParser() {
    return $this->root->parser;
  }

  /**
   * Returns the property syntax being used.
   * @return string the property syntax being used
   */
  public function getPropertySyntax() {
    return $this->root->parser->propertySyntax;
  }

  /**
   * Returns the SassScript parser.
   * @return SassScriptParser the SassScript parser
   */
  public function getScript() {
    return $this->root->script;
  }

  /**
   * Returns the renderer.
   * @return SassRenderer the renderer
   */
  public function getRenderer() {
    return $this->root->renderer;
  }

  /**
   * Returns the render style of the document tree.
   * @return string the render style of the document tree
   */
  public function getStyle() {
    return $this->root->parser->style;
  }

  /**
   * Returns a value indicating whether this node is in a directive
   * @param boolean true if the node is in a directive, false if not
   */
  public function inDirective() {
    return $this->parent instanceof SassDirectiveNode || $this->parent instanceof SassDirectiveNode;
  }

  /**
   * Returns a value indicating whether this node is in a SassScript directive
   * @param boolean true if this node is in a SassScript directive, false if not
   */
  public function inSassScriptDirective() {
    return $this->parent instanceof SassEachNode || $this->parent->parent instanceof SassEachNode || $this->parent instanceof SassForNode || $this->parent->parent instanceof SassForNode || $this->parent instanceof SassIfNode || $this->parent->parent instanceof SassIfNode || $this->parent instanceof SassWhileNode || $this->parent->parent instanceof SassWhileNode;
  }

  /**
   * Evaluates a SassScript expression.
   * @param string expression to evaluate
   * @param SassContext the context in which the expression is evaluated
   * @return SassLiteral value of parsed expression
   */
  protected function evaluate($expression, $context, $x = null) {
    $context->node = $this;
    return $this->script
      ->evaluate($expression, $context, $x);
  }

  /**
   * Replace interpolated SassScript contained in '#{}' with the parsed value.
   * @param string the text to interpolate
   * @param SassContext the context in which the string is interpolated
   * @return string the interpolated text
   */
  protected function interpolate($expression, $context) {
    $context->node = $this;
    return $this->script
      ->interpolate($expression, $context);
  }

  /**
   * Adds a warning to the node.
   * @param string warning message
   * @param array line
   */
  public function addWarning($message, $params = array()) {
    $warning = new SassDebugNode($this->token, $message, $params);
    $this
      ->addChild($warning);
  }

  /**
   * Parse the children of the node.
   * @param SassContext the context in which the children are parsed
   * @return array the parsed child nodes
   */
  protected function parseChildren($context) {
    $children = array();
    foreach ($this->children as $child) {
      $children = array_merge($children, $child
        ->parse($context));
    }

    // foreach
    return $children;
  }

  /**
   * Returns a value indicating if the token represents this type of node.
   * @param object token
   * @return boolean true if the token represents this type of node, false if not
   */
  public static function isa($token) {
    throw new SassNodeException('Child classes must override this method');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SassNode::$children protected property *
SassNode::$parent protected property *
SassNode::$root protected property *
SassNode::$token protected property *
SassNode::addChild public function * Adds a child to this node. *
SassNode::addWarning public function * Adds a warning to the node. *
SassNode::evaluate protected function * Evaluates a SassScript expression. *
SassNode::getChildren public function * Returns the node's children *
SassNode::getDebug_info private function * Returns the debug_info option setting for this node *
SassNode::getFilename private function * Returns the filename for this node *
SassNode::getLastChild public function * Returns the last child node of this node. *
SassNode::getLevel private function * Returns the level of this node. *
SassNode::getLine private function * Returns the line number for this node *
SassNode::getLine_numbers private function * Returns the line_numbers option setting for this node *
SassNode::getParent public function * Returns the node's parent *
SassNode::getParser public function * Returns the Sass parser. *
SassNode::getPropertySyntax public function * Returns the property syntax being used. *
SassNode::getRenderer public function * Returns the renderer. *
SassNode::getScript public function * Returns the SassScript parser. *
SassNode::getSource private function * Returns the source for this node *
SassNode::getStyle public function * Returns the render style of the document tree. *
SassNode::getVendor_properties private function * Returns vendor specific properties *
SassNode::hasChildren public function * Returns a value indicating if this node has children *
SassNode::hasParent public function * Return a value indicating if this node has a parent *
SassNode::inDirective public function * Returns a value indicating whether this node is in a directive *
SassNode::inSassScriptDirective public function * Returns a value indicating whether this node is in a SassScript directive *
SassNode::interpolate protected function * Replace interpolated SassScript contained in '#{}' with the parsed value. *
SassNode::isa public static function * Returns a value indicating if the token represents this type of node. * 7
SassNode::isChildOf public function * Returns a value indicating if this node is a child of the passed node. * This just checks the levels of the nodes. If this node is at a greater * level than the passed node if is a child of it. *
SassNode::parseChildren protected function * Parse the children of the node. *
SassNode::__clone public function * Resets children when cloned *
SassNode::__construct public function * Constructor. * 15
SassNode::__get public function * Getter. *
SassNode::__set public function * Setter. *