HTMLPurifier_AttrDef_CSS_Number::validate PHP Méthode

validate() public méthode

public validate ( string $number, HTMLPurifier_Config $config, HTMLPurifier_Context $context ) : string | boolean
$number string
$config HTMLPurifier_Config
$context HTMLPurifier_Context
Résultat string | boolean
    public function validate($number, $config, $context)
    {
        $number = $this->parseCDATA($number);
        if ($number === '') {
            return false;
        }
        if ($number === '0') {
            return '0';
        }
        $sign = '';
        switch ($number[0]) {
            case '-':
                if ($this->non_negative) {
                    return false;
                }
                $sign = '-';
            case '+':
                $number = substr($number, 1);
        }
        if (ctype_digit($number)) {
            $number = ltrim($number, '0');
            return $number ? $sign . $number : '0';
        }
        // Period is the only non-numeric character allowed
        if (strpos($number, '.') === false) {
            return false;
        }
        list($left, $right) = explode('.', $number, 2);
        if ($left === '' && $right === '') {
            return false;
        }
        if ($left !== '' && !ctype_digit($left)) {
            return false;
        }
        $left = ltrim($left, '0');
        $right = rtrim($right, '0');
        if ($right === '') {
            return $left ? $sign . $left : '0';
        } elseif (!ctype_digit($right)) {
            return false;
        }
        return $sign . $left . '.' . $right;
    }

Usage Example

Exemple #1
0
 /**
  * Validates the number and unit.
  */
 function validate()
 {
     // Special case:
     static $allowedUnits = array('em' => true, 'ex' => true, 'px' => true, 'in' => true, 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true);
     if ($this->n === '+0' || $this->n === '-0') {
         $this->n = '0';
     }
     if ($this->n === '0' && $this->unit === false) {
         return true;
     }
     if (!ctype_lower($this->unit)) {
         $this->unit = strtolower($this->unit);
     }
     if (!isset($allowedUnits[$this->unit])) {
         return false;
     }
     // Hack:
     $def = new HTMLPurifier_AttrDef_CSS_Number();
     $a = false;
     // hack hack
     $result = $def->validate($this->n, $a, $a);
     if ($result === false) {
         return false;
     }
     $this->n = $result;
     return true;
 }
All Usage Examples Of HTMLPurifier_AttrDef_CSS_Number::validate
HTMLPurifier_AttrDef_CSS_Number