PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS / Background.php
blob7f1ea3b0f1debcbf497b5e284b3bf3b96bb39ccf
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 * @type HTMLPurifier_AttrDef[]
13 * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
15 protected $info;
17 /**
18 * @param HTMLPurifier_Config $config
20 public function __construct($config)
22 $def = $config->getCSSDefinition();
23 $this->info['background-color'] = $def->info['background-color'];
24 $this->info['background-image'] = $def->info['background-image'];
25 $this->info['background-repeat'] = $def->info['background-repeat'];
26 $this->info['background-attachment'] = $def->info['background-attachment'];
27 $this->info['background-position'] = $def->info['background-position'];
30 /**
31 * @param string $string
32 * @param HTMLPurifier_Config $config
33 * @param HTMLPurifier_Context $context
34 * @return bool|string
36 public function validate($string, $config, $context)
38 // regular pre-processing
39 $string = $this->parseCDATA($string);
40 if ($string === '') {
41 return false;
44 // munge rgb() decl if necessary
45 $string = $this->mungeRgb($string);
47 // assumes URI doesn't have spaces in it
48 $bits = explode(' ', $string); // bits to process
50 $caught = array();
51 $caught['color'] = false;
52 $caught['image'] = false;
53 $caught['repeat'] = false;
54 $caught['attachment'] = false;
55 $caught['position'] = false;
57 $i = 0; // number of catches
59 foreach ($bits as $bit) {
60 if ($bit === '') {
61 continue;
63 foreach ($caught as $key => $status) {
64 if ($key != 'position') {
65 if ($status !== false) {
66 continue;
68 $r = $this->info['background-' . $key]->validate($bit, $config, $context);
69 } else {
70 $r = $bit;
72 if ($r === false) {
73 continue;
75 if ($key == 'position') {
76 if ($caught[$key] === false) {
77 $caught[$key] = '';
79 $caught[$key] .= $r . ' ';
80 } else {
81 $caught[$key] = $r;
83 $i++;
84 break;
88 if (!$i) {
89 return false;
91 if ($caught['position'] !== false) {
92 $caught['position'] = $this->info['background-position']->
93 validate($caught['position'], $config, $context);
96 $ret = array();
97 foreach ($caught as $value) {
98 if ($value === false) {
99 continue;
101 $ret[] = $value;
104 if (empty($ret)) {
105 return false;
107 return implode(' ', $ret);
111 // vim: et sw=4 sts=4