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

0001 <?php
0002 
0003 /**
0004  * Validates a Percentage as defined by the CSS spec.
0005  */
0006 class HTMLPurifier_AttrDef_CSS_Percentage extends HTMLPurifier_AttrDef
0007 {
0008 
0009     /**
0010      * Instance to defer number validation to.
0011      * @type HTMLPurifier_AttrDef_CSS_Number
0012      */
0013     protected $number_def;
0014 
0015     /**
0016      * @param bool $non_negative Whether to forbid negative values
0017      */
0018     public function __construct($non_negative = false)
0019     {
0020         $this->number_def = new HTMLPurifier_AttrDef_CSS_Number($non_negative);
0021     }
0022 
0023     /**
0024      * @param string $string
0025      * @param HTMLPurifier_Config $config
0026      * @param HTMLPurifier_Context $context
0027      * @return bool|string
0028      */
0029     public function validate($string, $config, $context)
0030     {
0031         $string = $this->parseCDATA($string);
0032 
0033         if ($string === '') {
0034             return false;
0035         }
0036         $length = strlen($string);
0037         if ($length === 1) {
0038             return false;
0039         }
0040         if ($string[$length - 1] !== '%') {
0041             return false;
0042         }
0043 
0044         $number = substr($string, 0, $length - 1);
0045         $number = $this->number_def->validate($number, $config, $context);
0046 
0047         if ($number === false) {
0048             return false;
0049         }
0050         return "$number%";
0051     }
0052 }
0053 
0054 // vim: et sw=4 sts=4