File: Tree.php

package info (click to toggle)
less.php 4.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 616 kB
  • sloc: php: 7,595; makefile: 7
file content (151 lines) | stat: -rw-r--r-- 3,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

/**
 * Tree
 */
class Less_Tree {

	public $parensInOp = false;
	public $extendOnEveryPath;
	public $allExtends;

	/**
	 * @see less-2.5.3.js#Node.prototype.toCSS
	 */
	public function toCSS() {
		$output = new Less_Output();
		$this->genCSS( $output );
		return $output->toString();
	}

	/**
	 * Generate CSS by adding it to the output object
	 *
	 * @param Less_Output $output The output
	 * @return void
	 */
	public function genCSS( $output ) {
	}

	public function compile( $env ) {
		return $this;
	}

	/**
	 * @param Less_Output $output
	 * @param Less_Tree_Ruleset[] $rules
	 */
	public static function outputRuleset( $output, $rules ) {
		$ruleCnt = count( $rules );
		Less_Environment::$tabLevel++;

		// Compressed
		if ( Less_Parser::$options['compress'] ) {
			$output->add( '{' );
			for ( $i = 0; $i < $ruleCnt; $i++ ) {
				$rules[$i]->genCSS( $output );
			}

			$output->add( '}' );
			Less_Environment::$tabLevel--;
			return;
		}

		// Non-compressed
		$tabSetStr = "\n" . str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 );
		$tabRuleStr = $tabSetStr . Less_Parser::$options['indentation'];

		$output->add( " {" );
		for ( $i = 0; $i < $ruleCnt; $i++ ) {
			$output->add( $tabRuleStr );
			$rules[$i]->genCSS( $output );
		}
		Less_Environment::$tabLevel--;
		$output->add( $tabSetStr . '}' );
	}

	public function accept( $visitor ) {
	}

	/**
	 * @param Less_Tree $a
	 * @param Less_Tree $b
	 * @return int|null
	 * @see less-2.5.3.js#Node.compare
	 */
	public static function nodeCompare( $a, $b ) {
		// Less_Tree subclasses that implement compare() are:
		// Anonymous, Color, Dimension, Keyword, Quoted, Unit
		if ( $b instanceof Less_Tree_Quoted || $b instanceof Less_Tree_Anonymous ) {
			// for "symmetric results" force toCSS-based comparison via b.compare()
			// of Quoted or Anonymous if either value is one of those
			// In JS, `-undefined` produces NAN, which, just like undefined
			// will enter the the default/false branch of Less_Tree_Condition#compile.
			// In PHP, `-null` is 0. To ensure parity, preserve the null.
			$res = $b->compare( $a );
			return $res !== null ? -$res : null;
		} elseif ( $a instanceof Less_Tree_Anonymous || $a instanceof Less_Tree_Color
			|| $a instanceof Less_Tree_Dimension || $a instanceof Less_Tree_Keyword
			|| $a instanceof Less_Tree_Quoted || $a instanceof Less_Tree_Unit
		) {
			return $a->compare( $b );
		} elseif ( get_class( $a ) !== get_class( $b ) ) {
			return null;
		}

		// Less_Tree subclasses that have an array value: Less_Tree_Expression, Less_Tree_Value
		// @phan-suppress-next-line PhanUndeclaredProperty
		$aval = $a->value ?? [];
		// @phan-suppress-next-line PhanUndeclaredProperty
		$bval = $b->value ?? [];
		if ( !( $a instanceof Less_Tree_Expression || $a instanceof Less_Tree_Value ) ) {
			return $aval === $bval ? 0 : null;
		}
		if ( count( $aval ) !== count( $bval ) ) {
			return null;
		}
		foreach ( $aval as $i => $item ) {
			if ( self::nodeCompare( $item, $bval[$i] ) !== 0 ) {
				return null;
			}
		}
		return 0;
	}

	/**
	 * @param string|float|int $a
	 * @param string|float|int $b
	 * @return int|null
	 * @see less-2.5.3.js#Node.numericCompare
	 */
	public static function numericCompare( $a, $b ) {
		return $a < $b ? -1
			: ( $a === $b ? 0
				: ( $a > $b ? 1
					// NAN is not greater, less, or equal
					: null
				)
			);
	}

	public static function ReferencedArray( $rules ) {
		foreach ( $rules as $rule ) {
			if ( method_exists( $rule, 'markReferenced' ) ) {
				$rule->markReferenced();
			}
		}
	}

	/**
	 * Requires php 5.3+
	 */
	public static function __set_state( $args ) {
		$class = get_called_class();
		$obj = new $class( null, null, null, null );
		foreach ( $args as $key => $val ) {
			$obj->$key = $val;
		}
		return $obj;
	}

}