Debian Codegraph @

vendor/wikimedia/less.php/lib/Less/Tree/Quoted.php

raw
1 <?php
2 /**
3 * @private
4 */
5 class Less_Tree_Quoted extends Less_Tree implements Less_Tree_HasValueProperty {
6 /** @var bool */
7 public $escaped;
8 /** @var string */
9 public $value;
10 /** @var string */
11 public $quote;
12 /** @var int|false */
13 public $index;
14 /** @var array|null */
15 public $currentFileInfo;
16
17 /** @var string */
18 public $variableRegex = '/@\{([\w-]+)\}/';
19 /** @var string */
20 public $propRegex = '/\$\{([\w-]+)\}/';
21
22 /**
23 * @param string $str
24 */
25 public function __construct( $str, $content = '', $escaped = true, $index = false, $currentFileInfo = null ) {
26 $this->escaped = $escaped;
27 $this->value = $content;
28 if ( $str ) {
29 $this->quote = $str[0];
30 }
31 $this->index = $index;
32 $this->currentFileInfo = $currentFileInfo;
33 }
34
35 /**
36 * @see Less_Tree::genCSS
37 */
38 public function genCSS( $output ) {
39 if ( !$this->escaped ) {
40 $output->add( $this->quote, $this->currentFileInfo, $this->index );
41 }
42 $output->add( $this->value );
43 if ( !$this->escaped ) {
44 $output->add( $this->quote );
45 }
46 }
47
48 /**
49 * @see less-3.13.1.js#Quoted.prototype.containsVariables
50 */
51 public function containsVariables() {
52 return preg_match( $this->variableRegex, $this->value );
53 }
54
55 private function variableReplacement( $r, $env ) {
56 do {
57 $value = $r;
58 if ( preg_match_all( $this->variableRegex, $value, $matches ) ) {
59 foreach ( $matches[1] as $i => $match ) {
60 $v = new Less_Tree_Variable( '@' . $match, $this->index, $this->currentFileInfo );
61 $v = $v->compile( $env );
62 $v = ( $v instanceof self ) ? $v->value : $v->toCSS();
63 $r = str_replace( $matches[0][$i], $v, $r );
64 }
65 }
66 } while ( $r != $value );
67 return $r;
68 }
69
70 private function propertyReplacement( $r, $env ) {
71 do {
72 $value = $r;
73 if ( preg_match_all( $this->propRegex, $value, $matches ) ) {
74 foreach ( $matches[1] as $i => $match ) {
75 $v = new Less_Tree_Property( '$' . $match, $this->index, $this->currentFileInfo );
76 $v = $v->compile( $env );
77 $v = ( $v instanceof self ) ? $v->value : $v->toCSS();
78 $r = str_replace( $matches[0][$i], $v, $r );
79 }
80 }
81 } while ( $r != $value );
82 return $r;
83 }
84
85 public function compile( $env ) {
86 $value = $this->value;
87 $value = $this->variableReplacement( $value, $env );
88 $value = $this->propertyReplacement( $value, $env );
89 return new self( $this->quote . $value . $this->quote, $value, $this->escaped, $this->index, $this->currentFileInfo );
90 }
91
92 /**
93 * @param mixed $other
94 * @return int|null
95 * @see less-2.5.3.js#Quoted.prototype.compare
96 */
97 public function compare( $other ) {
98 if ( $other instanceof self && !$this->escaped && !$other->escaped ) {
99 return Less_Tree::numericCompare( $this->value, $other->value );
100 } else {
101 return (
102 $other instanceof Less_Tree
103 && $this->toCSS() === $other->toCSS()
104 ) ? 0 : null;
105 }
106 }
107 }