Completed
Push — master ( d37729...8e0761 )
by Remo
01:58
created

LessRuleList::parseDirectDescendants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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