6 function cssparser($html = true) {
7 // Register "destructor"
8 register_shutdown_function(array(&$this, "finalize"));
9 $this->html = ($html != false);
22 $this->Add("ADDRESS", "");
23 $this->Add("APPLET", "");
24 $this->Add("AREA", "");
25 $this->Add("A", "text-decoration : underline; color : Blue;");
26 $this->Add("A:visited", "color : Purple;");
27 $this->Add("BASE", "");
28 $this->Add("BASEFONT", "");
29 $this->Add("BIG", "");
30 $this->Add("BLOCKQUOTE", "");
31 $this->Add("BODY", "");
33 $this->Add("B", "font-weight: bold;");
34 $this->Add("CAPTION", "");
35 $this->Add("CENTER", "");
36 $this->Add("CITE", "");
37 $this->Add("CODE", "");
39 $this->Add("DFN", "");
40 $this->Add("DIR", "");
41 $this->Add("DIV", "");
45 $this->Add("FONT", "");
46 $this->Add("FORM", "");
53 $this->Add("HEAD", "");
55 $this->Add("HTML", "");
56 $this->Add("IMG", "");
57 $this->Add("INPUT", "");
58 $this->Add("ISINDEX", "");
59 $this->Add("I", "font-style: italic;");
60 $this->Add("KBD", "");
61 $this->Add("LINK", "");
63 $this->Add("MAP", "");
64 $this->Add("MENU", "");
65 $this->Add("META", "");
67 $this->Add("OPTION", "");
68 $this->Add("PARAM", "");
69 $this->Add("PRE", "");
71 $this->Add("SAMP", "");
72 $this->Add("SCRIPT", "");
73 $this->Add("SELECT", "");
74 $this->Add("SMALL", "");
75 $this->Add("STRIKE", "");
76 $this->Add("STRONG", "");
77 $this->Add("STYLE", "");
78 $this->Add("SUB", "");
79 $this->Add("SUP", "");
80 $this->Add("TABLE", "");
82 $this->Add("TEXTAREA", "");
84 $this->Add("TITLE", "");
88 $this->Add("U", "text-decoration : underline;");
90 $this->Add("VAR", "");
95 function SetHTML($html) {
96 $this->html = ($html != false);
99 function Add($key, $codestr) {
100 $key = strtolower($key);
101 $codestr = strtolower($codestr);
102 if(!isset($this->css[$key])) {
103 $this->css[$key] = array();
105 $codes = explode(";",$codestr);
106 if(count($codes) > 0) {
107 foreach($codes as $code) {
109 list($codekey, $codevalue) = explode(":",$code,2);
110 if(strlen($codekey) > 0) {
111 $this->css[$key][trim($codekey)] = trim($codevalue);
117 function Get($key, $property) {
118 $key = strtolower($key);
119 $property = strtolower($property);
121 list($tag, $subtag) = explode(":",$key);
122 list($tag, $class) = explode(".",$tag);
123 list($tag, $id) = explode("#",$tag);
125 foreach($this->css as $_tag => $value) {
126 list($_tag, $_subtag) = explode(":",$_tag);
127 list($_tag, $_class) = explode(".",$_tag);
128 list($_tag, $_id) = explode("#",$_tag);
130 $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
131 $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
132 $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
133 $idmatch = (strcmp($id, $_id) == 0);
135 if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
137 if((strlen($temp) > 0) & (strlen($_class) > 0)) {
138 $temp .= ".".$_class;
139 } elseif(strlen($temp) == 0) {
142 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
143 $temp .= ":".$_subtag;
144 } elseif(strlen($temp) == 0) {
145 $temp = ":".$_subtag;
147 if(isset($this->css[$temp][$property])) {
148 $result = $this->css[$temp][$property];
155 function GetSection($key) {
156 $key = strtolower($key);
158 list($tag, $subtag) = explode(":",$key);
159 list($tag, $class) = explode(".",$tag);
160 list($tag, $id) = explode("#",$tag);
162 foreach($this->css as $_tag => $value) {
163 list($_tag, $_subtag) = explode(":",$_tag);
164 list($_tag, $_class) = explode(".",$_tag);
165 list($_tag, $_id) = explode("#",$_tag);
167 $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
168 $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
169 $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
170 $idmatch = (strcmp($id, $_id) == 0);
172 if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
174 if((strlen($temp) > 0) & (strlen($_class) > 0)) {
175 $temp .= ".".$_class;
176 } elseif(strlen($temp) == 0) {
179 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
180 $temp .= ":".$_subtag;
181 } elseif(strlen($temp) == 0) {
182 $temp = ":".$_subtag;
184 foreach($this->css[$temp] as $property => $value) {
185 $result[$property] = $value;
192 function ParseStr($str) {
195 $str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str);
196 // Parse this damn csscode
197 $parts = explode("}",$str);
198 if(count($parts) > 0) {
199 foreach($parts as $part) {
200 list($keystr,$codestr) = explode("{",$part);
201 $keys = explode(",",trim($keystr));
202 if(count($keys) > 0) {
203 foreach($keys as $key) {
204 if(strlen($key) > 0) {
205 $key = str_replace("\n", "", $key);
206 $key = str_replace("\\", "", $key);
207 $this->Add($key, trim($codestr));
214 return (count($this->css) > 0);
217 function Parse($filename) {
219 if(file_exists($filename)) {
220 return $this->ParseStr(file_get_contents($filename));
228 foreach($this->css as $key => $values) {
229 $result .= $key." {\n";
230 foreach($values as $key => $value) {
231 $result .= " $key: $value;\n";