2006-08-13 23:08:38 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Represents a Length as defined by CSS.
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef
|
2006-08-13 23:08:38 +00:00
|
|
|
{
|
|
|
|
|
2008-05-20 23:15:20 +00:00
|
|
|
protected $nonNegative;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* @param $non_negative Bool indication whether or not negative values are
|
|
|
|
* allowed.
|
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct($non_negative = false) {
|
2008-05-20 23:15:20 +00:00
|
|
|
$this->nonNegative = $non_negative;
|
2006-08-13 23:08:38 +00:00
|
|
|
}
|
|
|
|
|
2008-05-20 23:15:20 +00:00
|
|
|
public function validate($string, $config, $context) {
|
|
|
|
$string = $this->parseCDATA($string);
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2008-05-20 23:15:20 +00:00
|
|
|
// Optimizations
|
|
|
|
if ($string === '') return false;
|
|
|
|
if ($string === '0') return '0';
|
|
|
|
if (strlen($string) === 1) return false;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2008-05-20 23:15:20 +00:00
|
|
|
$length = HTMLPurifier_Length::make($string);
|
|
|
|
if (!$length->isValid($this->nonNegative)) return false;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2008-05-20 23:15:20 +00:00
|
|
|
$n = $length->getN();
|
|
|
|
if ($this->nonNegative && $n < 0) return false;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2008-05-20 23:15:20 +00:00
|
|
|
return $length->toString();
|
2006-08-13 23:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|