This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
cssobjects.php
449 lines (375 loc) · 10.2 KB
/
cssobjects.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
class CSSRuntimeError extends Exception {}
class CSSStylesheet
{
/** CSSObject[] */ protected $objects = array();
private $charset="ISO-8859-1";
private $vars = array();
function setCharset($c) {$this->charset = $c;}
function addVars(CSSDeclarations $decs)
{
$defs = $decs->getValues();
$this->vars = array_merge($this->vars,$defs);
}
function getVar($name)
{
if (!isset($this->vars[$name])) throw new CSSRuntimeError('unknown variable '.$name);
assert('$this->vars[$name] instanceof CSSExpression');
return $this->vars[$name]->toTerm();
}
function add(CSSObject $obj)
{
$this->objects[] = $obj;
}
function addSheet(CSSStylesheet $s)
{
$this->objects = array_merge($this->objects, $s->getObjects());
$this->addVarsArray($s->getVars());
}
function getObjects() {return $this->objects;}
function getVars() {return $this->vars;}
function addVarsArray($v) {
$this->vars = array_merge($this->vars, $v);
}
}
interface CSSObject {}
class CSSImport implements CSSObject
{
private $media,$uri;
function __construct($uri,$media)
{
$this->media = $media;
$this->uri = $uri;
}
function getImport() {return $this->import;}
function getMedia() {return $this->media;}
}
class CSSRuleset implements CSSObject
{
function __construct(CSSSelectors $sels,CSSDeclarations $decs)
{
$this->sels = $sels;
$this->decs = $decs;
}
/* stupid, because getDeclarations is property of returned value as well */
function getDeclarations() {return $this->decs;}
/* same as above */
function getSelectors() {return $this->sels;}
}
class CSSDeclarations
{
private $decs = array();
function add(CSSDeclaration $dec) {$this->decs[] = $dec;}
function isEmpty() {return !count($this->decs);}
function getValues()
{
$out = array();
foreach($this->decs as $d)
{
$out[$d->getName()] = $d->getValue();
}
return $out;
}
function getDeclarations() {return $this->decs;}
/** checks if ALL declarations have this filter equal to val */
function queryFilterAll($type,$val)
{
foreach($this->decs as $d)
{
if ($d->queryFilter($type)!==$val) return false;
}
return true;
}
}
class CSSMedia implements CSSObject
{
protected $media,$rulesets,$cond;
function __construct($media,$rulesets,$cond)
{
$this->media = $media;
$this->rulesets = $rulesets;
$this->cond = $cond;
}
function getMedia() {return $this->media;}
function getRulesets() {return $this->rulesets;}
function getConditions() {return $this->cond;}
}
abstract class CSSTerm
{
protected $value;
function __construct($value) {$this->value = $value;}
function output() {return (string)$this->value;}
function getValue() {return $this->value;}
function __toString() {return $this->output();}
}
class CSSTermVar extends CSSTerm
{
private $sheet,$name;
function __construct(CSSStylesheet $sheet,$name) {$this->name = $name; $this->sheet = $sheet;}
function output() {return $this->sheet->getVar($this->name)->output();}
function getValue() {return $this->sheet->getVar($this->name)->getValue();}
function toTerm()
{
$out = $this->sheet->getVar($this->name);
if ($out instanceof CSSTermMath) return $out->toTerm(); return $out;
}
function __toString() {return '\$'.$this->name;}
}
class CSSTermMath extends CSSTerm
{
private $expression;
function __construct($expression) {assert('is_array($expression)'); $this->expression = $expression;}
private function operate($op, CSSTerm $a,CSSTerm $b)
{
if ($a instanceof CSSTermMath || $a instanceof CSSTermVar) {$a = $a->toTerm();}
if ($b instanceof CSSTermMath || $b instanceof CSSTermVar) {$b = $b->toTerm();}
if ($a instanceof CSSTermMath || $a instanceof CSSTermVar) {$a = $a->toTerm();}
if ($b instanceof CSSTermMath || $b instanceof CSSTermVar) {$b = $b->toTerm();}
if ($op == '=')
{
return new CSSTermNumber($a->getValue() == $b->getValue());
}
if ($op == '>')
{
return new CSSTermNumber($a->getValue() > $b->getValue());
}
if ($op == '<')
{
return new CSSTermNumber($a->getValue() < $b->getValue());
}
if ($a instanceof CSSTermString || $b instanceof CSSTermString)
{
if ($op != '+') throw new CSSRuntimeError('invalid string operand');
return new CSSTermString($a->getValue() . $b->getValue());
}
if (!($a instanceof CSSTermSigned && $b instanceof CSSTermSigned))
{
throw new CSSRuntimeError('invalid operands '.get_class($a).' '.get_class($b));
}
switch($op)
{
case '*': case '%': case '/':
if ($op == '*') $res = $a->getValue() * $b->getValue();
elseif ($op == '/') $res = $a->getValue() / $b->getValue();
else $res = $a->getValue() % $b->getValue();
if ($a->getUnit()=='' || $a->getUnit()=='%') {$unit = $b->getUnit();} else {$unit = $a->getUnit();}
//d($a->getValue(),'a = '.$a->getUnit());
//d($b->getValue(),'b = '.$b->getUnit());
if ($unit=='px') $res = (int)$res;
if ($unit=='%' || ($unit=='' && $a->getUnit()=='%')) return new CSSTermPercentage(($res*100.0).'%');
if ($unit=='') return new CSSTermNumber($res);
return new CSSTermDimension($res.$unit);
default:
if ($op == '+') $res = $a->getValue() + $b->getValue();
else $res = $a->getValue() - $b->getValue();
if ($a->getUnit()=='' || $a->getUnit()=='%') {$unit = $b->getUnit();} else {$unit = $a->getUnit();}
//d($a->getValue(),'a = '.$a->getUnit());
//d($b->getValue(),'b = '.$b->getUnit());
if ($unit=='px') $res = (int)$res;
if ($unit=='%') return new CSSTermPercentage(($res*100.0).'%');
if ($unit=='') return new CSSTermNumber($res);
return new CSSTermDimension($res.$unit);
}
//d($a->getUnit());
return new CSSTermNumber($a->getValue() + $b->getValue());
}
/* +/-
abslength [in,pt,cm]
rellength [em,ex]
pxlength [px]
numbers []
x strings []
*,/
any * scalar
*/
private function calculate()
{
$v = $this->expression;
if (count($v)==1) return $v[0];
for($i=1;$i<count($v);$i+=2)
{
if (strchr("*/%",$v[$i]))
{
$v[$i-1] = $this->operate($v[$i],$v[$i-1],$v[$i+1]); array_splice($v,$i,2);
if (count($v)==1) return $v[0];
$i-=2;
}
}
for($i=1;$i<count($v);$i+=2)
{
if (strchr("+-",$v[$i]))
{
$v[$i-1] = $this->operate($v[$i],$v[$i-1],$v[$i+1]); array_splice($v,$i,2);
if (count($v)==1) return $v[0];
$i-=2;
}
}
if (count($v)==1) return $v[0];
for($i=1;$i<count($v);$i+=2)
{
if (strchr("<>=",$v[$i]))
{
$v[$i-1] = $this->operate($v[$i],$v[$i-1],$v[$i+1]); array_splice($v,$i,2);
if (count($v)==1) return $v[0];
$i-=2;
}
}
/*for($i=1;$i<count($v);$i+=2)
{
if ("?"==$v[$i]) {$v[$i-1] = $this->operate($v[$i],$v[$i-1],$v[$i+1]); array_splice($v,$i,2);}
}*/
return $v[0];
}
function output()
{
if ($this->termbusy) throw new CSSRuntimeError('circular dependency in math expression');
$this->termbusy = true;
$o = $this->calculate()->output();
$this->termbusy = false;
return $o;
}
function getValue() {return $this->calculate()->getValue();}
private $termbusy;
function toTerm()
{
return $this->calculate();
}
function __toString() {return 'expr';}
}
class CSSTermString extends CSSTerm
{
function output() {return '"'.$this->value.'"';} // CHECK: buggy, escape
}
class CSSTermIdent extends CSSTerm {}
class CSSTermURI extends CSSTerm
{
function output() {return 'url('.$this->value.')';} // CHECK: buggy, escape, care for ie5mac
}
class CSSTermColor extends CSSTerm
{
function output()
{
return '#'.preg_replace('/^(.)\1(.)\2(.)\3$/','\1\2\3',$this->value);
}
}
class CSSTermSigned extends CSSTerm
{
protected $sign;
function setSign($sign) {$this->sign = $sign;}
function output() {return ($this->sign=='-'?'-':'').$this->value;}
function getValue() {return $this->output();}
function getUnit() {return preg_replace('/[0-9.-]*(.*)$/','\1',$this->value);}
}
class CSSTermNumber extends CSSTermSigned {}
class CSSTermPercentage extends CSSTermSigned
{
function getValue() {return $this->output()/100.0;}
}
class CSSTermDimension extends CSSTermSigned {}
class CSSTermFunction extends CSSTerm
{
function __construct($fname,$fargs)
{
$this->fname = $fname;
$this->fargs = $fargs;
}
function getName() {return $this->fname;}
function getValue() { throw new CSSRuntimeError('Unable to get value of '.$this->getName().'() function.'); }
function output()
{
$o = $this->fname.'(';
foreach($this->fargs->getTerms() as $t)
{
$o .= $t[0].$t[1]->output();
}
return $o.')';
}
}
class CSSSelectors
{
private $sels,$filter;
function add(CSSSelector $sel)
{
$this->sels[] = $sel;
}
function getSelectors() {return $this->sels;}
}
class CSSSelector
{
private $sels = array();
function add($combinator,CSSSimpleSelector $s)
{
$this->sels[] = array($combinator,$s);
}
/* should actually return array of CSSSimpleSelectors. Make combinator a CSSSimpleSelector? */
function getSimpleSelectors() {return $this->sels;}
}
class CSSSimpleSelector
{
function __construct($element,array $qualifiers)
{
$this->element = $element;
$this->qualifiers = $qualifiers;
}
function getElement() {return $this->element;}
function getQualifiers() {return $this->qualifiers;}
}
class CSSExpression
{
private $exprs = array();
function __construct($op=NULL, $exp=NULL)
{
if ($exp!==NULL) $this->add($op,$exp);
}
function add($op, $exp)
{
assert('!is_array($exp)');
assert('$exp instanceof CSSTerm');
$this->exprs[] = array($op,$exp);
}
function toTerm()
{
if (count($this->exprs)>1) {throw new CSSRuntimeError('multi-term expression cast');}
return $this->exprs[0][1];
}
function getTerms()
{
return $this->exprs;
}
function __toString()
{
$o='';
foreach($this->exprs as $e) {$o.= $e[0].$e[1]->__toString();}
return $o;
}
}
class CSSDeclaration
{
function __construct($propname,CSSExpression $expr,$pri)
{
$this->propname = $propname;
$this->expr = $expr;
$this->pri = $pri;
}
function isImportant() {return $this->pri;}
function getName() {return $this->propname;}
/* stupid name? */
function getValue() {return $this->expr;}
function __toString()
{
return $this->getName().':'.$this->expr->__toString();
}
private $filter = array();
function setFilter($type,$val)
{
$this->filter[$type] = $val;
}
function queryFilter($type)
{
return isset($this->filter[$type])?$this->filter[$type]:NULL;
}
function unsetFilter($type)
{
unset($this->filter[$type]);
}
}