Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS / BackgroundPosition.php
blob0d10ab681deb8927f2784e0510f306def3247f98
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/CSS/Length.php';
5 require_once 'HTMLPurifier/AttrDef/CSS/Percentage.php';
7 /* W3C says:
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
17 ] |
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
22 left | center | right
23 ] ||
25 top | center | bottom
28 top, left = 0%
29 center, (none) = 50%
30 bottom, right = 100%
33 /* QuirksMode says:
34 keyword + length/percentage must be ordered correctly, as per W3C
36 Internet Explorer and Opera, however, support arbitrary ordering. We
37 should fix it up.
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
45 /**
46 * Validates the value of background-position.
48 class HTMLPurifier_AttrDef_CSS_BackgroundPosition extends HTMLPurifier_AttrDef
51 var $length;
52 var $percentage;
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);
63 $keywords = array();
64 $keywords['h'] = false; // left, right
65 $keywords['v'] = false; // top, bottom
66 $keywords['c'] = false; // center
67 $measures = array();
69 $i = 0;
71 $lookup = array(
72 'top' => 'v',
73 'bottom' => 'v',
74 'left' => 'h',
75 'right' => 'h',
76 'center' => 'c'
79 foreach ($bits as $bit) {
80 if ($bit === '') continue;
82 // test for keyword
83 $lbit = ctype_lower($bit) ? $bit : strtolower($bit);
84 if (isset($lookup[$lbit])) {
85 $status = $lookup[$lbit];
86 $keywords[$status] = $lbit;
87 $i++;
90 // test for length
91 $r = $this->length->validate($bit, $config, $context);
92 if ($r !== false) {
93 $measures[] = $r;
94 $i++;
97 // test for percentage
98 $r = $this->percentage->validate($bit, $config, $context);
99 if ($r !== false) {
100 $measures[] = $r;
101 $i++;
106 if (!$i) return false; // no valid values were caught
109 $ret = array();
111 // first keyword
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);