Completed
Push — master ( d0e5c1...382556 )
by Remo
03:46
created

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