Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
76 / 76
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Less_Tree_Selector
100.00% covered (success)
100.00%
76 / 76
100.00% covered (success)
100.00%
11 / 11
42
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 accept
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 createDerived
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 match
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 CacheElements
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
10
 isJustParentSelector
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
 compile
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 genCSS
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 markReferenced
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIsReferenced
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 getIsOutput
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @private
4 */
5class Less_Tree_Selector extends Less_Tree {
6
7    public $elements;
8    public $condition;
9    public $extendList = [];
10    public $_css;
11    public $index;
12    public $evaldCondition = false;
13    public $currentFileInfo = [];
14    public $isReferenced;
15    public $mediaEmpty;
16
17    public $elements_len = 0;
18
19    public $_oelements;
20    public $_oelements_assoc;
21    public $_oelements_len;
22    public $cacheable = true;
23
24    /**
25     * @param Less_Tree_Element[] $elements
26     * @param Less_Tree_Element[] $extendList
27     * @param Less_Tree_Condition|null $condition
28     * @param int|null $index
29     * @param array|null $currentFileInfo
30     * @param bool|null $isReferenced
31     */
32    public function __construct( $elements, $extendList = [], $condition = null, $index = null, $currentFileInfo = null, $isReferenced = null ) {
33        $this->elements = $elements;
34        $this->elements_len = count( $elements );
35        $this->extendList = $extendList;
36        $this->condition = $condition;
37        if ( $currentFileInfo ) {
38            $this->currentFileInfo = $currentFileInfo;
39        }
40        $this->isReferenced = $isReferenced;
41        if ( !$condition ) {
42            $this->evaldCondition = true;
43        }
44
45        $this->CacheElements();
46    }
47
48    public function accept( $visitor ) {
49        $this->elements = $visitor->visitArray( $this->elements );
50        $this->extendList = $visitor->visitArray( $this->extendList );
51        if ( $this->condition ) {
52            $this->condition = $visitor->visitObj( $this->condition );
53        }
54
55        if ( $visitor instanceof Less_Visitor_extendFinder ) {
56            $this->CacheElements();
57        }
58    }
59
60    public function createDerived( $elements, $extendList = null, $evaldCondition = null ) {
61        $newSelector = new self(
62            $elements,
63            ( $extendList ?: $this->extendList ),
64            null,
65            $this->index,
66            $this->currentFileInfo,
67            $this->isReferenced
68        );
69        $newSelector->evaldCondition = $evaldCondition ?: $this->evaldCondition;
70        $newSelector->mediaEmpty = $this->mediaEmpty;
71        return $newSelector;
72    }
73
74    public function match( $other ) {
75        if ( !$other->_oelements || ( $this->elements_len < $other->_oelements_len ) ) {
76            return 0;
77        }
78
79        for ( $i = 0; $i < $other->_oelements_len; $i++ ) {
80            if ( $this->elements[$i]->value !== $other->_oelements[$i] ) {
81                return 0;
82            }
83        }
84
85        return $other->_oelements_len; // return number of matched elements
86    }
87
88    /**
89     * @see less-2.5.3.js#Selector.prototype.CacheElements
90     */
91    public function CacheElements() {
92        $this->_oelements = [];
93        $this->_oelements_assoc = [];
94
95        $css = '';
96
97        foreach ( $this->elements as $v ) {
98
99            $css .= $v->combinator;
100            if ( !$v->value_is_object ) {
101                $css .= $v->value;
102                continue;
103            }
104
105            if ( isset( $v->value->value ) && is_scalar( $v->value->value ) ) {
106                $css .= $v->value->value;
107                continue;
108            }
109
110            if ( ( $v->value instanceof Less_Tree_Selector || $v->value instanceof Less_Tree_Variable )
111                || !is_string( $v->value->value ) ) {
112                $this->cacheable = false;
113                return;
114            }
115        }
116
117        $this->_oelements_len = preg_match_all( '/[,&#\*\.\w-](?:[\w-]|(?:\\\\.))*/', $css, $matches );
118        if ( $this->_oelements_len ) {
119            $this->_oelements = $matches[0];
120
121            if ( $this->_oelements[0] === '&' ) {
122                array_shift( $this->_oelements );
123                $this->_oelements_len--;
124            }
125
126            $this->_oelements_assoc = array_fill_keys( $this->_oelements, true );
127        }
128    }
129
130    public function isJustParentSelector() {
131        return !$this->mediaEmpty &&
132            count( $this->elements ) === 1 &&
133            $this->elements[0]->value === '&' &&
134            ( $this->elements[0]->combinator === ' ' || $this->elements[0]->combinator === '' );
135    }
136
137    public function compile( $env ) {
138        $elements = [];
139        foreach ( $this->elements as $el ) {
140            $elements[] = $el->compile( $env );
141        }
142
143        $extendList = [];
144        foreach ( $this->extendList as $el ) {
145            $extendList[] = $el->compile( $el );
146        }
147
148        $evaldCondition = false;
149        if ( $this->condition ) {
150            $evaldCondition = $this->condition->compile( $env );
151        }
152
153        return $this->createDerived( $elements, $extendList, $evaldCondition );
154    }
155
156    /**
157     * @see Less_Tree::genCSS
158     */
159    public function genCSS( $output, $firstSelector = true ) {
160        if ( !$firstSelector && $this->elements[0]->combinator === "" ) {
161            $output->add( ' ', $this->currentFileInfo, $this->index );
162        }
163
164        foreach ( $this->elements as $element ) {
165            $element->genCSS( $output );
166        }
167    }
168
169    public function markReferenced() {
170        $this->isReferenced = true;
171    }
172
173    public function getIsReferenced() {
174        return !isset( $this->currentFileInfo['reference'] ) || !$this->currentFileInfo['reference'] || $this->isReferenced;
175    }
176
177    public function getIsOutput() {
178        return $this->evaldCondition;
179    }
180
181}