Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php\r |
2 | \r | |
3 | /* W3C says:\r | |
4 | [ // adjective and number must be in correct order, even if\r | |
5 | // you could switch them without introducing ambiguity.\r | |
6 | // some browsers support that syntax\r | |
7 | [\r | |
8 | <percentage> | <length> | left | center | right\r | |
9 | ]\r | |
10 | [\r | |
11 | <percentage> | <length> | top | center | bottom\r | |
12 | ]?\r | |
13 | ] |\r | |
14 | [ // this signifies that the vertical and horizontal adjectives\r | |
15 | // can be arbitrarily ordered, however, there can only be two,\r | |
16 | // one of each, or none at all\r | |
17 | [\r | |
18 | left | center | right\r | |
19 | ] ||\r | |
20 | [\r | |
21 | top | center | bottom\r | |
22 | ]\r | |
23 | ]\r | |
24 | top, left = 0%\r | |
25 | center, (none) = 50%\r | |
26 | bottom, right = 100%\r | |
27 | */\r | |
28 | \r | |
29 | /* QuirksMode says:\r | |
30 | keyword + length/percentage must be ordered correctly, as per W3C\r | |
31 | \r | |
32 | Internet Explorer and Opera, however, support arbitrary ordering. We\r | |
33 | should fix it up.\r | |
34 | \r | |
35 | Minor issue though, not strictly necessary.\r | |
36 | */\r | |
37 | \r | |
38 | // control freaks may appreciate the ability to convert these to\r | |
39 | // percentages or something, but it's not necessary\r | |
40 | \r | |
41 | /**\r | |
42 | * Validates the value of background-position.\r | |
43 | */\r | |
44 | class HTMLPurifier_AttrDef_CSS_BackgroundPosition extends HTMLPurifier_AttrDef\r | |
45 | {\r | |
46 | \r | |
47 | protected $length;\r | |
48 | protected $percentage;\r | |
49 | \r | |
50 | public function __construct() {\r | |
51 | $this->length = new HTMLPurifier_AttrDef_CSS_Length();\r | |
52 | $this->percentage = new HTMLPurifier_AttrDef_CSS_Percentage();\r | |
53 | }\r | |
54 | \r | |
55 | public function validate($string, $config, $context) {\r | |
56 | $string = $this->parseCDATA($string);\r | |
57 | $bits = explode(' ', $string);\r | |
58 | \r | |
59 | $keywords = array();\r | |
60 | $keywords['h'] = false; // left, right\r | |
61 | $keywords['v'] = false; // top, bottom\r | |
62 | $keywords['ch'] = false; // center (first word)\r | |
63 | $keywords['cv'] = false; // center (second word)\r | |
64 | $measures = array();\r | |
65 | \r | |
66 | $i = 0;\r | |
67 | \r | |
68 | $lookup = array(\r | |
69 | 'top' => 'v',\r | |
70 | 'bottom' => 'v',\r | |
71 | 'left' => 'h',\r | |
72 | 'right' => 'h',\r | |
73 | 'center' => 'c'\r | |
74 | );\r | |
75 | \r | |
76 | foreach ($bits as $bit) {\r | |
77 | if ($bit === '') continue;\r | |
78 | \r | |
79 | // test for keyword\r | |
80 | $lbit = ctype_lower($bit) ? $bit : strtolower($bit);\r | |
81 | if (isset($lookup[$lbit])) {\r | |
82 | $status = $lookup[$lbit];\r | |
83 | if ($status == 'c') {\r | |
84 | if ($i == 0) {\r | |
85 | $status = 'ch';\r | |
86 | } else {\r | |
87 | $status = 'cv';\r | |
88 | }\r | |
89 | }\r | |
90 | $keywords[$status] = $lbit;\r | |
91 | $i++;\r | |
92 | }\r | |
93 | \r | |
94 | // test for length\r | |
95 | $r = $this->length->validate($bit, $config, $context);\r | |
96 | if ($r !== false) {\r | |
97 | $measures[] = $r;\r | |
98 | $i++;\r | |
99 | }\r | |
100 | \r | |
101 | // test for percentage\r | |
102 | $r = $this->percentage->validate($bit, $config, $context);\r | |
103 | if ($r !== false) {\r | |
104 | $measures[] = $r;\r | |
105 | $i++;\r | |
106 | }\r | |
107 | \r | |
108 | }\r | |
109 | \r | |
110 | if (!$i) return false; // no valid values were caught\r | |
111 | \r | |
112 | $ret = array();\r | |
113 | \r | |
114 | // first keyword\r | |
115 | if ($keywords['h']) $ret[] = $keywords['h'];\r | |
116 | elseif ($keywords['ch']) {\r | |
117 | $ret[] = $keywords['ch'];\r | |
118 | $keywords['cv'] = false; // prevent re-use: center = center center\r | |
119 | }\r | |
120 | elseif (count($measures)) $ret[] = array_shift($measures);\r | |
121 | \r | |
122 | if ($keywords['v']) $ret[] = $keywords['v'];\r | |
123 | elseif ($keywords['cv']) $ret[] = $keywords['cv'];\r | |
124 | elseif (count($measures)) $ret[] = array_shift($measures);\r | |
125 | \r | |
126 | if (empty($ret)) return false;\r | |
127 | return implode(' ', $ret);\r | |
128 | \r | |
129 | }\r | |
130 | \r | |
131 | }\r | |
132 | \r | |
133 | // vim: et sw=4 sts=4\r |