Don't lower-case components of background.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS / Background.php
blobe5b7438c21517ddfec6e87943e061076130c4c72
1 <?php
3 /**
4 * Validates shorthand CSS property background.
5 * @warning Does not support url tokens that have internal spaces.
6 */
7 class HTMLPurifier_AttrDef_CSS_Background extends HTMLPurifier_AttrDef
10 /**
11 * Local copy of component validators.
12 * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
14 protected $info;
16 public function __construct($config) {
17 $def = $config->getCSSDefinition();
18 $this->info['background-color'] = $def->info['background-color'];
19 $this->info['background-image'] = $def->info['background-image'];
20 $this->info['background-repeat'] = $def->info['background-repeat'];
21 $this->info['background-attachment'] = $def->info['background-attachment'];
22 $this->info['background-position'] = $def->info['background-position'];
25 public function validate($string, $config, $context) {
27 // regular pre-processing
28 $string = $this->parseCDATA($string);
29 if ($string === '') return false;
31 // munge rgb() decl if necessary
32 $string = $this->mungeRgb($string);
34 // assumes URI doesn't have spaces in it
35 $bits = explode(' ', $string); // bits to process
37 $caught = array();
38 $caught['color'] = false;
39 $caught['image'] = false;
40 $caught['repeat'] = false;
41 $caught['attachment'] = false;
42 $caught['position'] = false;
44 $i = 0; // number of catches
45 $none = false;
47 foreach ($bits as $bit) {
48 if ($bit === '') continue;
49 foreach ($caught as $key => $status) {
50 if ($key != 'position') {
51 if ($status !== false) continue;
52 $r = $this->info['background-' . $key]->validate($bit, $config, $context);
53 } else {
54 $r = $bit;
56 if ($r === false) continue;
57 if ($key == 'position') {
58 if ($caught[$key] === false) $caught[$key] = '';
59 $caught[$key] .= $r . ' ';
60 } else {
61 $caught[$key] = $r;
63 $i++;
64 break;
68 if (!$i) return false;
69 if ($caught['position'] !== false) {
70 $caught['position'] = $this->info['background-position']->
71 validate($caught['position'], $config, $context);
74 $ret = array();
75 foreach ($caught as $value) {
76 if ($value === false) continue;
77 $ret[] = $value;
80 if (empty($ret)) return false;
81 return implode(' ', $ret);
87 // vim: et sw=4 sts=4