| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Represents a Length as defined by CSS. |
|---|
| 5 | */ |
|---|
| 6 | class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef |
|---|
| 7 | { |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * Valid unit lookup table. |
|---|
| 11 | * @warning The code assumes all units are two characters long. Be careful |
|---|
| 12 | * if we have to change this behavior! |
|---|
| 13 | */ |
|---|
| 14 | protected $units = array('em' => true, 'ex' => true, 'px' => true, 'in' => true, |
|---|
| 15 | 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true); |
|---|
| 16 | /** |
|---|
| 17 | * Instance of HTMLPurifier_AttrDef_Number to defer number validation to |
|---|
| 18 | */ |
|---|
| 19 | protected $number_def; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * @param $non_negative Bool indication whether or not negative values are |
|---|
| 23 | * allowed. |
|---|
| 24 | */ |
|---|
| 25 | public function __construct($non_negative = false) { |
|---|
| 26 | $this->number_def = new HTMLPurifier_AttrDef_CSS_Number($non_negative); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public function validate($length, $config, $context) { |
|---|
| 30 | |
|---|
| 31 | $length = $this->parseCDATA($length); |
|---|
| 32 | if ($length === '') return false; |
|---|
| 33 | if ($length === '0') return '0'; |
|---|
| 34 | $strlen = strlen($length); |
|---|
| 35 | if ($strlen === 1) return false; // impossible! |
|---|
| 36 | |
|---|
| 37 | // we assume all units are two characters |
|---|
| 38 | $unit = substr($length, $strlen - 2); |
|---|
| 39 | if (!ctype_lower($unit)) $unit = strtolower($unit); |
|---|
| 40 | $number = substr($length, 0, $strlen - 2); |
|---|
| 41 | |
|---|
| 42 | if (!isset($this->units[$unit])) return false; |
|---|
| 43 | |
|---|
| 44 | $number = $this->number_def->validate($number, $config, $context); |
|---|
| 45 | if ($number === false) return false; |
|---|
| 46 | |
|---|
| 47 | return $number . $unit; |
|---|
| 48 | |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | } |
|---|
| 52 | |
|---|