-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.php
169 lines (144 loc) · 4.3 KB
/
parser.php
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
<?php
/**
* CSSPARSER
* Copyright (C) 2009 Peter Kröner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* CSS PARSER
* General purpose CSS parser
*/
class CssParser {
public $css;
public $parsed;
/**
* LOAD_STRING
* Loads a css string
*/
public function load_string($string, $overwrite = false){
if($overwrite){
$this->css = $string;
} else {
$this->css .= $string;
}
}
/**
* LOAD_FILE
* Loads a file
*/
public function load_file($file, $overwrite = false){
$this->load_string(file_get_contents($file), $overwrite);
}
/**
* LOAD_FILES
* Loads a number of files
*/
public function load_files($files){
$files = explode(';', $files);
foreach($files as $file){
$this->load_file($file, false);
}
}
/**
* PARSE
* Parses some CSS into an array
*/
public function parse(){
$css = $this->css;
// Remove CSS-Comments
$css = preg_replace('/\/\*.*?\*\//ms', '', $css);
// Remove HTML-Comments
$css = preg_replace('/([^\'"]+?)(\<!--|--\>)([^\'"]+?)/ms', '$1$3', $css);
// Extract @media-blocks into $blocks
preg_match_all('/@.+?\}[^\}]*?\}/ms',$css, $blocks);
// Append the rest to $blocks
array_push($blocks[0],preg_replace('/@.+?\}[^\}]*?\}/ms','',$css));
$ordered = array();
for($i=0;$i<count($blocks[0]);$i++){
// If @media-block, strip declaration and parenthesis
if(substr($blocks[0][$i],0,6) === '@media')
{
$ordered_key = preg_replace('/^(@media[^\{]+)\{.*\}$/ms','$1',$blocks[0][$i]);
$ordered_value = preg_replace('/^@media[^\{]+\{(.*)\}$/ms','$1',$blocks[0][$i]);
}
// Rule-blocks of the sort @import or @font-face
elseif(substr($blocks[0][$i],0,1) === '@')
{
$ordered_key = $blocks[0][$i];
$ordered_value = $blocks[0][$i];
}
else
{
$ordered_key = 'main';
$ordered_value = $blocks[0][$i];
}
// Split by parenthesis, ignoring those inside content-quotes
$ordered[$ordered_key] = preg_split('/([^\'"\{\}]*?[\'"].*?(?<!\\\)[\'"][^\'"\{\}]*?)[\{\}]|([^\'"\{\}]*?)[\{\}]/',trim($ordered_value," \r\n\t"),-1,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
}
// Beginning to rebuild new slim CSS-Array
foreach($ordered as $key => $val){
$new = array();
for($i = 0; $i<count($val); $i++){
// Split selectors and rules and split properties and values
$selector = trim($val[$i]," \r\n\t");
if(!empty($selector)){
if(!isset($new[$selector])) $new[$selector] = array();
$rules = explode(';',$val[++$i]);
foreach($rules as $rule){
$rule = trim($rule," \r\n\t");
if(!empty($rule)){
$rule = array_reverse(explode(':', $rule));
$property = trim(array_pop($rule)," \r\n\t");
$value = implode(':', array_reverse($rule));
if(!isset($new[$selector][$property]) || !preg_match('/!important/',$new[$selector][$property])) $new[$selector][$property] = $value;
elseif(preg_match('/!important/',$new[$selector][$property]) && preg_match('/!important/',$value)) $new[$selector][$property] = $value;
}
}
}
}
$ordered[$key] = $new;
}
$this->parsed = $ordered;
}
/**
* GLUE
* Turn an array back into CSS
*/
public function glue(){
if($this->parsed){
$output = '';
foreach($this->parsed as $media => $content){
if(substr($media,0,6) === '@media'){
$output .= $media . " {\n";
$prefix = "\t";
}
else $prefix = "";
foreach($content as $selector => $rules){
$output .= $prefix.$selector . " {\n";
foreach($rules as $property => $value){
$output .= $prefix."\t".$property.': '.$value;
$output .= ";\n";
}
$output .= $prefix."}\n\n";
}
if(substr($media,0,6) === '@media'){
$output .= "}\n\n";
}
}
return $output;
}
}
}
?>