File indexing completed on 2024-12-22 05:36:18

0001 <?php
0002 
0003 /**
0004  * Represents a Length as defined by CSS.
0005  */
0006 class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef
0007 {
0008 
0009     /**
0010      * @type HTMLPurifier_Length|string
0011      */
0012     protected $min;
0013 
0014     /**
0015      * @type HTMLPurifier_Length|string
0016      */
0017     protected $max;
0018 
0019     /**
0020      * @param HTMLPurifier_Length|string $min Minimum length, or null for no bound. String is also acceptable.
0021      * @param HTMLPurifier_Length|string $max Maximum length, or null for no bound. String is also acceptable.
0022      */
0023     public function __construct($min = null, $max = null)
0024     {
0025         $this->min = $min !== null ? HTMLPurifier_Length::make($min) : null;
0026         $this->max = $max !== null ? HTMLPurifier_Length::make($max) : null;
0027     }
0028 
0029     /**
0030      * @param string $string
0031      * @param HTMLPurifier_Config $config
0032      * @param HTMLPurifier_Context $context
0033      * @return bool|string
0034      */
0035     public function validate($string, $config, $context)
0036     {
0037         $string = $this->parseCDATA($string);
0038 
0039         // Optimizations
0040         if ($string === '') {
0041             return false;
0042         }
0043         if ($string === '0') {
0044             return '0';
0045         }
0046         if (strlen($string) === 1) {
0047             return false;
0048         }
0049 
0050         $length = HTMLPurifier_Length::make($string);
0051         if (!$length->isValid()) {
0052             return false;
0053         }
0054 
0055         if ($this->min) {
0056             $c = $length->compareTo($this->min);
0057             if ($c === false) {
0058                 return false;
0059             }
0060             if ($c < 0) {
0061                 return false;
0062             }
0063         }
0064         if ($this->max) {
0065             $c = $length->compareTo($this->max);
0066             if ($c === false) {
0067                 return false;
0068             }
0069             if ($c > 0) {
0070                 return false;
0071             }
0072         }
0073         return $length->toString();
0074     }
0075 }
0076 
0077 // vim: et sw=4 sts=4