Completed
Push — master ( 382556...a94bd0 )
by Remo
02:05
created

LessRuleList::parseTreeNode()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 10
Bugs 6 Features 2
Metric Value
c 10
b 6
f 2
dl 0
loc 34
ccs 23
cts 23
cp 1
rs 6.7272
cc 7
eloc 17
nc 10
nop 3
crap 7
1
<?php
2
3
namespace Ortic\Css2Less\tokens;
4
5
/**
6
 * Class LessRuleList
7
 * @package Ortic\Css2Less\tokens
8
 */
9
class LessRuleList
10
{
11
    private $list = array();
12
13
    /**
14
     * Add a new rule object to our list
15
     * @param LessRule $rule
16
     */
17 12
    public function addRule(LessRule $rule)
18
    {
19 12
        $this->list[] = $rule;
20 12
    }
21
22
    /**
23
     * Build and returns a tree for the CSS input
24
     * @return array
25
     */
26 12
    protected function getTree()
27
    {
28 12
        $output = array();
29
30 12
        foreach ($this->list as $ruleSet) {
31 12
            $selectors = $ruleSet->getSelectors();
32
33 12
            foreach ($ruleSet->getTokens() as $token) {
34 12
                $this->parseTreeNode($output, $selectors, $token);
35 12
            }
36 12
        }
37 12
        return $output;
38
    }
39
40
    /**
41
     * Add support for direct descendants operator by aligning the spaces properly.
42
     * the code below supports "html >p" since we split by spaces. A selector "html > p" would cause an
43
     * additional tree level, we therefore normalize them with the two lines below.
44
     *
45
     * @param string $selector
46
     * @return string
47
     */
48 12
    protected function parseDirectDescendants($selector)
49
    {
50 12
        $selector = str_replace('> ', '>', $selector);
51 12
        $selector = str_replace('>', ' >', $selector);
52 12
        return $selector;
53
    }
54
55
    /**
56
     * Support for pseudo classes by adding a space before ":" and also "&" to let less know that there
57
     * shouldn't be a space when concatenating the nested selectors to a single css rule. We have to
58
     * ignore every colon if it's wrapped by :not(...) as we don't nest this in LESS.
59
     *
60
     * @param string $selector
61
     * @return string
62
     */
63 12
    protected function parsePseudoClasses($selector)
64
    {
65 12
        $nestedPseudo = false;
66 12
        $lastCharacterColon = false;
67 12
        $selectorOut = '';
68 12
        for ($i = 0; $i < strlen($selector); $i++) {
69 12
            $c = $selector{$i};
70
71
            // Don't parse anything between (..) and [..]
72 12
            $nestedPseudo = ($c === '(' || $c === '[') || $nestedPseudo;
73 12
            $nestedPseudo = !($c === ')' || $c === ']') && $nestedPseudo;
74
75 12
            if ($nestedPseudo === false && $c === ':' && $lastCharacterColon === false) {
76 3
                $selectorOut .= ' &';
77 3
                $lastCharacterColon = true;
78 3
            }
79
            else {
80 12
                $lastCharacterColon = false;
81
            }
82
83 12
            $selectorOut .= $c;
84 12
        }
85 12
        return $selectorOut;
86
    }
87
88 12
    protected function parseAdjacentSelect($selector)
89
    {
90 12
        $selectorOut = '';
91 12
        $adjacentSelectorFound = false;
92 12
        for ($i = 0; $i < strlen($selector); $i++) {
93 12
            $c = $selector{$i};
94 12
            if ($c == ' ' && $adjacentSelectorFound) {
95 2
                continue;
96
            }
97
            else {
98 12
                $adjacentSelectorFound = false;
99
            }
100 12
            if ($c == '+') {
101 2
                $selectorOut .= '&';
102 2
                $adjacentSelectorFound = true;
103 2
            }
104 12
            $selectorOut .= $c;
105 12
        }
106
107 12
        return $selectorOut;
108
    }
109
110
    /**
111
     * Parse CSS input part into a LESS node
112
     * @param $output
113
     * @param $selectors
114
     * @param $token
115
     */
116 12
    protected function parseTreeNode(&$output, $selectors, $token)
117
    {
118
        // we don't parse comments
119 12
        if ($token instanceof \CssCommentToken) {
120 1
            return;
121
        }
122 12
        foreach ($token->MediaTypes as $mediaType) {
123
            // make sure we're aware of our media type
124 12
            if (!array_key_exists($mediaType, $output)) {
125 12
                $output[$mediaType] = array();
126 12
            }
127
128 12
            foreach ($selectors as $selector) {
129
                // add declaration token to output for each selector
130 12
                $currentNode = &$output[$mediaType];
131
132 12
                $selector = $this->parseDirectDescendants($selector);
133 12
                $selector = $this->parsePseudoClasses($selector);
134 12
                $selector = $this->parseAdjacentSelect($selector);
135
136
                // selectors like "html body" must be split into an array so we can
137
                // easily nest them
138 12
                $selectorPath = preg_split('[ ]', $selector, -1, PREG_SPLIT_NO_EMPTY);
139 12
                foreach ($selectorPath as $selectorPathItem) {
140 12
                    if (!array_key_exists($selectorPathItem, $currentNode)) {
141 12
                        $currentNode[$selectorPathItem] = array();
142 12
                    }
143 12
                    $currentNode = &$currentNode[$selectorPathItem];
144 12
                }
145
146 12
                $currentNode['@rules'][] = $this->formatTokenAsLess($token);
147 12
            }
148 12
        }
149 12
    }
150
151
    /**
152
     * Format LESS nodes in a nicer way with indentation and proper brackets
153
     * @param $token
154
     * @param int $level
155
     * @return string
156
     */
157 12
    public function formatTokenAsLess(\aCssToken $token, $level = 0)
158
    {
159 12
        $indentation = str_repeat("\t", $level);
160
161 12
        if ($token instanceof \CssRulesetDeclarationToken) {
162 12
            return $indentation . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
163 1
        } elseif ($token instanceof \CssAtKeyframesStartToken) {
164 1
            return $indentation . "@" . $token->AtRuleName . " \"" . $token->Name . "\" {";
165 1
        } elseif ($token instanceof \CssAtKeyframesRulesetStartToken) {
166 1
            return $indentation . "\t" . implode(",", $token->Selectors) . " {";
167 1
        } elseif ($token instanceof \CssAtKeyframesRulesetEndToken) {
168 1
            return $indentation . "\t" . "}";
169 1 View Code Duplication
        } elseif ($token instanceof \CssAtKeyframesRulesetDeclarationToken) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170 1
            return $indentation . "\t\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
171 1
        } elseif ($token instanceof \CssAtCharsetToken) {
172 1
            return $indentation . "@charset " . $token->Charset . ";";
173 1
        } elseif ($token instanceof \CssAtFontFaceStartToken) {
174 1
            return "@font-face {";
175 1 View Code Duplication
        } elseif ($token instanceof \CssAtFontFaceDeclarationToken) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176 1
            return $indentation . "\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
177
        } else {
178 1
            return $indentation . $token;
179
        }
