HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/AttrDef/CSS/Number.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
00007 {
00008 
00012     protected $non_negative = false;
00013 
00017     public function __construct($non_negative = false) {
00018         $this->non_negative = $non_negative;
00019     }
00020 
00025     public function validate($number, $config, $context) {
00026 
00027         $number = $this->parseCDATA($number);
00028 
00029         if ($number === '') return false;
00030         if ($number === '0') return '0';
00031 
00032         $sign = '';
00033         switch ($number[0]) {
00034             case '-':
00035                 if ($this->non_negative) return false;
00036                 $sign = '-';
00037             case '+':
00038                 $number = substr($number, 1);
00039         }
00040 
00041         if (ctype_digit($number)) {
00042             $number = ltrim($number, '0');
00043             return $number ? $sign . $number : '0';
00044         }
00045 
00046         // Period is the only non-numeric character allowed
00047         if (strpos($number, '.') === false) return false;
00048 
00049         list($left, $right) = explode('.', $number, 2);
00050 
00051         if ($left === '' && $right === '') return false;
00052         if ($left !== '' && !ctype_digit($left)) return false;
00053 
00054         $left  = ltrim($left,  '0');
00055         $right = rtrim($right, '0');
00056 
00057         if ($right === '') {
00058             return $left ? $sign . $left : '0';
00059         } elseif (!ctype_digit($right)) {
00060             return false;
00061         }
00062 
00063         return $sign . $left . '.' . $right;
00064 
00065     }
00066 
00067 }
00068 
00069 // vim: et sw=4 sts=4