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

0001 <?php
0002 
0003 /**
0004  * Validates the HTML type length (not to be confused with CSS's length).
0005  *
0006  * This accepts integer pixels or percentages as lengths for certain
0007  * HTML attributes.
0008  */
0009 
0010 class HTMLPurifier_AttrDef_HTML_Length extends HTMLPurifier_AttrDef_HTML_Pixels
0011 {
0012 
0013     /**
0014      * @param string $string
0015      * @param HTMLPurifier_Config $config
0016      * @param HTMLPurifier_Context $context
0017      * @return bool|string
0018      */
0019     public function validate($string, $config, $context)
0020     {
0021         $string = trim($string);
0022         if ($string === '') {
0023             return false;
0024         }
0025 
0026         $parent_result = parent::validate($string, $config, $context);
0027         if ($parent_result !== false) {
0028             return $parent_result;
0029         }
0030 
0031         $length = strlen($string);
0032         $last_char = $string[$length - 1];
0033 
0034         if ($last_char !== '%') {
0035             return false;
0036         }
0037 
0038         $points = substr($string, 0, $length - 1);
0039 
0040         if (!is_numeric($points)) {
0041             return false;
0042         }
0043 
0044         $points = (int)$points;
0045 
0046         if ($points < 0) {
0047             return '0%';
0048         }
0049         if ($points > 100) {
0050             return '100%';
0051         }
0052         return ((string)$points) . '%';
0053     }
0054 }
0055 
0056 // vim: et sw=4 sts=4