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

0001 <?php
0002 
0003 /**
0004  * Decorator which enables !important to be used in CSS values.
0005  */
0006 class HTMLPurifier_AttrDef_CSS_ImportantDecorator extends HTMLPurifier_AttrDef
0007 {
0008     /**
0009      * @type HTMLPurifier_AttrDef
0010      */
0011     public $def;
0012     /**
0013      * @type bool
0014      */
0015     public $allow;
0016 
0017     /**
0018      * @param HTMLPurifier_AttrDef $def Definition to wrap
0019      * @param bool $allow Whether or not to allow !important
0020      */
0021     public function __construct($def, $allow = false)
0022     {
0023         $this->def = $def;
0024         $this->allow = $allow;
0025     }
0026 
0027     /**
0028      * Intercepts and removes !important if necessary
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         // test for ! and important tokens
0037         $string = trim($string);
0038         $is_important = false;
0039         // :TODO: optimization: test directly for !important and ! important
0040         if (strlen($string) >= 9 && substr($string, -9) === 'important') {
0041             $temp = rtrim(substr($string, 0, -9));
0042             // use a temp, because we might want to restore important
0043             if (strlen($temp) >= 1 && substr($temp, -1) === '!') {
0044                 $string = rtrim(substr($temp, 0, -1));
0045                 $is_important = true;
0046             }
0047         }
0048         $string = $this->def->validate($string, $config, $context);
0049         if ($this->allow && $is_important) {
0050             $string .= ' !important';
0051         }
0052         return $string;
0053     }
0054 }
0055 
0056 // vim: et sw=4 sts=4