1
|
<?php
|
2
|
|
3
|
// $Id: class.cssparser.php 559 2008-01-18 19:12:51Z Ruebenwurzel $
|
4
|
|
5
|
/*
|
6
|
|
7
|
Website Baker Project <http://www.websitebaker.org/>
|
8
|
Copyright (C) 2004-2008, Ryan Djurovich
|
9
|
|
10
|
Website Baker is free software; you can redistribute it and/or modify
|
11
|
it under the terms of the GNU General Public License as published by
|
12
|
the Free Software Foundation; either version 2 of the License, or
|
13
|
(at your option) any later version.
|
14
|
|
15
|
Website Baker is distributed in the hope that it will be useful,
|
16
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
GNU General Public License for more details.
|
19
|
|
20
|
You should have received a copy of the GNU General Public License
|
21
|
along with Website Baker; if not, write to the Free Software
|
22
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
|
24
|
*/
|
25
|
|
26
|
/*
|
27
|
* Class to parse css information.
|
28
|
*
|
29
|
* See the readme file : http://www.phpclasses.org/browse/file/4685.html
|
30
|
*
|
31
|
* $Id: class.cssparser.php 559 2008-01-18 19:12:51Z Ruebenwurzel $
|
32
|
*
|
33
|
* @author http://www.phpclasses.org/browse/package/1289.html
|
34
|
* @package PhpGedView
|
35
|
* @subpackage Charts
|
36
|
*
|
37
|
* added function GetXML to the cssparser class (Christian Sommer, 2007)
|
38
|
*
|
39
|
*/
|
40
|
|
41
|
class cssparser {
|
42
|
var $css;
|
43
|
var $html;
|
44
|
|
45
|
function cssparser($html = true) {
|
46
|
// Register "destructor"
|
47
|
register_shutdown_function(array(&$this, "finalize"));
|
48
|
$this->html = ($html != false);
|
49
|
$this->Clear();
|
50
|
}
|
51
|
|
52
|
function finalize() {
|
53
|
unset($this->css);
|
54
|
}
|
55
|
|
56
|
function Clear() {
|
57
|
unset($this->css);
|
58
|
$this->css = array();
|
59
|
if($this->html) {
|
60
|
$this->Add("ADDRESS", "");
|
61
|
$this->Add("APPLET", "");
|
62
|
$this->Add("AREA", "");
|
63
|
$this->Add("A", "");
|
64
|
$this->Add("A:visited", "");
|
65
|
$this->Add("BASE", "");
|
66
|
$this->Add("BASEFONT", "");
|
67
|
$this->Add("BIG", "");
|
68
|
$this->Add("BLOCKQUOTE", "");
|
69
|
$this->Add("BODY", "");
|
70
|
$this->Add("BR", "");
|
71
|
$this->Add("B", "");
|
72
|
$this->Add("CAPTION", "");
|
73
|
$this->Add("CENTER", "");
|
74
|
$this->Add("CITE", "");
|
75
|
$this->Add("CODE", "");
|
76
|
$this->Add("DD", "");
|
77
|
$this->Add("DFN", "");
|
78
|
$this->Add("DIR", "");
|
79
|
$this->Add("DIV", "");
|
80
|
$this->Add("DL", "");
|
81
|
$this->Add("DT", "");
|
82
|
$this->Add("EM", "");
|
83
|
$this->Add("FONT", "");
|
84
|
$this->Add("FORM", "");
|
85
|
$this->Add("H1", "");
|
86
|
$this->Add("H2", "");
|
87
|
$this->Add("H3", "");
|
88
|
$this->Add("H4", "");
|
89
|
$this->Add("H5", "");
|
90
|
$this->Add("H6", "");
|
91
|
$this->Add("HEAD", "");
|
92
|
$this->Add("HR", "");
|
93
|
$this->Add("HTML", "");
|
94
|
$this->Add("IMG", "");
|
95
|
$this->Add("INPUT", "");
|
96
|
$this->Add("ISINDEX", "");
|
97
|
$this->Add("I", "");
|
98
|
$this->Add("KBD", "");
|
99
|
$this->Add("LINK", "");
|
100
|
$this->Add("LI", "");
|
101
|
$this->Add("MAP", "");
|
102
|
$this->Add("MENU", "");
|
103
|
$this->Add("META", "");
|
104
|
$this->Add("OL", "");
|
105
|
$this->Add("OPTION", "");
|
106
|
$this->Add("PARAM", "");
|
107
|
$this->Add("PRE", "");
|
108
|
$this->Add("P", "");
|
109
|
$this->Add("SAMP", "");
|
110
|
$this->Add("SCRIPT", "");
|
111
|
$this->Add("SELECT", "");
|
112
|
$this->Add("SMALL", "");
|
113
|
$this->Add("STRIKE", "");
|
114
|
$this->Add("STRONG", "");
|
115
|
$this->Add("STYLE", "");
|
116
|
$this->Add("SUB", "");
|
117
|
$this->Add("SUP", "");
|
118
|
$this->Add("TABLE", "");
|
119
|
$this->Add("TD", "");
|
120
|
$this->Add("TEXTAREA", "");
|
121
|
$this->Add("TH", "");
|
122
|
$this->Add("TITLE", "");
|
123
|
$this->Add("TR", "");
|
124
|
$this->Add("TT", "");
|
125
|
$this->Add("UL", "");
|
126
|
$this->Add("U", "");
|
127
|
$this->Add("VAR", "");
|
128
|
}
|
129
|
}
|
130
|
|
131
|
function SetHTML($html) {
|
132
|
$this->html = ($html != false);
|
133
|
}
|
134
|
|
135
|
function Add($key, $codestr) {
|
136
|
$key = strtolower($key);
|
137
|
// $codestr = strtolower($codestr);
|
138
|
if(!isset($this->css[$key])) {
|
139
|
$this->css[$key] = array();
|
140
|
}
|
141
|
$codes = explode(";",$codestr);
|
142
|
if(count($codes) > 0) {
|
143
|
foreach($codes as $code) {
|
144
|
$code = trim($code);
|
145
|
@list($codekey, $codevalue) = explode(":",$code);
|
146
|
if(strlen($codekey) > 0) {
|
147
|
$this->css[$key][trim($codekey)] = trim($codevalue);
|
148
|
}
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
|
153
|
function Get($key, $property) {
|
154
|
$key = strtolower($key);
|
155
|
// $property = strtolower($property);
|
156
|
@list($tag, $subtag) = explode(":",$key);
|
157
|
@list($tag, $class) = explode(".",$tag);
|
158
|
@list($tag, $id) = explode("#",$tag);
|
159
|
$result = "";
|
160
|
foreach($this->css as $_tag => $value) {
|
161
|
@list($_tag, $_subtag) = explode(":",$_tag);
|
162
|
@list($_tag, $_class) = explode(".",$_tag);
|
163
|
@list($_tag, $_id) = explode("#",$_tag);
|
164
|
$tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
|
165
|
$subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
|
166
|
$classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
|
167
|
$idmatch = (strcmp($id, $_id) == 0);
|
168
|
if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
|
169
|
$temp = $_tag;
|
170
|
if((strlen($temp) > 0) & (strlen($_class) > 0)) {
|
171
|
$temp .= ".".$_class;
|
172
|
}
|
173
|
elseif(strlen($temp) == 0) {
|
174
|
$temp = ".".$_class;
|
175
|
}
|
176
|
if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
|
177
|
$temp .= ":".$_subtag;
|
178
|
}
|
179
|
elseif(strlen($temp) == 0) {
|
180
|
$temp = ":".$_subtag;
|
181
|
}
|
182
|
if(isset($this->css[$temp][$property])) {
|
183
|
$result = $this->css[$temp][$property];
|
184
|
}
|
185
|
}
|
186
|
}
|
187
|
return $result;
|
188
|
}
|
189
|
|
190
|
function GetSection($key) {
|
191
|
$key = strtolower($key);
|
192
|
@list($tag, $subtag) = explode(":",$key);
|
193
|
@list($tag, $class) = explode(".",$tag);
|
194
|
@list($tag, $id) = explode("#",$tag);
|
195
|
$result = array();
|
196
|
foreach($this->css as $_tag => $value) {
|
197
|
@list($_tag, $_subtag) = explode(":",$_tag);
|
198
|
@list($_tag, $_class) = explode(".",$_tag);
|
199
|
@list($_tag, $_id) = explode("#",$_tag);
|
200
|
$tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
|
201
|
$subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
|
202
|
$classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
|
203
|
$idmatch = (strcmp($id, $_id) == 0);
|
204
|
if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
|
205
|
$temp = $_tag;
|
206
|
if((strlen($temp) > 0) & (strlen($_class) > 0)) {
|
207
|
$temp .= ".".$_class;
|
208
|
}
|
209
|
elseif(strlen($temp) == 0) {
|
210
|
$temp = ".".$_class;
|
211
|
}
|
212
|
if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
|
213
|
$temp .= ":".$_subtag;
|
214
|
}
|
215
|
elseif(strlen($temp) == 0) {
|
216
|
$temp = ":".$_subtag;
|
217
|
}
|
218
|
foreach($this->css[$temp] as $property => $value) {
|
219
|
$result[$property] = $value;
|
220
|
}
|
221
|
}
|
222
|
}
|
223
|
return $result;
|
224
|
}
|
225
|
|
226
|
function ParseStr($str) {
|
227
|
$this->Clear();
|
228
|
// Remove comments
|
229
|
$str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str);
|
230
|
// Parse this damn csscode
|
231
|
$parts = explode("}",$str);
|
232
|
if(count($parts) > 0) {
|
233
|
foreach($parts as $part) {
|
234
|
@list($keystr,$codestr) = explode("{",$part);
|
235
|
$keys = explode(",",trim($keystr));
|
236
|
if(count($keys) > 0) {
|
237
|
foreach($keys as $key) {
|
238
|
if(strlen($key) > 0) {
|
239
|
$key = str_replace("\n", "", $key);
|
240
|
$key = str_replace("\\", "", $key);
|
241
|
$this->Add($key, trim($codestr));
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
//
|
248
|
return (count($this->css) > 0);
|
249
|
}
|
250
|
|
251
|
function Parse($filename) {
|
252
|
$this->Clear();
|
253
|
if(file_exists($filename)) {
|
254
|
return $this->ParseStr(file_get_contents($filename));
|
255
|
}
|
256
|
else {
|
257
|
return false;
|
258
|
}
|
259
|
}
|
260
|
|
261
|
function GetCSS() {
|
262
|
$result = "";
|
263
|
foreach($this->css as $key => $values) {
|
264
|
$result .= $key." {\n";
|
265
|
foreach($values as $key => $value) {
|
266
|
$result .= " $key: $value;\n";
|
267
|
}
|
268
|
$result .= "}\n\n";
|
269
|
}
|
270
|
return $result;
|
271
|
}
|
272
|
|
273
|
function GetXML() {
|
274
|
// Construction of "fckstyles.xml" for FCKeditor
|
275
|
$styles = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"."\n";
|
276
|
$styles .= '<Styles>'."\n";
|
277
|
foreach ($this->css as $key => $value) {
|
278
|
// skip all CSS that are a combination of CSS
|
279
|
// skip CSS that are binded on an EVENT
|
280
|
if (strpos($key, " ") === false && strpos($key, ":") === false) {
|
281
|
$pieces = explode(".", $key, 2);
|
282
|
if (strcmp($pieces[0], "") != 0) {
|
283
|
continue;
|
284
|
} else {
|
285
|
$style_elem = "span";
|
286
|
}
|
287
|
if (strcmp($pieces[1], "") != 0) {
|
288
|
$style_class_name = $pieces[1];
|
289
|
} else {
|
290
|
$style_class_name = $pieces[0];
|
291
|
}
|
292
|
$styles .= '<Style name="'.$style_class_name.'" element="'.$style_elem.'"';
|
293
|
if (strcmp($style_class_name, $style_elem) != 0) {
|
294
|
$styles .= '>'."\n".'<Attribute name="class" value="'.$style_class_name.'" />'."\n".'</Style>'."\n";
|
295
|
} else {
|
296
|
$styles .= '/>'."\n";
|
297
|
}
|
298
|
}
|
299
|
}
|
300
|
$styles .= '</Styles>'."\n";
|
301
|
return trim($styles);
|
302
|
}
|
303
|
}
|
304
|
?>
|