Quoted.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Quoted
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Quoted extends Less_Tree {
  9. public $escaped;
  10. public $value;
  11. public $quote;
  12. public $index;
  13. public $currentFileInfo;
  14. public $type = 'Quoted';
  15. /**
  16. * @param string $str
  17. */
  18. public function __construct( $str, $content = '', $escaped = false, $index = false, $currentFileInfo = null ) {
  19. $this->escaped = $escaped;
  20. $this->value = $content;
  21. if ( $str ) {
  22. $this->quote = $str[0];
  23. }
  24. $this->index = $index;
  25. $this->currentFileInfo = $currentFileInfo;
  26. }
  27. /**
  28. * @see Less_Tree::genCSS
  29. */
  30. public function genCSS( $output ) {
  31. if ( !$this->escaped ) {
  32. $output->add( $this->quote, $this->currentFileInfo, $this->index );
  33. }
  34. $output->add( $this->value );
  35. if ( !$this->escaped ) {
  36. $output->add( $this->quote );
  37. }
  38. }
  39. public function compile( $env ) {
  40. $value = $this->value;
  41. if ( preg_match_all( '/`([^`]+)`/', $this->value, $matches ) ) {
  42. foreach ( $matches as $i => $match ) {
  43. $js = new Less_Tree_JavaScript( $matches[1], $this->index, true );
  44. $js = $js->compile()->value;
  45. $value = str_replace( $matches[0][$i], $js, $value );
  46. }
  47. }
  48. if ( preg_match_all( '/@\{([\w-]+)\}/', $value, $matches ) ) {
  49. foreach ( $matches[1] as $i => $match ) {
  50. $v = new Less_Tree_Variable( '@' . $match, $this->index, $this->currentFileInfo );
  51. $v = $v->compile( $env );
  52. $v = ( $v instanceof Less_Tree_Quoted ) ? $v->value : $v->toCSS();
  53. $value = str_replace( $matches[0][$i], $v, $value );
  54. }
  55. }
  56. return new Less_Tree_Quoted( $this->quote . $value . $this->quote, $value, $this->escaped, $this->index, $this->currentFileInfo );
  57. }
  58. public function compare( $x ) {
  59. if ( !Less_Parser::is_method( $x, 'toCSS' ) ) {
  60. return -1;
  61. }
  62. $left = $this->toCSS();
  63. $right = $x->toCSS();
  64. if ( $left === $right ) {
  65. return 0;
  66. }
  67. return $left < $right ? -1 : 1;
  68. }
  69. }