PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/_plugins_/sasspip/sassphp/tree/SassNode.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 349 lines | 159 code | 32 blank | 158 comment | 7 complexity | 71feb5ac56d892ad7f98beebf335bce4 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * SassNode class file.
  5. * @author Chris Yates <chris.l.yates@gmail.com>
  6. * @copyright Copyright (c) 2010 PBM Web Development
  7. * @license http://phamlp.googlecode.com/files/license.txt
  8. * @package PHamlP
  9. * @subpackage Sass.tree
  10. */
  11. require_once('SassContext.php');
  12. require_once('SassCommentNode.php');
  13. require_once('SassDebugNode.php');
  14. require_once('SassDirectiveNode.php');
  15. require_once('SassImportNode.php');
  16. require_once('SassMixinNode.php');
  17. require_once('SassMixinDefinitionNode.php');
  18. require_once('SassPropertyNode.php');
  19. require_once('SassRootNode.php');
  20. require_once('SassRuleNode.php');
  21. require_once('SassVariableNode.php');
  22. require_once('SassExtendNode.php');
  23. require_once('SassEachNode.php');
  24. require_once('SassForNode.php');
  25. require_once('SassIfNode.php');
  26. require_once('SassElseNode.php');
  27. require_once('SassWhileNode.php');
  28. require_once('SassNodeExceptions.php');
  29. require_once('SassFunctionDefinitionNode.php');
  30. require_once('SassReturnNode.php');
  31. require_once('SassContentNode.php');
  32. require_once('SassWarnNode.php');
  33. require_once('SassMediaNode.php');
  34. /**
  35. * SassNode class.
  36. * Base class for all Sass nodes.
  37. * @package PHamlP
  38. * @subpackage Sass.tree
  39. */
  40. class SassNode {
  41. /**
  42. * @var SassNode parent of this node
  43. */
  44. public $parent;
  45. /**
  46. * @var SassNode root node
  47. */
  48. public $root;
  49. /**
  50. * @var array children of this node
  51. */
  52. public $children = array();
  53. /**
  54. * @var object source token
  55. */
  56. public $token;
  57. /**
  58. * Constructor.
  59. * @param object source token
  60. * @return SassNode
  61. */
  62. public function __construct($token) {
  63. $this->token = $token;
  64. }
  65. /**
  66. * Getter.
  67. * @param string name of property to get
  68. * @return mixed return value of getter function
  69. */
  70. public function __get($name) {
  71. $getter = 'get' . ucfirst($name);
  72. if (method_exists($this, $getter)) {
  73. return $this->$getter();
  74. }
  75. throw new SassNodeException('No getter function for ' . $name, $this);
  76. }
  77. /**
  78. * Setter.
  79. * @param string name of property to set
  80. * @return mixed value of property
  81. * @return SassNode this node
  82. */
  83. public function __set($name, $value) {
  84. $setter = 'set' . ucfirst($name);
  85. if (method_exists($this, $setter)) {
  86. $this->$setter($value);
  87. return $this;
  88. }
  89. throw new SassNodeException('No setter function for ' . $name, $this);
  90. }
  91. /**
  92. * Resets children when cloned
  93. * @see parse
  94. */
  95. public function __clone() {
  96. $this->children = array();
  97. }
  98. /**
  99. * Return a value indicating if this node has a parent
  100. * @return array the node's parent
  101. */
  102. public function hasParent() {
  103. return !empty($this->parent);
  104. }
  105. /**
  106. * Returns the node's parent
  107. * @return array the node's parent
  108. */
  109. public function getParent() {
  110. return $this->parent;
  111. }
  112. /**
  113. * Adds a child to this node.
  114. * @return SassNode the child to add
  115. */
  116. public function addChild($child) {
  117. if ($child instanceof SassElseNode) {
  118. if (!$this->lastChild instanceof SassIfNode) {
  119. throw new SassException('@else(if) directive must come after @(else)if', $child);
  120. }
  121. $this->lastChild->addElse($child);
  122. }
  123. else {
  124. $this->children[] = $child;
  125. $child->parent = $this;
  126. $child->root = $this->root;
  127. }
  128. // The child will have children if a debug node has been added
  129. foreach ($child->children as $grandchild) {
  130. $grandchild->root = $this->root;
  131. }
  132. }
  133. /**
  134. * Returns a value indicating if this node has children
  135. * @return boolean true if the node has children, false if not
  136. */
  137. public function hasChildren() {
  138. return !empty($this->children);
  139. }
  140. /**
  141. * Returns the node's children
  142. * @return array the node's children
  143. */
  144. public function getChildren() {
  145. return $this->children;
  146. }
  147. /**
  148. * Returns a value indicating if this node is a child of the passed node.
  149. * This just checks the levels of the nodes. If this node is at a greater
  150. * level than the passed node if is a child of it.
  151. * @return boolean true if the node is a child of the passed node, false if not
  152. */
  153. public function isChildOf($node) {
  154. return $this->level > $node->level;
  155. }
  156. /**
  157. * Returns the last child node of this node.
  158. * @return SassNode the last child node of this node
  159. */
  160. public function getLastChild() {
  161. return $this->children[count($this->children) - 1];
  162. }
  163. /**
  164. * Returns the level of this node.
  165. * @return integer the level of this node
  166. */
  167. public function getLevel() {
  168. return $this->token->level;
  169. }
  170. /**
  171. * Returns the source for this node
  172. * @return string the source for this node
  173. */
  174. public function getSource() {
  175. return $this->token->source;
  176. }
  177. /**
  178. * Returns the debug_info option setting for this node
  179. * @return boolean the debug_info option setting for this node
  180. */
  181. public function getDebug_info() {
  182. return $this->parser->debug_info;
  183. }
  184. /**
  185. * Returns the line number for this node
  186. * @return string the line number for this node
  187. */
  188. public function getLine() {
  189. return $this->token->line;
  190. }
  191. /**
  192. * Returns the line_numbers option setting for this node
  193. * @return boolean the line_numbers option setting for this node
  194. */
  195. public function getLine_numbers() {
  196. return $this->parser->line_numbers;
  197. }
  198. /**
  199. * Returns the filename for this node
  200. * @return string the filename for this node
  201. */
  202. public function getFilename() {
  203. return $this->token->filename;
  204. }
  205. /**
  206. * Returns the Sass parser.
  207. * @return SassParser the Sass parser
  208. */
  209. public function getParser() {
  210. return $this->root->parser;
  211. }
  212. /**
  213. * Returns the property syntax being used.
  214. * @return string the property syntax being used
  215. */
  216. public function getPropertySyntax() {
  217. return $this->root->parser->propertySyntax;
  218. }
  219. /**
  220. * Returns the SassScript parser.
  221. * @return SassScriptParser the SassScript parser
  222. */
  223. public function getScript() {
  224. return $this->root->script;
  225. }
  226. /**
  227. * Returns the renderer.
  228. * @return SassRenderer the renderer
  229. */
  230. public function getRenderer() {
  231. return $this->root->renderer;
  232. }
  233. /**
  234. * Returns the render style of the document tree.
  235. * @return string the render style of the document tree
  236. */
  237. public function getStyle() {
  238. return $this->root->parser->style;
  239. }
  240. /**
  241. * Returns a value indicating whether this node is in a directive
  242. * @param boolean true if the node is in a directive, false if not
  243. */
  244. public function inDirective() {
  245. return $this->parent instanceof SassDirectiveNode ||
  246. $this->parent instanceof SassDirectiveNode;
  247. }
  248. /**
  249. * Returns a value indicating whether this node is in a SassScript directive
  250. * @param boolean true if this node is in a SassScript directive, false if not
  251. */
  252. public function inSassScriptDirective() {
  253. return $this->parent instanceof SassEachNode ||
  254. $this->parent->parent instanceof SassEachNode ||
  255. $this->parent instanceof SassForNode ||
  256. $this->parent->parent instanceof SassForNode ||
  257. $this->parent instanceof SassIfNode ||
  258. $this->parent->parent instanceof SassIfNode ||
  259. $this->parent instanceof SassWhileNode ||
  260. $this->parent->parent instanceof SassWhileNode;
  261. }
  262. /**
  263. * Evaluates a SassScript expression.
  264. * @param string expression to evaluate
  265. * @param SassContext the context in which the expression is evaluated
  266. * @return SassLiteral value of parsed expression
  267. */
  268. public function evaluate($expression, $context, $x=null) {
  269. $context->node = $this;
  270. return $this->script->evaluate($expression, $context, $x);
  271. }
  272. /**
  273. * Replace interpolated SassScript contained in '#{}' with the parsed value.
  274. * @param string the text to interpolate
  275. * @param SassContext the context in which the string is interpolated
  276. * @return string the interpolated text
  277. */
  278. public function interpolate($expression, $context) {
  279. $context->node = $this;
  280. return $this->script->interpolate($expression, $context);
  281. }
  282. /**
  283. * Adds a warning to the node.
  284. * @param string warning message
  285. * @param array line
  286. */
  287. public function addWarning($message) {
  288. $warning = new SassDebugNode($this->token, $message);
  289. $this->addChild($warning);
  290. }
  291. /**
  292. * Parse the children of the node.
  293. * @param SassContext the context in which the children are parsed
  294. * @return array the parsed child nodes
  295. */
  296. public function parseChildren($context) {
  297. $children = array();
  298. foreach ($this->children as $child) {
  299. # child could be a SassLiteral /or/ SassNode
  300. if (method_exists($child, 'parse')) {
  301. $kid = $child->parse($context);
  302. } else {
  303. $kid = array($child);
  304. }
  305. $children = array_merge($children, $kid);
  306. }
  307. return $children;
  308. }
  309. /**
  310. * Returns a value indicating if the token represents this type of node.
  311. * @param object token
  312. * @return boolean true if the token represents this type of node, false if not
  313. */
  314. public static function isa($token) {
  315. throw new SassNodeException('Child classes must override this method');
  316. }
  317. }