180
    }
181
182 12
    protected function formatAsLess($selector, $level = 0)
183
    {
184 12
        $return = '';
185 12
        $indentation = str_repeat("\t", $level);
186 12
        foreach ($selector as $nodeKey => $node) {
187 12
            $return .= $indentation . "{$nodeKey} {\n";
188
189 12
            foreach ($node as $subNodeKey => $subNodes) {
190 12
                if ($subNodeKey === '@rules') {
191 12
                    foreach ($subNodes as $subNode) {
192 12
                        $return .= $indentation . "\t" . $subNode . "\n";
193 12
                    }
194 12
                } else {
195 10
                    $return .= $this->formatAsLess(array($subNodeKey => $subNodes), $level + 1);
196
                }
197 12
            }
198
199 12
            $return .= $indentation . "}\n";
200
201 12
        }
202 12
        return $return;
203
    }
204
205 12
    public function lessify()
206
    {
207 12
        $tree = $this->getTree();
208 12
        $return = '';
209
210 12
        foreach ($tree as $mediaType => $node) {
211 12
            if ($mediaType == 'all') {
212 12
                $return .= $this->formatAsLess($node);
213 12
            } else {
214 1
                $return .= "@media {$mediaType} {\n";
215 1
                $return .= $this->formatAsLess($node, 1);
216 1
                $return .= "}\n";
217
            }
218 12
        }
219
220 12
        return $return;
221
    }
222
}
223