Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  /**

   4   * Decorator which enables !important to be used in CSS values.

   5   */
   6  class HTMLPurifier_AttrDef_CSS_ImportantDecorator extends HTMLPurifier_AttrDef
   7  {
   8      /**

   9       * @type HTMLPurifier_AttrDef

  10       */
  11      public $def;
  12      /**

  13       * @type bool

  14       */
  15      public $allow;
  16  
  17      /**

  18       * @param HTMLPurifier_AttrDef $def Definition to wrap

  19       * @param bool $allow Whether or not to allow !important

  20       */
  21      public function __construct($def, $allow = false)
  22      {
  23          $this->def = $def;
  24          $this->allow = $allow;
  25      }
  26  
  27      /**

  28       * Intercepts and removes !important if necessary

  29       * @param string $string

  30       * @param HTMLPurifier_Config $config

  31       * @param HTMLPurifier_Context $context

  32       * @return bool|string

  33       */
  34      public function validate($string, $config, $context)
  35      {
  36          // test for ! and important tokens

  37          $string = trim($string);
  38          $is_important = false;
  39          // :TODO: optimization: test directly for !important and ! important

  40          if (strlen($string) >= 9 && substr($string, -9) === 'important') {
  41              $temp = rtrim(substr($string, 0, -9));
  42              // use a temp, because we might want to restore important

  43              if (strlen($temp) >= 1 && substr($temp, -1) === '!') {
  44                  $string = rtrim(substr($temp, 0, -1));
  45                  $is_important = true;
  46              }
  47          }
  48          $string = $this->def->validate($string, $config, $context);
  49          if ($this->allow && $is_important) {
  50              $string .= ' !important';
  51          }
  52          return $string;
  53      }
  54  }
  55  
  56  // vim: et sw=4 sts=4