2006-08-13 22:35:55 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates a number as defined by the CSS spec.
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
|
2006-08-13 22:35:55 +00:00
|
|
|
{
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Bool indicating whether or not only positive values allowed.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $non_negative = false;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* @param $non_negative Bool indicating whether negatives are forbidden
|
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct($non_negative = false) {
|
2006-08-13 23:08:38 +00:00
|
|
|
$this->non_negative = $non_negative;
|
|
|
|
}
|
|
|
|
|
2008-05-20 21:23:38 +00:00
|
|
|
/**
|
|
|
|
* @warning Some contexts do not pass $config, $context. These
|
|
|
|
* variables should not be used without checking HTMLPurifier_Length
|
|
|
|
*/
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($number, $config, $context) {
|
2006-08-13 22:35:55 +00:00
|
|
|
|
|
|
|
$number = $this->parseCDATA($number);
|
|
|
|
|
|
|
|
if ($number === '') return false;
|
2007-12-16 23:16:45 +00:00
|
|
|
if ($number === '0') return '0';
|
2006-08-13 22:35:55 +00:00
|
|
|
|
|
|
|
$sign = '';
|
|
|
|
switch ($number[0]) {
|
|
|
|
case '-':
|
2006-08-13 23:08:38 +00:00
|
|
|
if ($this->non_negative) return false;
|
2006-08-13 22:35:55 +00:00
|
|
|
$sign = '-';
|
|
|
|
case '+':
|
|
|
|
$number = substr($number, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctype_digit($number)) {
|
|
|
|
$number = ltrim($number, '0');
|
|
|
|
return $number ? $sign . $number : '0';
|
|
|
|
}
|
2007-12-16 23:16:45 +00:00
|
|
|
|
|
|
|
// Period is the only non-numeric character allowed
|
|
|
|
if (strpos($number, '.') === false) return false;
|
2006-08-13 22:35:55 +00:00
|
|
|
|
|
|
|
list($left, $right) = explode('.', $number, 2);
|
|
|
|
|
2007-12-16 23:16:45 +00:00
|
|
|
if ($left === '' && $right === '') return false;
|
|
|
|
if ($left !== '' && !ctype_digit($left)) return false;
|
2006-08-13 22:35:55 +00:00
|
|
|
|
2007-12-16 23:16:45 +00:00
|
|
|
$left = ltrim($left, '0');
|
2006-08-13 22:35:55 +00:00
|
|
|
$right = rtrim($right, '0');
|
|
|
|
|
|
|
|
if ($right === '') {
|
|
|
|
return $left ? $sign . $left : '0';
|
|
|
|
} elseif (!ctype_digit($right)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sign . $left . '.' . $right;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|