friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/AttrDef/CSS/Number.php

85 lines
2.1 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
/**
* Validates a number as defined by the CSS spec.
*/
class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
{
/**
2016-02-09 10:06:17 +00:00
* Indicates whether or not only positive values are allowed.
* @type bool
2010-09-09 03:14:17 +00:00
*/
protected $non_negative = false;
/**
2016-02-09 10:06:17 +00:00
* @param bool $non_negative indicates whether negatives are forbidden
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function __construct($non_negative = false)
{
2010-09-09 03:14:17 +00:00
$this->non_negative = $non_negative;
}
/**
2016-02-09 10:06:17 +00:00
* @param string $number
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return string|bool
2010-09-09 03:14:17 +00:00
* @warning Some contexts do not pass $config, $context. These
* variables should not be used without checking HTMLPurifier_Length
*/
2016-02-09 10:06:17 +00:00
public function validate($number, $config, $context)
{
2010-09-09 03:14:17 +00:00
$number = $this->parseCDATA($number);
2016-02-09 10:06:17 +00:00
if ($number === '') {
return false;
}
if ($number === '0') {
return '0';
}
2010-09-09 03:14:17 +00:00
$sign = '';
switch ($number[0]) {
case '-':
2016-02-09 10:06:17 +00:00
if ($this->non_negative) {
return false;
}
2010-09-09 03:14:17 +00:00
$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
2016-02-09 10:06:17 +00:00
if (strpos($number, '.') === false) {
return false;
}
2010-09-09 03:14:17 +00:00
list($left, $right) = explode('.', $number, 2);
2016-02-09 10:06:17 +00:00
if ($left === '' && $right === '') {
return false;
}
if ($left !== '' && !ctype_digit($left)) {
return false;
}
2010-09-09 03:14:17 +00:00
2016-02-09 10:06:17 +00:00
$left = ltrim($left, '0');
2010-09-09 03:14:17 +00:00
$right = rtrim($right, '0');
if ($right === '') {
return $left ? $sign . $left : '0';
} elseif (!ctype_digit($right)) {
return false;
}
return $sign . $left . '.' . $right;
}
}
// vim: et sw=4 sts=4