SassImportNode.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /* SVN FILE: $Id: SassImportNode.php 49 2010-04-04 10:51:24Z chris.l.yates $ */
  3. /**
  4. * SassImportNode 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. /**
  12. * SassImportNode class.
  13. * Represents a CSS Import.
  14. * @package PHamlP
  15. * @subpackage Sass.tree
  16. */
  17. class SassImportNode extends SassNode {
  18. const IDENTIFIER = '@';
  19. const MATCH = '/^@import\s+(.+)/i';
  20. const MATCH_CSS = '/^(.+\.css|url\(.+\)|.+" \w+|"http)/im';
  21. const FILES = 1;
  22. /**
  23. * @var array files to import
  24. */
  25. private $files = array();
  26. /**
  27. * SassImportNode.
  28. * @param object source token
  29. * @return SassImportNode
  30. */
  31. public function __construct($token) {
  32. parent::__construct($token);
  33. preg_match(self::MATCH, $token->source, $matches);
  34. foreach (explode(',', $matches[self::FILES]) as $file) {
  35. $this->files[] = trim($file);
  36. }
  37. }
  38. /**
  39. * Parse this node.
  40. * If the node is a CSS import return the CSS import rule.
  41. * Else returns the rendered tree for the file.
  42. * @param SassContext the context in which this node is parsed
  43. * @return array the parsed node
  44. */
  45. public function parse($context) {
  46. $imported = array();
  47. foreach ($this->files as $file) {
  48. if (preg_match(self::MATCH_CSS, $file)) {
  49. return "@import {$file}";
  50. }
  51. else {
  52. $file = trim($file, '\'"');
  53. $tree = SassFile::getTree(
  54. SassFile::getFile($file, $this->parser), $this->parser);
  55. if (empty($tree)) {
  56. throw new SassImportNodeException('Unable to create document tree for {file}', array('{file}'=>$file), $this);
  57. }
  58. else {
  59. $imported = array_merge($imported, $tree->parse($context)->children);
  60. }
  61. }
  62. }
  63. return $imported;
  64. }
  65. }