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
|
|
|
{
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Valid unit lookup table.
|
|
|
|
* @warning The code assumes all units are two characters long. Be careful
|
|
|
|
* if we have to change this behavior!
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $units = array('em' => true, 'ex' => true, 'px' => true, 'in' => true,
|
2006-08-13 23:08:38 +00:00
|
|
|
'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true);
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Instance of HTMLPurifier_AttrDef_Number to defer number validation to
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $number_def;
|
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) {
|
2007-02-14 20:38:51 +00:00
|
|
|
$this->number_def = new HTMLPurifier_AttrDef_CSS_Number($non_negative);
|
2006-08-13 23:08:38 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($length, $config, $context) {
|
2006-08-13 23:08:38 +00:00
|
|
|
|
|
|
|
$length = $this->parseCDATA($length);
|
|
|
|
if ($length === '') return false;
|
|
|
|
if ($length === '0') return '0';
|
|
|
|
$strlen = strlen($length);
|
|
|
|
if ($strlen === 1) return false; // impossible!
|
|
|
|
|
|
|
|
// we assume all units are two characters
|
|
|
|
$unit = substr($length, $strlen - 2);
|
2007-01-20 01:40:56 +00:00
|
|
|
if (!ctype_lower($unit)) $unit = strtolower($unit);
|
2006-08-13 23:08:38 +00:00
|
|
|
$number = substr($length, 0, $strlen - 2);
|
|
|
|
|
|
|
|
if (!isset($this->units[$unit])) return false;
|
|
|
|
|
|
|
|
$number = $this->number_def->validate($number, $config, $context);
|
|
|
|
if ($number === false) return false;
|
|
|
|
|
|
|
|
return $number . $unit;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|