7f1ea3b0f1debcbf497b5e284b3bf3b96bb39ccf
[yaffs-website] / vendor / ezyang / htmlpurifier / library / HTMLPurifier / AttrDef / CSS / Background.php
1 <?php
2
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
8 {
9
10     /**
11      * Local copy of component validators.
12      * @type HTMLPurifier_AttrDef[]
13      * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
14      */
15     protected $info;
16
17     /**
18      * @param HTMLPurifier_Config $config
19      */
20     public function __construct($config)
21     {
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'];
28     }
29
30     /**
31      * @param string $string
32      * @param HTMLPurifier_Config $config
33      * @param HTMLPurifier_Context $context
34      * @return bool|string
35      */
36     public function validate($string, $config, $context)
37     {
38         // regular pre-processing
39         $string = $this->parseCDATA($string);
40         if ($string === '') {
41             return false;
42         }
43
44         // munge rgb() decl if necessary
45         $string = $this->mungeRgb($string);
46
47         // assumes URI doesn't have spaces in it
48         $bits = explode(' ', $string); // bits to process
49
50         $caught = array();
51         $caught['color'] = false;
52         $caught['image'] = false;
53         $caught['repeat'] = false;
54         $caught['attachment'] = false;
55         $caught['position'] = false;
56
57         $i = 0; // number of catches
58
59         foreach ($bits as $bit) {
60             if ($bit === '') {
61                 continue;
62             }
63             foreach ($caught as $key => $status) {
64                 if ($key != 'position') {
65                     if ($status !== false) {
66                         continue;
67                     }
68                     $r = $this->info['background-' . $key]->validate($bit, $config, $context);
69                 } else {
70                     $r = $bit;
71                 }
72                 if ($r === false) {
73                     continue;
74                 }
75                 if ($key == 'position') {
76                     if ($caught[$key] === false) {
77                         $caught[$key] = '';
78                     }
79                     $caught[$key] .= $r . ' ';
80                 } else {
81                     $caught[$key] = $r;
82                 }
83                 $i++;
84                 break;
85             }
86         }
87
88         if (!$i) {
89             return false;
90         }
91         if ($caught['position'] !== false) {
92             $caught['position'] = $this->info['background-position']->
93                 validate($caught['position'], $config, $context);
94         }
95
96         $ret = array();
97         foreach ($caught as $value) {
98             if ($value === false) {
99                 continue;
100             }
101             $ret[] = $value;
102         }
103
104         if (empty($ret)) {
105             return false;
106         }
107         return implode(' ', $ret);
108     }
109 }
110
111 // vim: et sw=4 sts=4