cssBeautifier.php

Gesponsorde koppelingen

PHP script bestanden

  1. cssBeautifier.php

« Lees de omschrijving en reacties

<pre>

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php    
class Solow_Css_Property
{
    protected $property;
    protected $value;
    
    public function __construct($property, $value)
    {

        $this->property = $property;
        $this->value = $value;
    }

    
    public function parseProperty()
    {

        return $this->property.": ".$this->value.";";
    }
}


class Solow_Css_DeclarationGroup
{
    protected $properties = array();  
    
    public function __construct($selector, $declarations)
    {

        $this->setProperties($selector, $declarations);  
    }

    
    protected function setProperties($selector, $declarations)
    {
        
        $properties = $this->getCleanedProperties($declarations);            
        foreach($properties as $property)
        {

            $propertyValue = explode(':',$property, 2);
            $this->setProperty(trim($propertyValue[0]),  trim($propertyValue[1]));
        }                                                
    }

    
    protected function getCleanedProperties($declarations)
    {

        $properties = array_filter(array_map('trim', explode(";", $declarations)));
        return $properties;
    }

    
    public function setProperty($property, $value)
    {

        $this->properties[$property] = new Solow_Css_Property($property, $value);
    }

    
    public function removeProperty($property)
    {

        unset($this->properties[$property]);
    }

    
    public function getParsedProperties($spacing="    ")
    {

        $temp="";
        foreach($this->properties as $property => $propertyObject)
        {

            $temp.=$spacing.$propertyObject->parseProperty().PHP_EOL;    
        }

        return $temp;
    }
}

          
class Solow_Css_Parser
{                    
    protected $declerationGroups = array();
    protected $variables = array();
    protected $constants = array();      
    protected $_spacing = '    ';
    
    public function __construct($cssString)
    {
                              
        $this->extractCss($cssString);
    }
  
    
    public function __toString()
    {

        return ''.$this->parseStylesheet();  
    }
  
    
    public function parseStylesheet()
    {

        $temp = "";
        foreach($this->declerationGroups as $selector=>$declerationGroup)
        {

            $temp.=$selector.PHP_EOL."{".PHP_EOL;
            $parsedProperties=$declerationGroup->getParsedProperties();                  
            $parsedProperties = preg_replace_callback('/var\(([^)]+)\)/', array($this, "getVariableRegex"), $parsedProperties);
            $parsedProperties = preg_replace_callback('/constant\(([^)]+)\)/', array($this, "getConstantRegex"), $parsedProperties);
            $temp .= substr($parsedProperties, 0, -1);
            $temp.=PHP_EOL."}".PHP_EOL.PHP_EOL;
        }

        return $temp;        
    }

    
    protected function getVariableRegex($match)
    {
        
        return $this->variables[$match[1]];  
    }

    
    protected function getConstantRegex($match)
    {
        
        return $this->constants[$match[1]];  
    }

    
    protected function extractCss($cssString)
    {

        preg_match_all('#([^{]+?)\{([^}]+?)\}#i', $cssString, $matches);  
        foreach($matches[1] as $key=>$tag)
        {
            
            $tag=trim($tag);
            if(strtolower($tag) == "@variables")
            {

                $this->setVariables($tag, $matches[2][$key]);        
            }

            elseif(strtolower($tag) == "@define")
            {

                $this->setConstants($tag, $matches[2][$key]);        
            }
  
            else
            {
                $this->setDeclerationGroup($tag, $matches[2][$key]);    
            }        
        }
    }

    
    public function setSpacing($spacing)
    {

        $this->_spacing = $spacing;
    }

    
    public function setDeclerationGroup($tag, $string)
    {

        $this->declerationGroups[$tag] = new Solow_Css_DeclarationGroup($tag, $string);  
    }

    
    public function removeDeclerationGroup($tag)
    {

        unset($this->declerationGroups[$tag]);
    }
                    
    
    public function setProperty($declerationGroup, $property, $value)
    {

        $this->declerationGroups[$declerationGroup]->setProperty($property, $value);  
    }

    
    public function removeProperty($declerationGroup, $property)
    {

        $this->declerationGroups[$declerationGroup]->removeProperty($property);  
    }

    
    protected function setVariables($selector, $declarations)
    {
                        
        $properties = $this->getCleanedProperties($declarations);
        foreach($properties as $property)
        {

            $propertyValue = explode(':',$property);
            $this->setVariable(trim($propertyValue[0]),  trim($propertyValue[1]));
        }                                                
    }

    
    protected function setConstants($selector, $declarations)
    {
                        
        $properties = $this->getCleanedProperties($declarations);
        foreach($properties as $property)
        {

            $propertyValue = explode(':',$property);
            $this->setConstant(trim($propertyValue[0]),  trim($propertyValue[1]));
        }                                                
    }

    
    public function setVariable($variable, $value)
    {

        $this->variables[$variable] = $value;
    }

    
    public function unsetVariable($variable)
    {

        unset($this->variables[$variable]);
    }

    
    public function setConstant($constant, $value)
    {

        $this->constants[$constant] = $value;
    }

    
    public function unsetConstant($constant)
    {

        unset($this->constants[$constant]);
    }

    
    protected function getCleanedProperties($declarations)
    {

        $properties = array_filter(array_map('trim', explode(";", $declarations)));
        return $properties;
    }
}
    

$test = <<<EOI
@variables
{
        someVar: SomeKey;
        someOtherVar: SomeOtherKey;
        widthGen: 100%;
        image: someCrappyImage.jpg;
}

@Define
{
    someMarginConstant: nothing to do with margin ROFL!;
}
        
#header{            
    width: var(widthGen);
    height: 95px;  
    background-image: url("var(image)");
    background-repeat: no-repeat;
    background-position: left;
    clear: both;
}
        
        #stranglyPlaced
        {
        width: var(widthGen); height: 150px;
        clear: both;
            margin: constant(someMarginConstant);
        }
        
        .oneLiner{width:100%;height:150px;clear:both; margin:0 auto;}
EOI
;

$testInstance = new Solow_Css_Parser($test);  
$testInstance->setProperty('.oneLiner', 'non-existant', 'bullcrap');
echo $testInstance;      
?>


OUTPUT:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#header
{
    width: 100%;
    height: 95px;
    background-image: url("someCrappyImage.jpg");
    background-repeat: no-repeat;
    background-position: left;
    clear: both;
    color: #000;
}

#stranglyPlaced
{
    width: 100%;
    height: 150px;
    clear: both;
    margin: nothing to do with margin ROFL!;
}

.oneLiner
{
    width: 100%;
    height: 150px;
    clear: both;
    margin: 0 auto;
    non-existant: bullcrap;
}

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.