4 [ // adjective and number must be in correct order, even if
5 // you could switch them without introducing ambiguity.
6 // some browsers support that syntax
8 <percentage> | <length> | left | center | right
11 <percentage> | <length> | top | center | bottom
14 [ // this signifies that the vertical and horizontal adjectives
15 // can be arbitrarily ordered, however, there can only be two,
16 // one of each, or none at all
30 keyword + length/percentage must be ordered correctly, as per W3C
32 Internet Explorer and Opera, however, support arbitrary ordering. We
35 Minor issue though, not strictly necessary.
38 // control freaks may appreciate the ability to convert these to
39 // percentages or something, but it's not necessary
42 * Validates the value of background-position.
44 class HTMLPurifier_AttrDef_CSS_BackgroundPosition extends HTMLPurifier_AttrDef
48 protected $percentage;
50 public function __construct() {
51 $this->length = new HTMLPurifier_AttrDef_CSS_Length();
52 $this->percentage = new HTMLPurifier_AttrDef_CSS_Percentage();
55 public function validate($string, $config, $context) {
56 $string = $this->parseCDATA($string);
57 $bits = explode(' ', $string);
60 $keywords['h'] = false; // left, right
61 $keywords['v'] = false; // top, bottom
62 $keywords['c'] = false; // center
75 foreach ($bits as $bit) {
76 if ($bit === '') continue;
79 $lbit = ctype_lower($bit) ? $bit : strtolower($bit);
80 if (isset($lookup[$lbit])) {
81 $status = $lookup[$lbit];
82 $keywords[$status] = $lbit;
87 $r = $this->length->validate($bit, $config, $context);
93 // test for percentage
94 $r = $this->percentage->validate($bit, $config, $context);
102 if (!$i) return false; // no valid values were caught
108 if ($keywords['h']) $ret[] = $keywords['h'];
109 elseif (count($measures)) $ret[] = array_shift($measures);
110 elseif ($keywords['c']) {
111 $ret[] = $keywords['c'];
112 $keywords['c'] = false; // prevent re-use: center = center center
115 if ($keywords['v']) $ret[] = $keywords['v'];
116 elseif (count($measures)) $ret[] = array_shift($measures);
117 elseif ($keywords['c']) $ret[] = $keywords['c'];
119 if (empty($ret)) return false;
120 return implode(' ', $ret);
126 // vim: et sw=4 sts=4