File indexing completed on 2024-06-16 05:29:43

0001 <?php
0002 
0003 /**
0004  * Validates shorthand CSS property list-style.
0005  * @warning Does not support url tokens that have internal spaces.
0006  */
0007 class HTMLPurifier_AttrDef_CSS_ListStyle extends HTMLPurifier_AttrDef
0008 {
0009 
0010     /**
0011      * Local copy of validators.
0012      * @type HTMLPurifier_AttrDef[]
0013      * @note See HTMLPurifier_AttrDef_CSS_Font::$info for a similar impl.
0014      */
0015     protected $info;
0016 
0017     /**
0018      * @param HTMLPurifier_Config $config
0019      */
0020     public function __construct($config)
0021     {
0022         $def = $config->getCSSDefinition();
0023         $this->info['list-style-type'] = $def->info['list-style-type'];
0024         $this->info['list-style-position'] = $def->info['list-style-position'];
0025         $this->info['list-style-image'] = $def->info['list-style-image'];
0026     }
0027 
0028     /**
0029      * @param string $string
0030      * @param HTMLPurifier_Config $config
0031      * @param HTMLPurifier_Context $context
0032      * @return bool|string
0033      */
0034     public function validate($string, $config, $context)
0035     {
0036         // regular pre-processing
0037         $string = $this->parseCDATA($string);
0038         if ($string === '') {
0039             return false;
0040         }
0041 
0042         // assumes URI doesn't have spaces in it
0043         $bits = explode(' ', strtolower($string)); // bits to process
0044 
0045         $caught = array();
0046         $caught['type'] = false;
0047         $caught['position'] = false;
0048         $caught['image'] = false;
0049 
0050         $i = 0; // number of catches
0051         $none = false;
0052 
0053         foreach ($bits as $bit) {
0054             if ($i >= 3) {
0055                 return;
0056             } // optimization bit
0057             if ($bit === '') {
0058                 continue;
0059             }
0060             foreach ($caught as $key => $status) {
0061                 if ($status !== false) {
0062                     continue;
0063                 }
0064                 $r = $this->info['list-style-' . $key]->validate($bit, $config, $context);
0065                 if ($r === false) {
0066                     continue;
0067                 }
0068                 if ($r === 'none') {
0069                     if ($none) {
0070                         continue;
0071                     } else {
0072                         $none = true;
0073                     }
0074                     if ($key == 'image') {
0075                         continue;
0076                     }
0077                 }
0078                 $caught[$key] = $r;
0079                 $i++;
0080                 break;
0081             }
0082         }
0083 
0084         if (!$i) {
0085             return false;
0086         }
0087 
0088         $ret = array();
0089 
0090         // construct type
0091         if ($caught['type']) {
0092             $ret[] = $caught['type'];
0093         }
0094 
0095         // construct image
0096         if ($caught['image']) {
0097             $ret[] = $caught['image'];
0098         }
0099 
0100         // construct position
0101         if ($caught['position']) {
0102             $ret[] = $caught['position'];
0103         }
0104 
0105         if (empty($ret)) {
0106             return false;
0107         }
0108         return implode(' ', $ret);
0109     }
0110 }
0111 
0112 // vim: et sw=4 sts=4