-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsecss.class.php
181 lines (166 loc) · 4.3 KB
/
parsecss.class.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
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/* Based on a class by Michael Ettl([email protected]) */
class CSS
{
protected $css;
protected $cssprops;
protected $cssstr;
/**
* Constructor function for PHP5
*
*/
public function __construct()
{
$this->css = array();
$this->cssprops = array();
$this->cssstr = "";
}
/**
* Parses an entire CSS file
*
* @param mixed $filename CSS File to parse
*/
public function parse_file($file_name)
{
$fh = fopen($file_name, "r") or die("Error opening file $file_name");
$css_str = fread($fh, filesize($file_name));
fclose($fh);
return($this->parse_css($css_str));
}
/**
* Parses a CSS string
*
* @param string $css_str CSS to parse
*/
public function parse_css($css_str)
{
$this->cssstr = $css_str;
$this->css = "";
$this->cssprops = "";
// Strip all line endings and both single and multiline comments
$css_str = preg_replace("/\/\*.+?\*\//s", "", $css_str);
$css_class = explode("}", $css_str);
while(list($key, $val) = each($css_class)){
$aCSSObj = explode("{", $val);
$cSel = strtolower(trim($aCSSObj[0]));
if($cSel){
$this->cssprops[] = $cSel;
$a = explode(";", $aCSSObj[1]);
while(list($key, $val0) = each($a)){
if(trim($val0)){
$aCSSSub = explode(":", $val0);
$cAtt = strtolower(trim($aCSSSub[0]));
if(isset($aCSSSub[1])){
$aCSSItem[$cAtt] = trim($aCSSSub[1]);
}
}
}
if((isset($this->css[$cSel])) && ($this->css[$cSel])){
$aCSSItem = array_merge($this->css[$cSel], $aCSSItem);
}
$this->css[$cSel] = $aCSSItem;
unset($aCSSItem);
}
if(strstr($cSel, ",")){
$aTags = explode(",", $cSel);
foreach($aTags as $key0 => $value0){
$this->css[$value0] = $this->css[$cSel];
}
unset($this->css[$cSel]);
}
}
unset($css_str, $css_class, $aCSSSub, $aCSSItem, $aCSSObj);
return $this->css;
}
/**
* Builds a CSS string out of an existing object
*
* @param boolean $sorted Sort the attributes alphabetically
* @return string Resulting CSS string
*/
public function build_css($sorted = false)
{
$this->cssstr = "";
foreach($this->css as $key0 => $value0) {
$trimmed = trim($key0);
$this->cssstr .= "$trimmed {\n";
if($sorted) ksort($this->css[$key0], SORT_STRING);
foreach($this->css[$key0] as $key1 => $value1) {
$this->cssstr .= "\t$key1: $value1;\n";
}
$this->cssstr .= "}\n";
}
return ($this->cssstr);
}
/**
* Writes an existing CSS string to file
*
* @param string $file_name File to save to
* @param boolean $sorted Sort the attributes alphabetically
*/
public function write_file($file_name, $sorted = false)
{
if($this->css == "") die("There is no CSS to write!");
if($this->cssstr == "") $this->build_css($sorted);
$fh = fopen($file_name, "w") or die("Error opening file $file_name");
fwrite($fh, $this->cssstr);
fclose($fh);
}
/**
* Returns the entire CSS array
*
* @return array or false
*/
public function get_css()
{
if (isset($this->css)) return ($this->css);
return false;
}
/**
* Returns all CSS properties
*
* @return array
*/
public function get_properties()
{
if (isset($this->cssprops)) return ($this->cssprops);
return array();
}
/**
* Returns a specified CSS property and all its attributes
*
* @param string $property
* @return array
*/
public function get_property($prop)
{
if (isset($this->css[$prop])) return ($this->css[$prop]);
return array();
}
/**
* Gets attribute value of a specified CSS property
*
* @param string $prop CSS property
* @param string $attr CSS attribute
* @return string
*/
public function get_value($prop, $attr)
{
if (isset($this->css[$prop][$attr])) return ($this->css[$prop][$attr]);
return "";
}
/**
* Sets attribute value of a specified CSS property
*
* @param string $prop CSS property
* @param string $attr CSS attribute
* @param string $value CSS attribute value
* @return boolean Returns true when succeeded
*/
public function set_value($prop, $attr, $value)
{
if(empty($prop)||empty($attr)) return false;
$this->css[$prop][$attr] = $value;
return true;
}
}