Completed
Push — master ( d16265...f301aa )
by Remo
01:54
created

LessRuleList::parseSelectors()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 8.7624
cc 5
eloc 14
nc 4
nop 2
crap 5
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 14
    public function addRule(LessRule $rule)
18
    {
19 14
        $this->list[] = $rule;
20 14
    }
21
22
    /**
23
     * Build and returns a tree for the CSS input
24
     * @return array
25
     */
26 14
    protected function getTree()
27
    {
28 14
        $output = array();
29
30 14
        foreach ($this->list as $ruleSet) {
31 14
            $selectors = $ruleSet->getSelectors();
32
33 14
            foreach ($ruleSet->getTokens() as $token) {
34 14
                $this->parseTreeNode($output, $selectors, $token);
35 14
            }
36 14
        }
37 14
        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 14
    protected function parseDirectDescendants($selector)
49
    {
50 14
        $selector = str_replace('> ', '>', $selector);
51 14
        $selector = str_replace('>', ' >', $selector);
52 14
        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 14
    protected function parsePseudoClasses($selector)
64
    {
65 14
        $nestedPseudo = false;
66 14
        $lastCharacterColon = false;
67 14
        $selectorOut = '';
68 14
        for ($i = 0; $i < strlen($selector); $i++) {
69 14
            $c = $selector{$i};
70
71
            // Don't parse anything between (..) and [..]
72 14
            $nestedPseudo = ($c === '(' || $c === '[') || $nestedPseudo;
73 14
            $nestedPseudo = !($c === ')' || $c === ']') && $nestedPseudo;
74
75 14
            if ($nestedPseudo === false && $c === ':' && $lastCharacterColon === false) {
76 3
                $selectorOut .= ' &';
77 3
                $lastCharacterColon = true;
78 3
            }
79
            else {
80 14
                $lastCharacterColon = false;
81
            }
82
83 14
            $selectorOut .= $c;
84 14
        }
85 14
        return $selectorOut;
86
    }
87
88
    /**
89
     * Ensures that operators like "+" are properly combined with a "&"
90
     * 
91
     * @param $selector
92
     * @param array $characters
93
     * @return string
94
     */
95 14
    protected function parseSelectors($selector, array $characters)
96
    {
97 14
        $selectorOut = '';
98 14
        $selectorFound = false;
99 14
        for ($i = 0; $i < strlen($selector); $i++) {
100 14
            $c = $selector{$i};
101 14
            if ($c == ' ' && $selectorFound) {
102 3
                continue;
103
            }
104
            else {
105 14
                $selectorFound = false;
106
            }
107 14
            if (in_array($c, $characters)) {
108 3
                $selectorOut .= '&';
109 3
                $selectorFound = true;
110 3
            }
111 14
            $selectorOut .= $c;
112 14
        }
113
114 14
        return $selectorOut;
115
    }
116
117
    /**
118
     * Parse CSS input part into a LESS node
119
     * @param $output
120
     * @param $selectors
121
     * @param $token
122
     */
123 14
    protected function parseTreeNode(&$output, $selectors, $token)
124
    {
125
        // we don't parse comments
126 14
        if ($token instanceof \CssCommentToken) {
127 1
            return;
128
        }
129 14
        foreach ($token->MediaTypes as $mediaType) {
130
            // make sure we're aware of our media type
131 14
            if (!array_key_exists($mediaType, $output)) {
132 14
                $output[$mediaType] = array();
133 14
            }
134
135 14
            foreach ($selectors as $selector) {
136
                // add declaration token to output for each selector
137 14
                $currentNode = &$output[$mediaType];
138
139 14
                $selector = $this->parseDirectDescendants($selector);
140 14
                $selector = $this->parsePseudoClasses($selector);
141 14
                $selector = $this->parseSelectors($selector, ['+', '~']);
142
143
                // selectors like "html body" must be split into an array so we can
144
                // easily nest them
145 14
                $selectorPath = preg_split('[ ]', $selector, -1, PREG_SPLIT_NO_EMPTY);
146 14
                foreach ($selectorPath as $selectorPathItem) {
147 14
                    if (!array_key_exists($selectorPathItem, $currentNode)) {
148 14
                        $currentNode[$selectorPathItem] = array();
149 14
                    }
150 14
                    $currentNode = &$currentNode[$selectorPathItem];
151 14
                }
152
153 14
                $currentNode['@rules'][] = $this->formatTokenAsLess($token);
154 14
            }
155 14
        }
156 14
    }
157
158
    /**
159
     * Format LESS nodes in a nicer way with indentation and proper brackets
160
     * @param $token
161
     * @param int $level
162
     * @return string
163
     */
164 14
    public function formatTokenAsLess(\aCssToken $token, $level = 0)
165
    {
166 14
        $indentation = str_repeat("\t", $level);
167
168 14
        if ($token instanceof \CssRulesetDeclarationToken) {
169 14
            return $indentation . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
170 1
        } elseif ($token instanceof \CssAtKeyframesStartToken) {
171 1
            return $indentation . "@" . $token->AtRuleName . " \"" . $token->Name . "\" {";
172 1
        } elseif ($token instanceof \CssAtKeyframesRulesetStartToken) {
173 1
            return $indentation . "\t" . implode(",", $token->Selectors) . " {";
174 1
        } elseif ($token instanceof \CssAtKeyframesRulesetEndToken) {
175 1
            return $indentation . "\t" . "}";
176 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...
177 1
            return $indentation . "\t\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
178 1
        } elseif ($token instanceof \CssAtCharsetToken) {
179 1
            return $indentation . "@charset " . $token->Charset . ";";
180 1
        } elseif ($token instanceof \CssAtFontFaceStartToken) {
181 1
            return "@font-face {";
182 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...
183 1
            return $indentation . "\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
184
        } else {
185 1
            return $indentation . $token;
186
        }
187
    }
188
189 14
    protected function formatAsLess($selector, $level = 0)
190
    {
191 14
        $return = '';
192 14
        $indentation = str_repeat("\t", $level);
193 14
        foreach ($selector as $nodeKey => $node) {
194 14
            $return .= $indentation . "{$nodeKey} {\n";
195
196 14
            foreach ($node as $subNodeKey => $subNodes) {
197 14
                if ($subNodeKey === '@rules') {
198 14
                    foreach ($subNodes as $subNode) {
199 14
                        $return .= $indentation . "\t" . $subNode . "\n";
200 14
                    }
201 14
                } else {
202 11
                    $return .= $this->formatAsLess(array($subNodeKey => $subNodes), $level + 1);
203
                }
204 14
            }
205
206 14
            $return .= $indentation . "}\n";
207
208 14
        }
209 14
        return $return;
210
    }
211
212 14
    public function lessify()
213
    {
214 14
        $tree = $this->getTree();
215 14
        $return = '';
216
217 14
        foreach ($tree as $mediaType => $node) {
218 14
            if ($mediaType == 'all') {
219 14
                $return .= $this->formatAsLess($node);
220 14
            } else {
221 1
                $return .= "@media {$mediaType} {\n";
222 1
                $return .= $this->formatAsLess($node, 1);
223 1
                $return .= "}\n";
224
            }
225 14
        }
226
227 14
        return $return;
228
    }
229
}
230