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-25 02:24:39 +00:00
|
|
|
public function HTMLPurifier_AttrDef_CSS_Number($non_negative = false) {
|
2006-08-13 23:08:38 +00:00
|
|
|
$this->non_negative = $non_negative;
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function validate($number, $config, &$context) {
|
2006-08-13 22:35:55 +00:00
|
|
|
|
|
|
|
$number = $this->parseCDATA($number);
|
|
|
|
|
|
|
|
if ($number === '') return false;
|
|
|
|
|
|
|
|
$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';
|
|
|
|
}
|
|
|
|
if (!strpos($number, '.')) return false;
|
|
|
|
|
|
|
|
list($left, $right) = explode('.', $number, 2);
|
|
|
|
|
|
|
|
if (!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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|