Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.59% covered (success)
97.59%
81 / 83
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Less_Tree_Import
97.59% covered (success)
97.59%
81 / 83
77.78% covered (warning)
77.78%
7 / 9
40
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
8
 accept
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 genCSS
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getPath
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 isVariableImport
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 compileForImport
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 compilePath
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 compile
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
7
 skip
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
1<?php
2/**
3 * CSS `@import` node
4 *
5 * The general strategy here is that we don't want to wait
6 * for the parsing to be completed, before we start importing
7 * the file. That's because in the context of a browser,
8 * most of the time will be spent waiting for the server to respond.
9 *
10 * On creation, we push the import path to our import queue, though
11 * `import,push`, we also pass it a callback, which it'll call once
12 * the file has been fetched, and parsed.
13 *
14 * @private
15 * @see less-2.5.3.js#Import.prototype
16 */
17class Less_Tree_Import extends Less_Tree {
18
19    /** @var array<string,bool> */
20    public $options;
21    /** @var int */
22    public $index;
23    /** @var Less_Tree_Quoted|Less_Tree_Url */
24    public $path;
25    /** @var Less_Tree_Value */
26    public $features;
27    /** @var array|null */
28    public $currentFileInfo;
29    /** @var bool|null */
30    public $css;
31    /** @var bool|null This is populated by Less_ImportVisitor */
32    public $doSkip = false;
33    /** @var string|null This is populated by Less_ImportVisitor */
34    public $importedFilename;
35    /**
36     * This is populated by Less_ImportVisitor.
37     *
38     * For imports that use "inline", this holds a raw string.
39     *
40     * @var string|Less_Tree_Ruleset|null
41     */
42    public $root;
43
44    public function __construct( $path, $features, array $options, $index, $currentFileInfo = null ) {
45        $this->options = $options + [ 'inline' => false, 'optional' => false, 'multiple' => false ];
46        $this->index = $index;
47        $this->path = $path;
48        $this->features = $features;
49        $this->currentFileInfo = $currentFileInfo;
50
51        if ( isset( $this->options['less'] ) || $this->options['inline'] ) {
52            $this->css = !isset( $this->options['less'] ) || !$this->options['less'] || $this->options['inline'];
53        } else {
54            $pathValue = $this->getPath();
55            // Leave any ".css" file imports as literals for the browser.
56            // Also leave any remote HTTP resources as literals regardless of whether
57            // they contain ".css" in their filename.
58            if ( $pathValue && (
59                preg_match( '/[#\.\&\?\/]css([\?;].*)?$/', $pathValue )
60                || preg_match( '/^(https?:)?\/\//i', $pathValue )
61            ) ) {
62                $this->css = true;
63            }
64        }
65    }
66
67//
68// The actual import node doesn't return anything, when converted to CSS.
69// The reason is that it's used at the evaluation stage, so that the rules
70// it imports can be treated like any other rules.
71//
72// In `eval`, we make sure all Import nodes get evaluated, recursively, so
73// we end up with a flat structure, which can easily be imported in the parent
74// ruleset.
75//
76
77    public function accept( $visitor ) {
78        if ( $this->features ) {
79            $this->features = $visitor->visitObj( $this->features );
80        }
81        $this->path = $visitor->visitObj( $this->path );
82
83        if ( !$this->options['inline'] && $this->root ) {
84            $this->root = $visitor->visit( $this->root );
85        }
86    }
87
88    public function genCSS( $output ) {
89        if ( $this->css && !isset( $this->path->currentFileInfo["reference"] ) ) {
90            $output->add( '@import ', $this->currentFileInfo, $this->index );
91            $this->path->genCSS( $output );
92            if ( $this->features ) {
93                $output->add( ' ' );
94                $this->features->genCSS( $output );
95            }
96            $output->add( ';' );
97        }
98    }
99
100    /**
101     * @return string|null
102     */
103    public function getPath() {
104        // During the first pass, Less_Tree_Url may contain a Less_Tree_Variable (not yet expanded),
105        // and thus has no value property defined yet. Return null until we reach the next phase.
106        // https://github.com/wikimedia/less.php/issues/29
107        // TODO: Upstream doesn't need a check against Less_Tree_Variable. Why do we?
108        $path = ( $this->path instanceof Less_Tree_Url && !( $this->path->value instanceof Less_Tree_Variable ) )
109            ? $this->path->value->value
110            // e.g. Less_Tree_Quoted
111            : $this->path->value;
112
113        if ( is_string( $path ) ) {
114            // remove query string and fragment
115            return preg_replace( '/[\?#][^\?]*$/', '', $path );
116        }
117    }
118
119    public function isVariableImport() {
120        $path = $this->path;
121        if ( $path instanceof Less_Tree_Url ) {
122            $path = $path->value;
123        }
124        if ( $path instanceof Less_Tree_Quoted ) {
125            return $path->containsVariables();
126        }
127        return true;
128    }
129
130    public function compileForImport( $env ) {
131        $path = $this->path;
132        if ( $path instanceof Less_Tree_Url ) {
133            $path = $path->value;
134        }
135        return new self( $path->compile( $env ), $this->features, $this->options, $this->index, $this->currentFileInfo );
136    }
137
138    public function compilePath( $env ) {
139        $path = $this->path->compile( $env );
140        $rootpath = $this->currentFileInfo['rootpath'] ?? null;
141
142        if ( !( $path instanceof Less_Tree_Url ) ) {
143            if ( $rootpath ) {
144                $pathValue = $path->value;
145                // Add the base path if the import is relative
146                if ( $pathValue && Less_Environment::isPathRelative( $pathValue ) ) {
147                    $path->value = $this->currentFileInfo['uri_root'] . $pathValue;
148                }
149            }
150            $path->value = Less_Environment::normalizePath( $path->value );
151        }
152
153        return $path;
154    }
155
156    /**
157     * @param Less_Environment $env
158     * @see less-2.5.3.js#Import.prototype.eval
159     */
160    public function compile( $env ) {
161        $features = ( $this->features ? $this->features->compile( $env ) : null );
162
163        // import once
164        if ( $this->skip( $env ) ) {
165            return [];
166        }
167
168        if ( $this->options['inline'] ) {
169            $contents = new Less_Tree_Anonymous(
170                $this->root,
171                0,
172                [
173                    'filename' => $this->importedFilename,
174                    'reference' => $this->currentFileInfo['reference'] ?? null,
175                ],
176                true,
177                true,
178                false
179            );
180            return $this->features
181                ? new Less_Tree_Media( [ $contents ], $this->features->value )
182                : [ $contents ];
183        } elseif ( $this->css ) {
184            $newImport = new self( $this->compilePath( $env ), $features, $this->options, $this->index );
185            // TODO: We might need upstream's `if (!newImport.css && this.error) { throw this.error;`
186            return $newImport;
187        } else {
188            $ruleset = new Less_Tree_Ruleset( null, $this->root->rules );
189
190            $ruleset->evalImports( $env );
191
192            return $this->features
193                ? new Less_Tree_Media( $ruleset->rules, $this->features->value )
194                : $ruleset->rules;
195
196        }
197    }
198
199    /**
200     * Should the import be skipped?
201     *
202     * @param Less_Environment $env
203     * @return bool|null
204     */
205    public function skip( $env ) {
206        $path = $this->getPath();
207        // TODO: Since our Import->getPath() varies from upstream Less.js (ours can return null).
208        // we therefore need an empty string fallback here. Remove this fallback once getPath()
209        // is in sync with upstream.
210        $fullPath = Less_FileManager::getFilePath( $path, $this->currentFileInfo )[0] ?? $path ?? '';
211
212        if ( $this->doSkip !== null ) {
213            return $this->doSkip;
214        }
215
216        // @see less-2.5.3.js#ImportVisitor.prototype.onImported
217        if ( isset( $env->importVisitorOnceMap[$fullPath] ) ) {
218            return true;
219        }
220
221        $env->importVisitorOnceMap[$fullPath] = true;
222        return false;
223    }
224}