ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
BackgroundPosition.php
Go to the documentation of this file.
1 <?php
2 
3 /* W3C says:
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
7  [
8  <percentage> | <length> | left | center | right
9  ]
10  [
11  <percentage> | <length> | top | center | bottom
12  ]?
13  ] |
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
17  [
18  left | center | right
19  ] ||
20  [
21  top | center | bottom
22  ]
23  ]
24  top, left = 0%
25  center, (none) = 50%
26  bottom, right = 100%
27 */
28 
29 /* QuirksMode says:
30  keyword + length/percentage must be ordered correctly, as per W3C
31 
32  Internet Explorer and Opera, however, support arbitrary ordering. We
33  should fix it up.
34 
35  Minor issue though, not strictly necessary.
36 */
37 
38 // control freaks may appreciate the ability to convert these to
39 // percentages or something, but it's not necessary
40 
45 {
46 
47  protected $length;
48  protected $percentage;
49 
50  public function __construct() {
51  $this->length = new HTMLPurifier_AttrDef_CSS_Length();
52  $this->percentage = new HTMLPurifier_AttrDef_CSS_Percentage();
53  }
54 
55  public function validate($string, $config, $context) {
56  $string = $this->parseCDATA($string);
57  $bits = explode(' ', $string);
58 
59  $keywords = array();
60  $keywords['h'] = false; // left, right
61  $keywords['v'] = false; // top, bottom
62  $keywords['ch'] = false; // center (first word)
63  $keywords['cv'] = false; // center (second word)
64  $measures = array();
65 
66  $i = 0;
67 
68  $lookup = array(
69  'top' => 'v',
70  'bottom' => 'v',
71  'left' => 'h',
72  'right' => 'h',
73  'center' => 'c'
74  );
75 
76  foreach ($bits as $bit) {
77  if ($bit === '') continue;
78 
79  // test for keyword
80  $lbit = ctype_lower($bit) ? $bit : strtolower($bit);
81  if (isset($lookup[$lbit])) {
82  $status = $lookup[$lbit];
83  if ($status == 'c') {
84  if ($i == 0) {
85  $status = 'ch';
86  } else {
87  $status = 'cv';
88  }
89  }
90  $keywords[$status] = $lbit;
91  $i++;
92  }
93 
94  // test for length
95  $r = $this->length->validate($bit, $config, $context);
96  if ($r !== false) {
97  $measures[] = $r;
98  $i++;
99  }
100 
101  // test for percentage
102  $r = $this->percentage->validate($bit, $config, $context);
103  if ($r !== false) {
104  $measures[] = $r;
105  $i++;
106  }
107 
108  }
109 
110  if (!$i) return false; // no valid values were caught
111 
112  $ret = array();
113 
114  // first keyword
115  if ($keywords['h']) $ret[] = $keywords['h'];
116  elseif ($keywords['ch']) {
117  $ret[] = $keywords['ch'];
118  $keywords['cv'] = false; // prevent re-use: center = center center
119  }
120  elseif (count($measures)) $ret[] = array_shift($measures);
121 
122  if ($keywords['v']) $ret[] = $keywords['v'];
123  elseif ($keywords['cv']) $ret[] = $keywords['cv'];
124  elseif (count($measures)) $ret[] = array_shift($measures);
125 
126  if (empty($ret)) return false;
127  return implode(' ', $ret);
128 
129  }
130 
131 }
132 
133 // vim: et sw=4 sts=4