File: Import.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (216 lines) | stat: -rw-r--r-- 6,551 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
 * CSS `@import` node
 *
 * The general strategy here is that we don't want to wait
 * for the parsing to be completed, before we start importing
 * the file. That's because in the context of a browser,
 * most of the time will be spent waiting for the server to respond.
 *
 * On creation, we push the import path to our import queue, though
 * `import,push`, we also pass it a callback, which it'll call once
 * the file has been fetched, and parsed.
 *
 * @private
 * @see less-2.5.3.js#Import.prototype
 */
class Less_Tree_Import extends Less_Tree {

	public $options;
	public $index;
	public $path;
	public $features;
	public $currentFileInfo;
	public $css;
	/** @var bool|null This is populated by Less_ImportVisitor */
	public $doSkip = false;
	/** @var string|null This is populated by Less_ImportVisitor */
	public $importedFilename;
	/**
	 * This is populated by Less_ImportVisitor.
	 *
	 * For imports that use "inline", this holds a raw string.
	 *
	 * @var string|Less_Tree_Ruleset|null
	 */
	public $root;

	public function __construct( $path, $features, array $options, $index, $currentFileInfo = null ) {
		$this->options = $options + [ 'inline' => false, 'optional' => false, 'multiple' => false ];
		$this->index = $index;
		$this->path = $path;
		$this->features = $features;
		$this->currentFileInfo = $currentFileInfo;

		if ( isset( $this->options['less'] ) || $this->options['inline'] ) {
			$this->css = !isset( $this->options['less'] ) || !$this->options['less'] || $this->options['inline'];
		} else {
			$pathValue = $this->getPath();
			// Leave any ".css" file imports as literals for the browser.
			// Also leave any remote HTTP resources as literals regardless of whether
			// they contain ".css" in their filename.
			if ( $pathValue && (
				preg_match( '/[#\.\&\?\/]css([\?;].*)?$/', $pathValue )
				|| preg_match( '/^(https?:)?\/\//i', $pathValue )
			) ) {
				$this->css = true;
			}
		}
	}

//
// The actual import node doesn't return anything, when converted to CSS.
// The reason is that it's used at the evaluation stage, so that the rules
// it imports can be treated like any other rules.
//
// In `eval`, we make sure all Import nodes get evaluated, recursively, so
// we end up with a flat structure, which can easily be imported in the parent
// ruleset.
//

	public function accept( $visitor ) {
		if ( $this->features ) {
			$this->features = $visitor->visitObj( $this->features );
		}
		$this->path = $visitor->visitObj( $this->path );

		if ( !$this->options['inline'] && $this->root ) {
			$this->root = $visitor->visit( $this->root );
		}
	}

	public function genCSS( $output ) {
		if ( $this->css && !isset( $this->path->currentFileInfo["reference"] ) ) {
			$output->add( '@import ', $this->currentFileInfo, $this->index );
			$this->path->genCSS( $output );
			if ( $this->features ) {
				$output->add( ' ' );
				$this->features->genCSS( $output );
			}
			$output->add( ';' );
		}
	}

	/**
	 * @return string|null
	 */
	public function getPath() {
		// During the first pass, Less_Tree_Url may contain a Less_Tree_Variable (not yet expanded),
		// and thus has no value property defined yet. Return null until we reach the next phase.
		// https://github.com/wikimedia/less.php/issues/29
		// TODO: Upstream doesn't need a check against Less_Tree_Variable. Why do we?
		$path = ( $this->path instanceof Less_Tree_Url && !( $this->path->value instanceof Less_Tree_Variable ) )
			? $this->path->value->value
			// e.g. Less_Tree_Quoted
			: $this->path->value;

		if ( is_string( $path ) ) {
			// remove query string and fragment
			return preg_replace( '/[\?#][^\?]*$/', '', $path );
		}
	}

	public function isVariableImport() {
		$path = $this->path;
		if ( $path instanceof Less_Tree_Url ) {
			$path = $path->value;
		}
		if ( $path instanceof Less_Tree_Quoted ) {
			return $path->containsVariables();
		}
		return true;
	}

	public function compileForImport( $env ) {
		$path = $this->path;
		if ( $path instanceof Less_Tree_Url ) {
			 $path = $path->value;
		}
		return new self( $path->compile( $env ), $this->features, $this->options, $this->index, $this->currentFileInfo );
	}

	public function compilePath( $env ) {
		$path = $this->path->compile( $env );
		$rootpath = $this->currentFileInfo['rootpath'] ?? null;

		if ( !( $path instanceof Less_Tree_Url ) ) {
			if ( $rootpath ) {
				$pathValue = $path->value;
				// Add the base path if the import is relative
				if ( $pathValue && Less_Environment::isPathRelative( $pathValue ) ) {
					$path->value = $this->currentFileInfo['uri_root'] . $pathValue;
				}
			}
			$path->value = Less_Environment::normalizePath( $path->value );
		}

		return $path;
	}

	/**
	 * @param Less_Environment $env
	 * @see less-2.5.3.js#Import.prototype.eval
	 */
	public function compile( $env ) {
		$features = ( $this->features ? $this->features->compile( $env ) : null );

		// import once
		if ( $this->skip( $env ) ) {
			return [];
		}

		if ( $this->options['inline'] ) {
			$contents = new Less_Tree_Anonymous( $this->root, 0,
				[
					'filename' => $this->importedFilename,
					'reference' => $this->currentFileInfo['reference'] ?? null,
				],
				true,
				true,
				false
			);
			return $this->features
				? new Less_Tree_Media( [ $contents ], $this->features->value )
				: [ $contents ];
		} elseif ( $this->css ) {
			$newImport = new self( $this->compilePath( $env ), $features, $this->options, $this->index );
			// TODO: We might need upstream's `if (!newImport.css && this.error) { throw this.error;`
			return $newImport;
		} else {
			$ruleset = new Less_Tree_Ruleset( null, $this->root->rules );

			$ruleset->evalImports( $env );

			return $this->features
				? new Less_Tree_Media( $ruleset->rules, $this->features->value )
				: $ruleset->rules;

		}
	}

	/**
	 * Should the import be skipped?
	 *
	 * @param Less_Environment $env
	 * @return bool|null
	 */
	public function skip( $env ) {
		$path = $this->getPath();
		// TODO: Since our Import->getPath() varies from upstream Less.js (ours can return null).
		// we therefore need an empty string fallback here. Remove this fallback once getPath()
		// is in sync with upstream.
		$fullPath = Less_FileManager::getFilePath( $path, $this->currentFileInfo )[0] ?? $path ?? '';

		if ( $this->doSkip !== null ) {
			return $this->doSkip;
		}

		// @see less-2.5.3.js#ImportVisitor.prototype.onImported
		if ( isset( $env->importVisitorOnceMap[$fullPath] ) ) {
			return true;
		}

		$env->importVisitorOnceMap[$fullPath] = true;
		return false;
	}
}