yii-bootstrap /demo/protected/extensions/less/lib/lessphp/lib/Less/Node/Ruleset.php

Language PHP Lines 254
MD5 Hash f6ae6300c5c88bc8422e63a873e0c244 Estimated Cost $5,012 (why?)
Repository https://bitbucket.org/imdeany/yii-bootstrap View Raw File View Project SPDX
  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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

namespace Less\Node;

class Ruleset
{
    public $selectors;
    public $rules;
    protected $lookups;
    public $root;
    private $_variables;
    private $_rulesets;

    public function __construct($selectors, $rules)
    {
        $this->selectors = $selectors;
        $this->rules = (array) $rules;
        $this->lookups = array();

    }

    public function compile($env)
    {
        $ruleset = new Ruleset($this->selectors, $this->rules);
        $ruleset->root = $this->root;

        // push the current ruleset to the frames stack
        $env->unshiftFrame($ruleset);

        // Evaluate imports
        if ($ruleset->root) {
            for($i = 0; $i < count($ruleset->rules); $i++) {
                if ($ruleset->rules[$i] instanceof \Less\Node\Import && ! $ruleset->rules[$i]->css) {
                    $newRules = $ruleset->rules[$i]->compile($env);
                    $ruleset->rules = array_merge(
                        array_slice($ruleset->rules, 0, $i),
                        (array) $newRules,
                        array_slice($ruleset->rules, $i + 1)
                    );
                }
            }
        }

        // Store the frames around mixin definitions,
        // so they can be evaluated like closures when the time comes.
        foreach($ruleset->rules as $i => $rule) {
            if ($rule instanceof \Less\Node\Mixin\Definition) {
                $ruleset->rules[$i]->frames = $env->frames;
            }
        }

        // Evaluate mixin calls.
        for($i = 0; $i < count($ruleset->rules); $i++) {
            if ($ruleset->rules[$i] instanceof \Less\Node\Mixin\Call) {
                $newRules = $ruleset->rules[$i]->compile($env);
                $ruleset->rules = array_merge(
                    array_slice($ruleset->rules, 0, $i),
                    $newRules,
                    array_slice($ruleset->rules, $i + 1)
                );
            }
        }

        // Evaluate everything else
        foreach($ruleset->rules as $i => $rule) {
            if (! ($rule instanceof \Less\Node\Mixin\Definition)) {
                $ruleset->rules[$i] = is_string($rule) ? $rule : $rule->compile($env);
            }
        }

        // Pop the stack
        $env->shiftFrame();

        return $ruleset;
    }

    public function match($args)
    {
        return ! is_array($args) || count($args) === 0;
    }

    public function variables()
    {
        if ( ! $this->_variables) {
            $this->_variables = array_reduce($this->rules, function ($hash, $r) {
                if ($r instanceof \Less\Node\Rule && $r->variable === true) {
                    $hash[$r->name] = $r;
                }
                return $hash;
            });
        }

        return $this->_variables;
    }

    public function variable($name)
    {
        $vars = $this->variables();

        return isset($vars[$name]) ? $vars[$name] : null;
    }

    public function rulesets ()
    {
        if ($this->_rulesets) {
            return $this->_rulesets;
        } else {
            return $this->_rulesets = array_filter($this->rules, function ($r) {
                return ($r instanceof \Less\Node\Ruleset) || ($r instanceof \Less\Node\Mixin\Definition);
            });
        }
    }

    public function find ($selector, $self = null, $env = null)
    {
        $self = $self ?: $this;
        $rules = array();
        $key = $selector->toCSS($env);

        if (array_key_exists($key, $this->lookups)) {
            return $this->lookups[$key];
        }

        foreach($this->rulesets() as $rule) {
            if ($rule !== $self) {
                foreach($rule->selectors as $ruleSelector) {
                    if ($selector->match($ruleSelector)) {

                        if (count($selector->elements) > count($ruleSelector->elements)) {
                            $rules = array_merge($rules, $rule->find( new \Less\Node\Selector(array_slice($selector->elements, 1)), $self, $env));
                        } else {
                            $rules[] = $rule;
                        }
                        break;
                    }
                }
            }
        }

        $this->lookups[$key] = $rules;

        return $this->lookups[$key];
    }

    //
    // Entry point for code generation
    //
    //     `context` holds an array of arrays.
    //
    public function toCSS ($context, $env)
    {
        $css = array();      // The CSS output
        $rules = array();    // node.Rule instances
        $rulesets = array(); // node.Ruleset instances
        $paths = array();    // Current selectors

        if (! $this->root) {
            if (count($context) === 0) {
                $paths = array_map(function ($s) { return array($s); }, $this->selectors);
            } else {
                $this->joinSelectors($paths, $context, $this->selectors);
            }
        }

        // Compile rules and rulesets
        foreach($this->rules as $rule) {
            if (isset($rule->rules) || ($rule instanceof \Less\Node\Directive)) {
                $rulesets[] = $rule->toCSS($paths, $env);
            } else if ($rule instanceof \Less\Node\Comment) {
                if (!$rule->silent) {
                    if ($this->root) {
                        $rulesets[] = $rule->toCSS($env);
                    } else {
                        $rules[] = $rule->toCSS($env);
                    }
                }
            } else {
                if (method_exists($rule, 'toCSS') && ( ! isset($rule->variable) ||  ! $rule->variable)) {
                    $rules[] = $rule->toCSS($env);
                } else if (isset($rule->value) && $rule->value && ! $rule->variable) {
                    $rules[] = (string) $rule->value;
                }
            }
        }

        $rulesets = implode('', $rulesets);

        // If this is the root node, we don't render
        // a selector, or {}.
        // Otherwise, only output if this ruleset has rules.
        if ($this->root) {
            $css[] = implode($env->compress ? '' : "\n", $rules);
        } else {
            if (count($rules)) {
                $selector = array_map(function ($p) use ($env) {
                    return trim(implode('', array_map(function ($s) use ($env) {
                        return $s->toCSS($env);
                    }, $p)));
                }, $paths);

                $selector = implode($env->compress ? ',' : (count($paths) > 3 ? ",\n" : ', '), $selector);

                $css[] = $selector;
                $css[] = ($env->compress ? '{' : " {\n  ") .
                         implode($env->compress ? '' : "\n  ", $rules) .
                         ($env->compress ? '}' : "\n}\n");
            }
        }
        $css[] = $rulesets;

        return implode('', $css) . ($env->compress ? "\n" : '');
    }

    public function joinSelectors (&$paths, $context, $selectors)
    {
        foreach($selectors as $selector) {
            $this->joinSelector($paths, $context, $selector);
        }
    }

    public function joinSelector (&$paths, $context, $selector)
    {
        $before = array();
        $after = array();
        $beforeElements = array();
        $afterElements = array();
        $hasParentSelector = false;

        foreach($selector->elements as $el) {
            if (strlen($el->combinator->value) > 0 && $el->combinator->value[0] === '&') {
                $hasParentSelector = true;
            }
            if ($hasParentSelector) {
                $afterElements[] = $el;
            } else {
                $beforeElements[] = $el;
            }
        }
        if (! $hasParentSelector) {
            $afterElements = $beforeElements;
            $beforeElements = array();
        }
        if (count($beforeElements) > 0) {
            $before[] = new \Less\Node\Selector($beforeElements);
        }
        if (count($afterElements) > 0) {
            $after[] = new \Less\Node\Selector($afterElements);
        }
        foreach($context as $c) {
            $paths[] = array_merge($before, $c, $after);
        }
    }
}
Back to Top