f2a19b761d7efb1c0b8367e38b28bb40845a42fb
[atutor.git] / docs / include / classes / cssparser.php
1 <?php
2 class cssparser {
3   var $css;
4   var $html;
5   
6   function cssparser($html = true) {
7     // Register "destructor"
8     register_shutdown_function(array(&$this, "finalize"));
9     $this->html = ($html != false);
10     $this->Clear();
11   }
12   
13   function finalize() {
14     unset($this->css);
15   }
16   
17   function Clear() {
18     unset($this->css);
19     $this->css = array();
20     if($this->html) {
21                 /*
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", "");
32       $this->Add("BR", "");
33       $this->Add("B", "font-weight: bold;");
34       $this->Add("CAPTION", "");
35       $this->Add("CENTER", "");
36       $this->Add("CITE", "");
37       $this->Add("CODE", "");
38       $this->Add("DD", "");
39       $this->Add("DFN", "");
40       $this->Add("DIR", "");
41       $this->Add("DIV", "");
42       $this->Add("DL", "");
43       $this->Add("DT", "");
44       $this->Add("EM", "");
45       $this->Add("FONT", "");
46       $this->Add("FORM", "");
47       $this->Add("H1", "");
48       $this->Add("H2", "");
49       $this->Add("H3", "");
50       $this->Add("H4", "");
51       $this->Add("H5", "");
52       $this->Add("H6", "");
53       $this->Add("HEAD", "");
54       $this->Add("HR", "");
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", "");
62       $this->Add("LI", "");
63       $this->Add("MAP", "");
64       $this->Add("MENU", "");
65       $this->Add("META", "");
66       $this->Add("OL", "");
67       $this->Add("OPTION", "");
68       $this->Add("PARAM", "");
69       $this->Add("PRE", "");
70       $this->Add("P", "");
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", "");
81       $this->Add("TD", "");
82       $this->Add("TEXTAREA", "");
83       $this->Add("TH", "");
84       $this->Add("TITLE", "");
85       $this->Add("TR", "");
86       $this->Add("TT", "");
87       $this->Add("UL", "");
88       $this->Add("U", "text-decoration : underline;");
89           */
90       $this->Add("VAR", "");
91
92     }
93   }
94   
95   function SetHTML($html) {
96     $this->html = ($html != false);
97   }
98   
99   function Add($key, $codestr) {
100     $key = strtolower($key);
101     $codestr = strtolower($codestr);
102     if(!isset($this->css[$key])) {
103       $this->css[$key] = array();
104     }
105     $codes = explode(";",$codestr);
106     if(count($codes) > 0) {
107       foreach($codes as $code) {
108         $code = trim($code);
109         list($codekey, $codevalue) = explode(":",$code,2);
110         if(strlen($codekey) > 0) {
111           $this->css[$key][trim($codekey)] = trim($codevalue);
112         }
113       }
114     }
115   }
116   
117   function Get($key, $property) {
118     $key = strtolower($key);
119     $property = strtolower($property);
120     
121     list($tag, $subtag) = explode(":",$key);
122     list($tag, $class) = explode(".",$tag);
123     list($tag, $id) = explode("#",$tag);
124     $result = "";
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);
129       
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);
134       
135       if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
136         $temp = $_tag;
137         if((strlen($temp) > 0) & (strlen($_class) > 0)) {
138           $temp .= ".".$_class;
139         } elseif(strlen($temp) == 0) {
140           $temp = ".".$_class;
141         }
142         if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
143           $temp .= ":".$_subtag;
144         } elseif(strlen($temp) == 0) {
145           $temp = ":".$_subtag;
146         }
147         if(isset($this->css[$temp][$property])) {
148           $result = $this->css[$temp][$property];
149         }
150       }
151     }
152     return $result;
153   }
154   
155   function GetSection($key) {
156     $key = strtolower($key);
157     
158     list($tag, $subtag) = explode(":",$key);
159     list($tag, $class) = explode(".",$tag);
160     list($tag, $id) = explode("#",$tag);
161     $result = array();
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);
166       
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);
171       
172       if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
173         $temp = $_tag;
174         if((strlen($temp) > 0) & (strlen($_class) > 0)) {
175           $temp .= ".".$_class;
176         } elseif(strlen($temp) == 0) {
177           $temp = ".".$_class;
178         }
179         if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
180           $temp .= ":".$_subtag;
181         } elseif(strlen($temp) == 0) {
182           $temp = ":".$_subtag;
183         }
184         foreach($this->css[$temp] as $property => $value) {
185           $result[$property] = $value;
186         }
187       }
188     }
189     return $result;
190   }
191   
192   function ParseStr($str) {
193     $this->Clear();
194     // Remove comments
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));
208             }
209           }
210         }
211       }
212     }
213     //
214     return (count($this->css) > 0);
215   }
216   
217   function Parse($filename) {
218     $this->Clear();
219     if(file_exists($filename)) {
220       return $this->ParseStr(file_get_contents($filename));
221     } else {
222       return false;
223     }
224   }
225   
226   function GetCSS() {
227     $result = "";
228     foreach($this->css as $key => $values) {
229       $result .= $key." {\n";
230       foreach($values as $key => $value) {
231         $result .= "  $key: $value;\n";
232       }
233       $result .= "}\n\n";
234     }
235     return $result;
236   }
237 }
238 ?>