Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / CSS / Length.php
blob7da26a8f6b9de325e71429dd6b914fa2f410b81d
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/CSS/Number.php';
6 /**
7 * Represents a Length as defined by CSS.
8 */
9 class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef
12 /**
13 * Valid unit lookup table.
14 * @warning The code assumes all units are two characters long. Be careful
15 * if we have to change this behavior!
17 var $units = array('em' => true, 'ex' => true, 'px' => true, 'in' => true,
18 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true);
19 /**
20 * Instance of HTMLPurifier_AttrDef_Number to defer number validation to
22 var $number_def;
24 /**
25 * @param $non_negative Bool indication whether or not negative values are
26 * allowed.
28 function HTMLPurifier_AttrDef_CSS_Length($non_negative = false) {
29 $this->number_def = new HTMLPurifier_AttrDef_CSS_Number($non_negative);
32 function validate($length, $config, &$context) {
34 $length = $this->parseCDATA($length);
35 if ($length === '') return false;
36 if ($length === '0') return '0';
37 $strlen = strlen($length);
38 if ($strlen === 1) return false; // impossible!
40 // we assume all units are two characters
41 $unit = substr($length, $strlen - 2);
42 if (!ctype_lower($unit)) $unit = strtolower($unit);
43 $number = substr($length, 0, $strlen - 2);
45 if (!isset($this->units[$unit])) return false;
47 $number = $this->number_def->validate($number, $config, $context);
48 if ($number === false) return false;
50 return $number . $unit;