3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/CSS/Length.php';
5 require_once 'HTMLPurifier/AttrDef/CSS/Percentage.php';
8 [ // adjective and number must be in correct order, even if
9 // you could switch them without introducing ambiguity.
10 // some browsers support that syntax
12 <percentage> | <length> | left | center | right
15 <percentage> | <length> | top | center | bottom
18 [ // this signifies that the vertical and horizontal adjectives
19 // can be arbitrarily ordered, however, there can only be two,
20 // one of each, or none at all
34 keyword + length/percentage must be ordered correctly, as per W3C
36 Internet Explorer and Opera, however, support arbitrary ordering. We
39 Minor issue though, not strictly necessary.
42 // control freaks may appreciate the ability to convert these to
43 // percentages or something, but it's not necessary
46 * Validates the value of background-position.
48 class HTMLPurifier_AttrDef_CSS_BackgroundPosition
extends HTMLPurifier_AttrDef
54 function HTMLPurifier_AttrDef_CSS_BackgroundPosition() {
55 $this->length
= new HTMLPurifier_AttrDef_CSS_Length();
56 $this->percentage
= new HTMLPurifier_AttrDef_CSS_Percentage();
59 function validate($string, $config, &$context) {
60 $string = $this->parseCDATA($string);
61 $bits = explode(' ', $string);
64 $keywords['h'] = false; // left, right
65 $keywords['v'] = false; // top, bottom
66 $keywords['c'] = false; // center
79 foreach ($bits as $bit) {
80 if ($bit === '') continue;
83 $lbit = ctype_lower($bit) ?
$bit : strtolower($bit);
84 if (isset($lookup[$lbit])) {
85 $status = $lookup[$lbit];
86 $keywords[$status] = $lbit;
91 $r = $this->length
->validate($bit, $config, $context);
97 // test for percentage
98 $r = $this->percentage
->validate($bit, $config, $context);
106 if (!$i) return false; // no valid values were caught
112 if ($keywords['h']) $ret[] = $keywords['h'];
113 elseif (count($measures)) $ret[] = array_shift($measures);
114 elseif ($keywords['c']) {
115 $ret[] = $keywords['c'];
116 $keywords['c'] = false; // prevent re-use: center = center center
119 if ($keywords['v']) $ret[] = $keywords['v'];
120 elseif (count($measures)) $ret[] = array_shift($measures);
121 elseif ($keywords['c']) $ret[] = $keywords['c'];
123 if (empty($ret)) return false;
124 return implode(' ', $ret);