1 | <?php |
||
2 | /* Source: https://github.com/moodle/moodle/blob/MOODLE_310_STABLE/backup/cc/cc_lib/gral_lib/cssparser.php under GNU/GPL license */ |
||
3 | |||
4 | class CssParser |
||
5 | { |
||
6 | private $css; |
||
7 | private $html; |
||
8 | |||
9 | public function __construct($html = true) |
||
10 | { |
||
11 | $this->html = ($html != false); |
||
12 | $this->clear(); |
||
13 | } |
||
14 | |||
15 | public function clear() |
||
16 | { |
||
17 | unset($this->css); |
||
18 | $this->css = []; |
||
19 | if ($this->html) { |
||
20 | $this->add("ADDRESS", ""); |
||
21 | $this->add("APPLET", ""); |
||
22 | $this->add("AREA", ""); |
||
23 | $this->add("A", "text-decoration : underline; color : Blue;"); |
||
24 | $this->add("A:visited", "color : Purple;"); |
||
25 | $this->add("BASE", ""); |
||
26 | $this->add("BASEFONT", ""); |
||
27 | $this->add("BIG", ""); |
||
28 | $this->add("BLOCKQUOTE", ""); |
||
29 | $this->add("BODY", ""); |
||
30 | $this->add("BR", ""); |
||
31 | $this->add("B", "font-weight: bold;"); |
||
32 | $this->add("CAPTION", ""); |
||
33 | $this->add("CENTER", ""); |
||
34 | $this->add("CITE", ""); |
||
35 | $this->add("CODE", ""); |
||
36 | $this->add("DD", ""); |
||
37 | $this->add("DFN", ""); |
||
38 | $this->add("DIR", ""); |
||
39 | $this->add("DIV", ""); |
||
40 | $this->add("DL", ""); |
||
41 | $this->add("DT", ""); |
||
42 | $this->add("EM", ""); |
||
43 | $this->add("FONT", ""); |
||
44 | $this->add("FORM", ""); |
||
45 | $this->add("H1", ""); |
||
46 | $this->add("H2", ""); |
||
47 | $this->add("H3", ""); |
||
48 | $this->add("H4", ""); |
||
49 | $this->add("H5", ""); |
||
50 | $this->add("H6", ""); |
||
51 | $this->add("HEAD", ""); |
||
52 | $this->add("HR", ""); |
||
53 | $this->add("HTML", ""); |
||
54 | $this->add("IMG", ""); |
||
55 | $this->add("INPUT", ""); |
||
56 | $this->add("ISINDEX", ""); |
||
57 | $this->add("I", "font-style: italic;"); |
||
58 | $this->add("KBD", ""); |
||
59 | $this->add("LINK", ""); |
||
60 | $this->add("LI", ""); |
||
61 | $this->add("MAP", ""); |
||
62 | $this->add("MENU", ""); |
||
63 | $this->add("META", ""); |
||
64 | $this->add("OL", ""); |
||
65 | $this->add("OPTION", ""); |
||
66 | $this->add("PARAM", ""); |
||
67 | $this->add("PRE", ""); |
||
68 | $this->add("P", ""); |
||
69 | $this->add("SAMP", ""); |
||
70 | $this->add("SCRIPT", ""); |
||
71 | $this->add("SELECT", ""); |
||
72 | $this->add("SMALL", ""); |
||
73 | $this->add("STRIKE", ""); |
||
74 | $this->add("STRONG", ""); |
||
75 | $this->add("STYLE", ""); |
||
76 | $this->add("SUB", ""); |
||
77 | $this->add("SUP", ""); |
||
78 | $this->add("TABLE", ""); |
||
79 | $this->add("TD", ""); |
||
80 | $this->add("TEXTAREA", ""); |
||
81 | $this->add("TH", ""); |
||
82 | $this->add("TITLE", ""); |
||
83 | $this->add("TR", ""); |
||
84 | $this->add("TT", ""); |
||
85 | $this->add("UL", ""); |
||
86 | $this->add("U", "text-decoration : underline;"); |
||
87 | $this->add("VAR", ""); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function setHTML($html) |
||
92 | { |
||
93 | $this->html = ($html != false); |
||
94 | } |
||
95 | |||
96 | public function add($key, $codestr) |
||
97 | { |
||
98 | $key = strtolower($key); |
||
99 | $codestr = strtolower($codestr); |
||
100 | if (!isset($this->css[$key])) { |
||
101 | $this->css[$key] = []; |
||
102 | } |
||
103 | $codes = explode(";", $codestr); |
||
104 | if (count($codes) > 0) { |
||
105 | $codekey = ''; |
||
106 | $codevalue = ''; |
||
107 | foreach ($codes as $code) { |
||
108 | $code = trim($code); |
||
109 | $this->assignValues(explode(":", $code), $codekey, $codevalue); |
||
110 | if (strlen($codekey) > 0) { |
||
111 | $this->css[$key][trim($codekey)] = trim($codevalue); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public function get($key, $property) |
||
118 | { |
||
119 | $key = strtolower($key); |
||
120 | $property = strtolower($property); |
||
121 | $tag = ''; |
||
122 | $subtag = ''; |
||
123 | $class = ''; |
||
124 | $id = ''; |
||
125 | $this->assignValues(explode(":", $key), $tag, $subtag); |
||
126 | $this->assignValues(explode(".", $tag), $tag, $class); |
||
127 | $this->assignValues(explode("#", $tag), $tag, $id); |
||
128 | $result = ""; |
||
129 | $_subtag = ''; |
||
130 | $_class = ''; |
||
131 | $_id = ''; |
||
132 | foreach ($this->css as $_tag => $value) { |
||
133 | $this->assignValues(explode(":", $_tag), $_tag, $_subtag); |
||
134 | $this->assignValues(explode(".", $_tag), $_tag, $_class); |
||
135 | $this->assignValues(explode("#", $_tag), $_tag, $_id); |
||
136 | |||
137 | $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
138 | $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0); |
||
0 ignored issues
–
show
|
|||
139 | $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0); |
||
0 ignored issues
–
show
|
|||
140 | $idmatch = (strcmp($id, $_id) == 0); |
||
141 | |||
142 | if ($tagmatch & $subtagmatch & $classmatch & $idmatch) { |
||
143 | $temp = $_tag; |
||
144 | if ((strlen($temp) > 0) & (strlen($_class) > 0)) { |
||
0 ignored issues
–
show
|
|||
145 | $temp .= ".".$_class; |
||
146 | } elseif (strlen($temp) == 0) { |
||
147 | $temp = ".".$_class; |
||
148 | } |
||
149 | if ((strlen($temp) > 0) & (strlen($_subtag) > 0)) { |
||
0 ignored issues
–
show
|
|||
150 | $temp .= ":".$_subtag; |
||
151 | } elseif (strlen($temp) == 0) { |
||
152 | $temp = ":".$_subtag; |
||
153 | } |
||
154 | if (isset($this->css[$temp][$property])) { |
||
155 | $result = $this->css[$temp][$property]; |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | public function getSection($key) |
||
164 | { |
||
165 | $key = strtolower($key); |
||
166 | $tag = ''; |
||
167 | $subtag = ''; |
||
168 | $class = ''; |
||
169 | $id = ''; |
||
170 | $_subtag = ''; |
||
171 | $_class = ''; |
||
172 | $_id = ''; |
||
173 | |||
174 | $this->assignValues(explode(":", $key), $tag, $subtag); |
||
175 | $this->assignValues(explode(".", $tag), $tag, $class); |
||
176 | $this->assignValues(explode("#", $tag), $tag, $id); |
||
177 | $result = []; |
||
178 | foreach ($this->css as $_tag => $value) { |
||
179 | $this->assignValues(explode(":", $_tag), $_tag, $_subtag); |
||
180 | $this->assignValues(explode(".", $_tag), $_tag, $_class); |
||
181 | $this->assignValues(explode("#", $_tag), $_tag, $_id); |
||
182 | |||
183 | $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0); |
||
0 ignored issues
–
show
|
|||
184 | $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0); |
||
0 ignored issues
–
show
|
|||
185 | $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0); |
||
0 ignored issues
–
show
|
|||
186 | $idmatch = (strcmp($id, $_id) == 0); |
||
187 | |||
188 | if ($tagmatch & $subtagmatch & $classmatch & $idmatch) { |
||
189 | $temp = $_tag; |
||
190 | if ((strlen($temp) > 0) & (strlen($_class) > 0)) { |
||
0 ignored issues
–
show
|
|||
191 | $temp .= ".".$_class; |
||
192 | } elseif (strlen($temp) == 0) { |
||
193 | $temp = ".".$_class; |
||
194 | } |
||
195 | if ((strlen($temp) > 0) & (strlen($_subtag) > 0)) { |
||
0 ignored issues
–
show
|
|||
196 | $temp .= ":".$_subtag; |
||
197 | } elseif (strlen($temp) == 0) { |
||
198 | $temp = ":".$_subtag; |
||
199 | } |
||
200 | foreach ($this->css[$temp] as $property => $value) { |
||
0 ignored issues
–
show
|
|||
201 | $result[$property] = $value; |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | return $result; |
||
207 | } |
||
208 | |||
209 | public function parseStr($str) |
||
210 | { |
||
211 | $this->clear(); |
||
212 | // Remove comments |
||
213 | $str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str); |
||
214 | // Parse this damn csscode |
||
215 | $parts = explode("}", $str); |
||
216 | if (count($parts) > 0) { |
||
217 | foreach ($parts as $part) { |
||
218 | $keystr = ''; |
||
219 | $codestr = ''; |
||
220 | $this->assignValues(explode("{", $part), $keystr, $codestr); |
||
221 | $keys = explode(",", trim($keystr)); |
||
222 | if (count($keys) > 0) { |
||
223 | foreach ($keys as $key) { |
||
224 | if (strlen($key) > 0) { |
||
225 | $key = str_replace("\n", "", $key); |
||
226 | $key = str_replace("\\", "", $key); |
||
227 | $this->Add($key, trim($codestr)); |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | return count($this->css) > 0; |
||
235 | } |
||
236 | |||
237 | public function parse($filename) |
||
238 | { |
||
239 | $this->clear(); |
||
240 | if (file_exists($filename)) { |
||
241 | return $this->parseStr(file_get_contents($filename)); |
||
242 | } else { |
||
243 | return false; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public function getCSS() |
||
248 | { |
||
249 | $result = ""; |
||
250 | foreach ($this->css as $key => $values) { |
||
251 | $result .= $key." {\n"; |
||
252 | foreach ($values as $key => $value) { |
||
0 ignored issues
–
show
|
|||
253 | $result .= " $key: $value;\n"; |
||
254 | } |
||
255 | $result .= "}\n\n"; |
||
256 | } |
||
257 | |||
258 | return $result; |
||
259 | } |
||
260 | |||
261 | private function assignValues($arr, &$val1, &$val2) |
||
262 | { |
||
263 | $n = count($arr); |
||
264 | if ($n > 0) { |
||
265 | $val1 = $arr[0]; |
||
266 | $val2 = ($n > 1) ? $arr[1] : ''; |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 |