Debian Codegraph @

vendor/ezyang/htmlpurifier/library/HTMLPurifier/CSSDefinition.php

raw
1 <?php
2
3 /**
4 * Defines allowed CSS attributes and what their values are.
5 * @see HTMLPurifier_HTMLDefinition
6 */
7 class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition
8 {
9
10 public $type = 'CSS';
11
12 /**
13 * Assoc array of attribute name to definition object.
14 * @type HTMLPurifier_AttrDef[]
15 */
16 public $info = [];
17
18 /**
19 * Constructs the info array. The meat of this class.
20 * @param HTMLPurifier_Config $config
21 */
22 protected function doSetup($config)
23 {
24 $this->info['text-align'] = new HTMLPurifier_AttrDef_Enum(
25 ['left', 'right', 'center', 'justify'],
26 false
27 );
28
29 $this->info['direction'] = new HTMLPurifier_AttrDef_Enum(
30 ['ltr', 'rtl'],
31 false
32 );
33
34 $border_style =
35 $this->info['border-bottom-style'] =
36 $this->info['border-right-style'] =
37 $this->info['border-left-style'] =
38 $this->info['border-top-style'] = new HTMLPurifier_AttrDef_Enum(
39 [
40 'none',
41 'hidden',
42 'dotted',
43 'dashed',
44 'solid',
45 'double',
46 'groove',
47 'ridge',
48 'inset',
49 'outset'
50 ],
51 false
52 );
53
54 $this->info['border-style'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_style);
55
56 $this->info['clear'] = new HTMLPurifier_AttrDef_Enum(
57 ['none', 'left', 'right', 'both'],
58 false
59 );
60 $this->info['float'] = new HTMLPurifier_AttrDef_Enum(
61 ['none', 'left', 'right'],
62 false
63 );
64 $this->info['font-style'] = new HTMLPurifier_AttrDef_Enum(
65 ['normal', 'italic', 'oblique'],
66 false
67 );
68 $this->info['font-variant'] = new HTMLPurifier_AttrDef_Enum(
69 ['normal', 'small-caps'],
70 false
71 );
72
73 $uri_or_none = new HTMLPurifier_AttrDef_CSS_Composite(
74 [
75 new HTMLPurifier_AttrDef_Enum(['none']),
76 new HTMLPurifier_AttrDef_CSS_URI()
77 ]
78 );
79
80 $this->info['list-style-position'] = new HTMLPurifier_AttrDef_Enum(
81 ['inside', 'outside'],
82 false
83 );
84 $this->info['list-style-type'] = new HTMLPurifier_AttrDef_Enum(
85 [
86 'disc',
87 'circle',
88 'square',
89 'decimal',
90 'lower-roman',
91 'upper-roman',
92 'lower-alpha',
93 'upper-alpha',
94 'none'
95 ],
96 false
97 );
98 $this->info['list-style-image'] = $uri_or_none;
99
100 $this->info['list-style'] = new HTMLPurifier_AttrDef_CSS_ListStyle($config);
101
102 $this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
103 ['capitalize', 'uppercase', 'lowercase', 'none'],
104 false
105 );
106 $this->info['color'] = new HTMLPurifier_AttrDef_CSS_Color();
107
108 $this->info['background-image'] = $uri_or_none;
109 $this->info['background-repeat'] = new HTMLPurifier_AttrDef_Enum(
110 ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
111 );
112 $this->info['background-attachment'] = new HTMLPurifier_AttrDef_Enum(
113 ['scroll', 'fixed']
114 );
115 $this->info['background-position'] = new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
116
117 $this->info['background-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
118 [
119 new HTMLPurifier_AttrDef_Enum(
120 [
121 'auto',
122 'cover',
123 'contain',
124 ]
125 ),
126 new HTMLPurifier_AttrDef_CSS_Percentage(),
127 new HTMLPurifier_AttrDef_CSS_Length()
128 ]
129 );
130
131 $border_color =
132 $this->info['border-top-color'] =
133 $this->info['border-bottom-color'] =
134 $this->info['border-left-color'] =
135 $this->info['border-right-color'] =
136 $this->info['background-color'] = new HTMLPurifier_AttrDef_CSS_Composite(
137 [
138 new HTMLPurifier_AttrDef_Enum(['transparent']),
139 new HTMLPurifier_AttrDef_CSS_Color()
140 ]
141 );
142
143 $this->info['background'] = new HTMLPurifier_AttrDef_CSS_Background($config);
144
145 $this->info['border-color'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_color);
146
147 $border_width =
148 $this->info['border-top-width'] =
149 $this->info['border-bottom-width'] =
150 $this->info['border-left-width'] =
151 $this->info['border-right-width'] = new HTMLPurifier_AttrDef_CSS_Composite(
152 [
153 new HTMLPurifier_AttrDef_Enum(['thin', 'medium', 'thick']),
154 new HTMLPurifier_AttrDef_CSS_Length('0') //disallow negative
155 ]
156 );
157
158 $this->info['border-width'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_width);
159
160 $this->info['letter-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
161 [
162 new HTMLPurifier_AttrDef_Enum(['normal']),
163 new HTMLPurifier_AttrDef_CSS_Length()
164 ]
165 );
166
167 $this->info['word-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
168 [
169 new HTMLPurifier_AttrDef_Enum(['normal']),
170 new HTMLPurifier_AttrDef_CSS_Length()
171 ]
172 );
173
174 $this->info['font-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
175 [
176 new HTMLPurifier_AttrDef_Enum(
177 [
178 'xx-small',
179 'x-small',
180 'small',
181 'medium',
182 'large',
183 'x-large',
184 'xx-large',
185 'larger',
186 'smaller'
187 ]
188 ),
189 new HTMLPurifier_AttrDef_CSS_Percentage(),
190 new HTMLPurifier_AttrDef_CSS_Length()
191 ]
192 );
193
194 $this->info['line-height'] = new HTMLPurifier_AttrDef_CSS_Composite(
195 [
196 new HTMLPurifier_AttrDef_Enum(['normal']),
197 new HTMLPurifier_AttrDef_CSS_Number(true), // no negatives
198 new HTMLPurifier_AttrDef_CSS_Length('0'),
199 new HTMLPurifier_AttrDef_CSS_Percentage(true)
200 ]
201 );
202
203 $margin =
204 $this->info['margin-top'] =
205 $this->info['margin-bottom'] =
206 $this->info['margin-left'] =
207 $this->info['margin-right'] = new HTMLPurifier_AttrDef_CSS_Composite(
208 [
209 new HTMLPurifier_AttrDef_CSS_Length(),
210 new HTMLPurifier_AttrDef_CSS_Percentage(),
211 new HTMLPurifier_AttrDef_Enum(['auto'])
212 ]
213 );
214
215 $this->info['margin'] = new HTMLPurifier_AttrDef_CSS_Multiple($margin);
216
217 // non-negative
218 $padding =
219 $this->info['padding-top'] =
220 $this->info['padding-bottom'] =
221 $this->info['padding-left'] =
222 $this->info['padding-right'] = new HTMLPurifier_AttrDef_CSS_Composite(
223 [
224 new HTMLPurifier_AttrDef_CSS_Length('0'),
225 new HTMLPurifier_AttrDef_CSS_Percentage(true)
226 ]
227 );
228
229 $this->info['padding'] = new HTMLPurifier_AttrDef_CSS_Multiple($padding);
230
231 $this->info['text-indent'] = new HTMLPurifier_AttrDef_CSS_Composite(
232 [
233 new HTMLPurifier_AttrDef_CSS_Length(),
234 new HTMLPurifier_AttrDef_CSS_Percentage()
235 ]
236 );
237
238 $trusted_wh = new HTMLPurifier_AttrDef_CSS_Composite(
239 [
240 new HTMLPurifier_AttrDef_CSS_Length('0'),
241 new HTMLPurifier_AttrDef_CSS_Percentage(true),
242 new HTMLPurifier_AttrDef_Enum(['auto'])
243 ]
244 );
245 $trusted_min_wh = new HTMLPurifier_AttrDef_CSS_Composite(
246 [
247 new HTMLPurifier_AttrDef_CSS_Length('0'),
248 new HTMLPurifier_AttrDef_CSS_Percentage(true),
249 ]
250 );
251 $trusted_max_wh = new HTMLPurifier_AttrDef_CSS_Composite(
252 [
253 new HTMLPurifier_AttrDef_CSS_Length('0'),
254 new HTMLPurifier_AttrDef_CSS_Percentage(true),
255 new HTMLPurifier_AttrDef_Enum(['none'])
256 ]
257 );
258 $max = $config->get('CSS.MaxImgLength');
259
260 $this->info['width'] =
261 $this->info['height'] =
262 $max === null ?
263 $trusted_wh :
264 new HTMLPurifier_AttrDef_Switch(
265 'img',
266 // For img tags:
267 new HTMLPurifier_AttrDef_CSS_Composite(
268 [
269 new HTMLPurifier_AttrDef_CSS_Length('0', $max),
270 new HTMLPurifier_AttrDef_Enum(['auto'])
271 ]
272 ),
273 // For everyone else:
274 $trusted_wh
275 );
276 $this->info['min-width'] =
277 $this->info['min-height'] =
278 $max === null ?
279 $trusted_min_wh :
280 new HTMLPurifier_AttrDef_Switch(
281 'img',
282 // For img tags:
283 new HTMLPurifier_AttrDef_CSS_Length('0', $max),
284 // For everyone else:
285 $trusted_min_wh
286 );
287 $this->info['max-width'] =
288 $this->info['max-height'] =
289 $max === null ?
290 $trusted_max_wh :
291 new HTMLPurifier_AttrDef_Switch(
292 'img',
293 // For img tags:
294 new HTMLPurifier_AttrDef_CSS_Composite(
295 [
296 new HTMLPurifier_AttrDef_CSS_Length('0', $max),
297 new HTMLPurifier_AttrDef_Enum(['none'])
298 ]
299 ),
300 // For everyone else:
301 $trusted_max_wh
302 );
303
304 $this->info['aspect-ratio'] = new HTMLPurifier_AttrDef_CSS_Multiple(
305 new HTMLPurifier_AttrDef_CSS_Composite([
306 new HTMLPurifier_AttrDef_CSS_Ratio(),
307 new HTMLPurifier_AttrDef_Enum(['auto']),
308 ])
309 );
310
311 // text-decoration and related shorthands
312 $this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();
313
314 $this->info['text-decoration-line'] = new HTMLPurifier_AttrDef_Enum(
315 ['none', 'underline', 'overline', 'line-through']
316 );
317
318 $this->info['text-decoration-style'] = new HTMLPurifier_AttrDef_Enum(
319 ['solid', 'double', 'dotted', 'dashed', 'wavy']
320 );
321
322 $this->info['text-decoration-color'] = new HTMLPurifier_AttrDef_CSS_Color();
323
324 $this->info['text-decoration-thickness'] = new HTMLPurifier_AttrDef_CSS_Composite([
325 new HTMLPurifier_AttrDef_CSS_Length(),
326 new HTMLPurifier_AttrDef_CSS_Percentage(),
327 new HTMLPurifier_AttrDef_Enum(['auto', 'from-font'])
328 ]);
329
330 $this->info['font-family'] = new HTMLPurifier_AttrDef_CSS_FontFamily();
331
332 // this could use specialized code
333 $this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
334 [
335 'normal',
336 'bold',
337 'bolder',
338 'lighter',
339 '100',
340 '200',
341 '300',
342 '400',
343 '500',
344 '600',
345 '700',
346 '800',
347 '900'
348 ],
349 false
350 );
351
352 // MUST be called after other font properties, as it references
353 // a CSSDefinition object
354 $this->info['font'] = new HTMLPurifier_AttrDef_CSS_Font($config);
355
356 // same here
357 $this->info['border'] =
358 $this->info['border-bottom'] =
359 $this->info['border-top'] =
360 $this->info['border-left'] =
361 $this->info['border-right'] = new HTMLPurifier_AttrDef_CSS_Border($config);
362
363 $this->info['border-collapse'] = new HTMLPurifier_AttrDef_Enum(
364 ['collapse', 'separate']
365 );
366
367 $this->info['caption-side'] = new HTMLPurifier_AttrDef_Enum(
368 ['top', 'bottom']
369 );
370
371 $this->info['table-layout'] = new HTMLPurifier_AttrDef_Enum(
372 ['auto', 'fixed']
373 );
374
375 $this->info['vertical-align'] = new HTMLPurifier_AttrDef_CSS_Composite(
376 [
377 new HTMLPurifier_AttrDef_Enum(
378 [
379 'baseline',
380 'sub',
381 'super',
382 'top',
383 'text-top',
384 'middle',
385 'bottom',
386 'text-bottom'
387 ]
388 ),
389 new HTMLPurifier_AttrDef_CSS_Length(),
390 new HTMLPurifier_AttrDef_CSS_Percentage()
391 ]
392 );
393
394 $this->info['border-spacing'] = new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length(), 2);
395
396 // These CSS properties don't work on many browsers, but we live
397 // in THE FUTURE!
398 $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(
399 ['nowrap', 'normal', 'pre', 'pre-wrap', 'pre-line']
400 );
401
402 if ($config->get('CSS.Proprietary')) {
403 $this->doSetupProprietary($config);
404 }
405
406 if ($config->get('CSS.AllowTricky')) {
407 $this->doSetupTricky($config);
408 }
409
410 if ($config->get('CSS.Trusted')) {
411 $this->doSetupTrusted($config);
412 }
413
414 $allow_important = $config->get('CSS.AllowImportant');
415 // wrap all attr-defs with decorator that handles !important
416 foreach ($this->info as $k => $v) {
417 $this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
418 }
419
420 $this->setupConfigStuff($config);
421 }
422
423 /**
424 * @param HTMLPurifier_Config $config
425 */
426 protected function doSetupProprietary($config)
427 {
428 // Internet Explorer only scrollbar colors
429 $this->info['scrollbar-arrow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
430 $this->info['scrollbar-base-color'] = new HTMLPurifier_AttrDef_CSS_Color();
431 $this->info['scrollbar-darkshadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
432 $this->info['scrollbar-face-color'] = new HTMLPurifier_AttrDef_CSS_Color();
433 $this->info['scrollbar-highlight-color'] = new HTMLPurifier_AttrDef_CSS_Color();
434 $this->info['scrollbar-shadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
435
436 // vendor specific prefixes of opacity
437 $this->info['-moz-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
438 $this->info['-khtml-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
439
440 // only opacity, for now
441 $this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
442
443 // more CSS3
444 $this->info['page-break-after'] =
445 $this->info['page-break-before'] = new HTMLPurifier_AttrDef_Enum(
446 [
447 'auto',
448 'always',
449 'avoid',
450 'left',
451 'right'
452 ]
453 );
454 $this->info['page-break-inside'] = new HTMLPurifier_AttrDef_Enum(['auto', 'avoid']);
455
456 $border_radius = new HTMLPurifier_AttrDef_CSS_Composite(
457 [
458 new HTMLPurifier_AttrDef_CSS_Percentage(true), // disallow negative
459 new HTMLPurifier_AttrDef_CSS_Length('0') // disallow negative
460 ]);
461
462 $this->info['border-top-left-radius'] =
463 $this->info['border-top-right-radius'] =
464 $this->info['border-bottom-right-radius'] =
465 $this->info['border-bottom-left-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius, 2);
466 // TODO: support SLASH syntax
467 $this->info['border-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius, 4);
468
469 }
470
471 /**
472 * @param HTMLPurifier_Config $config
473 */
474 protected function doSetupTricky($config)
475 {
476 $this->info['display'] = new HTMLPurifier_AttrDef_Enum(
477 [
478 'inline',
479 'block',
480 'list-item',
481 'run-in',
482 'compact',
483 'marker',
484 'table',
485 'inline-block',
486 'inline-table',
487 'table-row-group',
488 'table-header-group',
489 'table-footer-group',
490 'table-row',
491 'table-column-group',
492 'table-column',
493 'table-cell',
494 'table-caption',
495 'none'
496 ]
497 );
498 $this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(
499 ['visible', 'hidden', 'collapse']
500 );
501 $this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(['visible', 'hidden', 'auto', 'scroll']);
502 $this->info['opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
503 }
504
505 /**
506 * @param HTMLPurifier_Config $config
507 */
508 protected function doSetupTrusted($config)
509 {
510 $this->info['position'] = new HTMLPurifier_AttrDef_Enum(
511 ['static', 'relative', 'absolute', 'fixed']
512 );
513 $this->info['top'] =
514 $this->info['left'] =
515 $this->info['right'] =
516 $this->info['bottom'] = new HTMLPurifier_AttrDef_CSS_Composite(
517 [
518 new HTMLPurifier_AttrDef_CSS_Length(),
519 new HTMLPurifier_AttrDef_CSS_Percentage(),
520 new HTMLPurifier_AttrDef_Enum(['auto']),
521 ]
522 );
523 $this->info['z-index'] = new HTMLPurifier_AttrDef_CSS_Composite(
524 [
525 new HTMLPurifier_AttrDef_Integer(),
526 new HTMLPurifier_AttrDef_Enum(['auto']),
527 ]
528 );
529 }
530
531 /**
532 * Performs extra config-based processing. Based off of
533 * HTMLPurifier_HTMLDefinition.
534 * @param HTMLPurifier_Config $config
535 * @todo Refactor duplicate elements into common class (probably using
536 * composition, not inheritance).
537 */
538 protected function setupConfigStuff($config)
539 {
540 // setup allowed elements
541 $support = "(for information on implementing this, see the " .
542 "support forums) ";
543 $allowed_properties = $config->get('CSS.AllowedProperties');
544 if ($allowed_properties !== null) {
545 foreach ($this->info as $name => $d) {
546 if (!isset($allowed_properties[$name])) {
547 unset($this->info[$name]);
548 }
549 unset($allowed_properties[$name]);
550 }
551 // emit errors
552 foreach ($allowed_properties as $name => $d) {
553 // :TODO: Is this htmlspecialchars() call really necessary?
554 $name = htmlspecialchars($name);
555 trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
556 }
557 }
558
559 $forbidden_properties = $config->get('CSS.ForbiddenProperties');
560 if ($forbidden_properties !== null) {
561 foreach ($this->info as $name => $d) {
562 if (isset($forbidden_properties[$name])) {
563 unset($this->info[$name]);
564 }
565 }
566 }
567 }
568 }
569
570 // vim: et sw=4 sts=4