2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 function cssparser($html = true) {
22 // Register "destructor"
23 register_shutdown_function(array(&$this, "finalize"));
24 $this->html
= ($html != false);
36 $this->Add("ADDRESS", "");
37 $this->Add("APPLET", "");
38 $this->Add("AREA", "");
39 $this->Add("A", "text-decoration : underline; color : Blue;");
40 $this->Add("A:visited", "color : Purple;");
41 $this->Add("BASE", "");
42 $this->Add("BASEFONT", "");
43 $this->Add("BIG", "");
44 $this->Add("BLOCKQUOTE", "");
45 $this->Add("BODY", "");
47 $this->Add("B", "font-weight: bold;");
48 $this->Add("CAPTION", "");
49 $this->Add("CENTER", "");
50 $this->Add("CITE", "");
51 $this->Add("CODE", "");
53 $this->Add("DFN", "");
54 $this->Add("DIR", "");
55 $this->Add("DIV", "");
59 $this->Add("FONT", "");
60 $this->Add("FORM", "");
67 $this->Add("HEAD", "");
69 $this->Add("HTML", "");
70 $this->Add("IMG", "");
71 $this->Add("INPUT", "");
72 $this->Add("ISINDEX", "");
73 $this->Add("I", "font-style: italic;");
74 $this->Add("KBD", "");
75 $this->Add("LINK", "");
77 $this->Add("MAP", "");
78 $this->Add("MENU", "");
79 $this->Add("META", "");
81 $this->Add("OPTION", "");
82 $this->Add("PARAM", "");
83 $this->Add("PRE", "");
85 $this->Add("SAMP", "");
86 $this->Add("SCRIPT", "");
87 $this->Add("SELECT", "");
88 $this->Add("SMALL", "");
89 $this->Add("STRIKE", "");
90 $this->Add("STRONG", "");
91 $this->Add("STYLE", "");
92 $this->Add("SUB", "");
93 $this->Add("SUP", "");
94 $this->Add("TABLE", "");
96 $this->Add("TEXTAREA", "");
98 $this->Add("TITLE", "");
100 $this->Add("TT", "");
101 $this->Add("UL", "");
102 $this->Add("U", "text-decoration : underline;");
103 $this->Add("VAR", "");
107 function SetHTML($html) {
108 $this->html
= ($html != false);
111 function Add($key, $codestr) {
112 $key = strtolower($key);
113 $codestr = strtolower($codestr);
114 if(!isset($this->css
[$key])) {
115 $this->css
[$key] = array();
117 $codes = explode(";",$codestr);
118 if(count($codes) > 0) {
119 $codekey=''; $codevalue='';
120 foreach($codes as $code) {
122 $this->assignValues(explode(":",$code),$codekey,$codevalue);
123 if(strlen($codekey) > 0) {
124 $this->css
[$key][trim($codekey)] = trim($codevalue);
130 private function assignValues($arr,&$val1,&$val2) {
134 $val2=($n > 1) ?
$arr[1] : '';
137 function Get($key, $property) {
138 $key = strtolower($key);
139 $property = strtolower($property);
140 $tag='';$subtag='';$class='';$id='';
141 $this->assignValues(explode(":",$key),$tag,$subtag);
142 $this->assignValues(explode(".",$tag),$tag,$class);
143 $this->assignValues(explode("#",$tag),$tag,$id);
145 $_subtag=''; $_class=''; $_id='';
146 foreach($this->css
as $_tag => $value) {
147 $this->assignValues(explode(":",$_tag),$_tag,$_subtag);
148 $this->assignValues(explode(".",$_tag),$_tag,$_class);
149 $this->assignValues(explode("#",$_tag),$_tag,$_id);
151 $tagmatch = (strcmp($tag, $_tag) == 0) |
(strlen($_tag) == 0);
152 $subtagmatch = (strcmp($subtag, $_subtag) == 0) |
(strlen($_subtag) == 0);
153 $classmatch = (strcmp($class, $_class) == 0) |
(strlen($_class) == 0);
154 $idmatch = (strcmp($id, $_id) == 0);
156 if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
158 if((strlen($temp) > 0) & (strlen($_class) > 0)) {
159 $temp .= ".".$_class;
160 } elseif(strlen($temp) == 0) {
163 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
164 $temp .= ":".$_subtag;
165 } elseif(strlen($temp) == 0) {
166 $temp = ":".$_subtag;
168 if(isset($this->css
[$temp][$property])) {
169 $result = $this->css
[$temp][$property];
176 function GetSection($key) {
177 $key = strtolower($key);
178 $tag='';$subtag='';$class='';$id='';
179 $_subtag=''; $_class=''; $_id='';
181 $this->assignValues(explode(":",$key),$tag,$subtag);
182 $this->assignValues(explode(".",$tag),$tag,$class);
183 $this->assignValues(explode("#",$tag),$tag,$id);
185 foreach($this->css
as $_tag => $value) {
186 $this->assignValues(explode(":",$_tag),$_tag,$_subtag);
187 $this->assignValues(explode(".",$_tag),$_tag,$_class);
188 $this->assignValues(explode("#",$_tag),$_tag,$_id);
190 $tagmatch = (strcmp($tag, $_tag) == 0) |
(strlen($_tag) == 0);
191 $subtagmatch = (strcmp($subtag, $_subtag) == 0) |
(strlen($_subtag) == 0);
192 $classmatch = (strcmp($class, $_class) == 0) |
(strlen($_class) == 0);
193 $idmatch = (strcmp($id, $_id) == 0);
195 if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
197 if((strlen($temp) > 0) & (strlen($_class) > 0)) {
198 $temp .= ".".$_class;
199 } elseif(strlen($temp) == 0) {
202 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
203 $temp .= ":".$_subtag;
204 } elseif(strlen($temp) == 0) {
205 $temp = ":".$_subtag;
207 foreach($this->css
[$temp] as $property => $value) {
208 $result[$property] = $value;
215 function ParseStr($str) {
218 $str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str);
219 // Parse this damn csscode
220 $parts = explode("}",$str);
221 if(count($parts) > 0) {
222 foreach($parts as $part) {
223 $keystr='';$codestr='';
224 $this->assignValues(explode("{",$part),$keystr,$codestr);
225 $keys = explode(",",trim($keystr));
226 if(count($keys) > 0) {
227 foreach($keys as $key) {
228 if(strlen($key) > 0) {
229 $key = str_replace("\n", "", $key);
230 $key = str_replace("\\", "", $key);
231 $this->Add($key, trim($codestr));
238 return (count($this->css
) > 0);
241 function Parse($filename) {
243 if(file_exists($filename)) {
244 return $this->ParseStr(file_get_contents($filename));
252 foreach($this->css
as $key => $values) {
253 $result .= $key." {\n";
254 foreach($values as $key => $value) {
255 $result .= " $key: $value;\n";