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-21 01:56:48 +00:00
|
|
|
protected $min, $max;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
2008-05-21 01:56:48 +00:00
|
|
|
* @param HTMLPurifier_Length $max Minimum length, or null for no bound. String is also acceptable.
|
|
|
|
* @param HTMLPurifier_Length $max Maximum length, or null for no bound. String is also acceptable.
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2008-05-21 01:56:48 +00:00
|
|
|
public function __construct($min = null, $max = null) {
|
|
|
|
$this->min = $min !== null ? HTMLPurifier_Length::make($min) : null;
|
|
|
|
$this->max = $max !== null ? HTMLPurifier_Length::make($max) : null;
|
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);
|
2008-05-21 01:56:48 +00:00
|
|
|
if (!$length->isValid()) return false;
|
2006-08-13 23:08:38 +00:00
|
|
|
|
2008-05-21 01:56:48 +00:00
|
|
|
if ($this->min) {
|
|
|
|
$c = $length->compareTo($this->min);
|
|
|
|
if ($c === false) return false;
|
|
|
|
if ($c < 0) return false;
|
|
|
|
}
|
|
|
|
if ($this->max) {
|
|
|
|
$c = $length->compareTo($this->max);
|
|
|
|
if ($c === false) return false;
|
|
|
|
if ($c > 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|