Source for file BackgroundPosition.php

Documentation is available at BackgroundPosition.php

  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.  
  41. /**
  42.  * Validates the value of background-position.
  43.  */
  44. {
  45.     
  46.     protected $length;
  47.     protected $percentage;
  48.     
  49.     public function __construct({
  50.         $this->length     = new HTMLPurifier_AttrDef_CSS_Length();
  51.         $this->percentage = new HTMLPurifier_AttrDef_CSS_Percentage();
  52.     }
  53.     
  54.     public function validate($string$config$context{
  55.         $string $this->parseCDATA($string);
  56.         $bits explode(' '$string);
  57.         
  58.         $keywords array();
  59.         $keywords['h'false// left, right
  60.         $keywords['v'false// top, bottom
  61.         $keywords['c'false// center
  62.         $measures array();
  63.         
  64.         $i 0;
  65.         
  66.         $lookup array(
  67.             'top' => 'v',
  68.             'bottom' => 'v',
  69.             'left' => 'h',
  70.             'right' => 'h',
  71.             'center' => 'c'
  72.         );
  73.         
  74.         foreach ($bits as $bit{
  75.             if ($bit === ''continue;
  76.             
  77.             // test for keyword
  78.             $lbit ctype_lower($bit$bit strtolower($bit);
  79.             if (isset($lookup[$lbit])) {
  80.                 $status $lookup[$lbit];
  81.                 $keywords[$status$lbit;
  82.                 $i++;
  83.             }
  84.             
  85.             // test for length
  86.             $r $this->length->validate($bit$config$context);
  87.             if ($r !== false{
  88.                 $measures[$r;
  89.                 $i++;
  90.             }
  91.             
  92.             // test for percentage
  93.             $r $this->percentage->validate($bit$config$context);
  94.             if ($r !== false{
  95.                 $measures[$r;
  96.                 $i++;
  97.             }
  98.             
  99.         }
  100.         
  101.         if (!$ireturn false// no valid values were caught
  102.         
  103.         
  104.         $ret array();
  105.         
  106.         // first keyword
  107.         if     ($keywords['h'])     $ret[$keywords['h'];
  108.         elseif (count($measures))   $ret[array_shift($measures);
  109.         elseif ($keywords['c']{
  110.             $ret[$keywords['c'];
  111.             $keywords['c'false// prevent re-use: center = center center
  112.         }
  113.         
  114.         if     ($keywords['v'])     $ret[$keywords['v'];
  115.         elseif (count($measures))   $ret[array_shift($measures);
  116.         elseif ($keywords['c'])     $ret[$keywords['c'];
  117.         
  118.         if (empty($ret)) return false;
  119.         return implode(' '$ret);
  120.         
  121.     }
  122.     
  123. }

Documentation generated on Thu, 19 Jun 2008 18:48:52 -0400 by phpDocumentor 1.4.2