library/HTMLPurifier/AttrDef/CSS/BackgroundPosition.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /* W3C says:
00004     [ // adjective and number must be in correct order, even if
00005       // you could switch them without introducing ambiguity.
00006       // some browsers support that syntax
00007         [
00008             <percentage> | <length> | left | center | right
00009         ]
00010         [ 
00011             <percentage> | <length> | top | center | bottom
00012         ]?
00013     ] |
00014     [ // this signifies that the vertical and horizontal adjectives
00015       // can be arbitrarily ordered, however, there can only be two,
00016       // one of each, or none at all
00017         [
00018             left | center | right
00019         ] ||
00020         [
00021             top | center | bottom
00022         ]
00023     ]
00024     top, left = 0%
00025     center, (none) = 50%
00026     bottom, right = 100%
00027 */
00028 
00029 /* QuirksMode says:
00030     keyword + length/percentage must be ordered correctly, as per W3C
00031     
00032     Internet Explorer and Opera, however, support arbitrary ordering. We
00033     should fix it up.
00034     
00035     Minor issue though, not strictly necessary.
00036 */
00037 
00038 // control freaks may appreciate the ability to convert these to
00039 // percentages or something, but it's not necessary
00040 
00044 class HTMLPurifier_AttrDef_CSS_BackgroundPosition extends HTMLPurifier_AttrDef
00045 {
00046     
00047     protected $length;
00048     protected $percentage;
00049     
00050     public function __construct() {
00051         $this->length     = new HTMLPurifier_AttrDef_CSS_Length();
00052         $this->percentage = new HTMLPurifier_AttrDef_CSS_Percentage();
00053     }
00054     
00055     public function validate($string, $config, $context) {
00056         $string = $this->parseCDATA($string);
00057         $bits = explode(' ', $string);
00058         
00059         $keywords = array();
00060         $keywords['h'] = false; // left, right
00061         $keywords['v'] = false; // top, bottom
00062         $keywords['c'] = false; // center
00063         $measures = array();
00064         
00065         $i = 0;
00066         
00067         $lookup = array(
00068             'top' => 'v',
00069             'bottom' => 'v',
00070             'left' => 'h',
00071             'right' => 'h',
00072             'center' => 'c'
00073         );
00074         
00075         foreach ($bits as $bit) {
00076             if ($bit === '') continue;
00077             
00078             // test for keyword
00079             $lbit = ctype_lower($bit) ? $bit : strtolower($bit);
00080             if (isset($lookup[$lbit])) {
00081                 $status = $lookup[$lbit];
00082                 $keywords[$status] = $lbit;
00083                 $i++;
00084             }
00085             
00086             // test for length
00087             $r = $this->length->validate($bit, $config, $context);
00088             if ($r !== false) {
00089                 $measures[] = $r;
00090                 $i++;
00091             }
00092             
00093             // test for percentage
00094             $r = $this->percentage->validate($bit, $config, $context);
00095             if ($r !== false) {
00096                 $measures[] = $r;
00097                 $i++;
00098             }
00099             
00100         }
00101         
00102         if (!$i) return false; // no valid values were caught
00103         
00104         
00105         $ret = array();
00106         
00107         // first keyword
00108         if     ($keywords['h'])     $ret[] = $keywords['h'];
00109         elseif (count($measures))   $ret[] = array_shift($measures);
00110         elseif ($keywords['c']) {
00111             $ret[] = $keywords['c'];
00112             $keywords['c'] = false; // prevent re-use: center = center center
00113         }
00114         
00115         if     ($keywords['v'])     $ret[] = $keywords['v'];
00116         elseif (count($measures))   $ret[] = array_shift($measures);
00117         elseif ($keywords['c'])     $ret[] = $keywords['c'];
00118         
00119         if (empty($ret)) return false;
00120         return implode(' ', $ret);
00121         
00122     }
00123     
00124 }
00125 

Generated on Thu Jun 19 18:47:25 2008 for HTMLPurifier by  doxygen 1.5.3