Merge branch 'm25_MDL-41410_Move_Comment_To_Appropriate_Place' of https://github...
[moodle.git] / backup / cc / cc_lib / gral_lib / cssparser.php
blob1dcbee8858d533d59a3213291199344b9dd3d3a5
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 class cssparser {
18 private $css;
19 private $html;
21 function cssparser($html = true) {
22 // Register "destructor"
23 register_shutdown_function(array(&$this, "finalize"));
24 $this->html = ($html != false);
25 $this->Clear();
28 function finalize() {
29 unset($this->css);
32 function Clear() {
33 unset($this->css);
34 $this->css = array();
35 if($this->html) {
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", "");
46 $this->Add("BR", "");
47 $this->Add("B", "font-weight: bold;");
48 $this->Add("CAPTION", "");
49 $this->Add("CENTER", "");
50 $this->Add("CITE", "");
51 $this->Add("CODE", "");
52 $this->Add("DD", "");
53 $this->Add("DFN", "");
54 $this->Add("DIR", "");
55 $this->Add("DIV", "");
56 $this->Add("DL", "");
57 $this->Add("DT", "");
58 $this->Add("EM", "");
59 $this->Add("FONT", "");
60 $this->Add("FORM", "");
61 $this->Add("H1", "");
62 $this->Add("H2", "");
63 $this->Add("H3", "");
64 $this->Add("H4", "");
65 $this->Add("H5", "");
66 $this->Add("H6", "");
67 $this->Add("HEAD", "");
68 $this->Add("HR", "");
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", "");
76 $this->Add("LI", "");
77 $this->Add("MAP", "");
78 $this->Add("MENU", "");
79 $this->Add("META", "");
80 $this->Add("OL", "");
81 $this->Add("OPTION", "");
82 $this->Add("PARAM", "");
83 $this->Add("PRE", "");
84 $this->Add("P", "");
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", "");
95 $this->Add("TD", "");
96 $this->Add("TEXTAREA", "");
97 $this->Add("TH", "");
98 $this->Add("TITLE", "");
99 $this->Add("TR", "");
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) {
121 $code = trim($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) {
131 $n = count($arr);
132 if ($n > 0) {
133 $val1=$arr[0];
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);
144 $result = "";
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) {
157 $temp = $_tag;
158 if((strlen($temp) > 0) & (strlen($_class) > 0)) {
159 $temp .= ".".$_class;
160 } elseif(strlen($temp) == 0) {
161 $temp = ".".$_class;
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];
173 return $result;
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);
184 $result = array();
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) {
196 $temp = $_tag;
197 if((strlen($temp) > 0) & (strlen($_class) > 0)) {
198 $temp .= ".".$_class;
199 } elseif(strlen($temp) == 0) {
200 $temp = ".".$_class;
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;
212 return $result;
215 function ParseStr($str) {
216 $this->Clear();
217 // Remove comments
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) {
242 $this->Clear();
243 if(file_exists($filename)) {
244 return $this->ParseStr(file_get_contents($filename));
245 } else {
246 return false;
250 function GetCSS() {
251 $result = "";
252 foreach($this->css as $key => $values) {
253 $result .= $key." {\n";
254 foreach($values as $key => $value) {
255 $result .= " $key: $value;\n";
257 $result .= "}\n\n";
259 return $result;