00001 <?php
00002
00007 class HTMLPurifier_Length
00008 {
00009
00013 protected $n;
00014
00018 protected $unit;
00019
00023 protected $isValid;
00024
00028 protected static $allowedUnits = array(
00029 'em' => true, 'ex' => true, 'px' => true, 'in' => true,
00030 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
00031 );
00032
00037 public function __construct($n = '0', $u = false) {
00038 $this->n = (string) $n;
00039 $this->unit = $u !== false ? (string) $u : false;
00040 }
00041
00046 static public function make($s) {
00047 if ($s instanceof HTMLPurifier_Length) return $s;
00048 $n_length = strspn($s, '1234567890.+-');
00049 $n = substr($s, 0, $n_length);
00050 $unit = substr($s, $n_length);
00051 if ($unit === '') $unit = false;
00052 return new HTMLPurifier_Length($n, $unit);
00053 }
00054
00058 protected function validate() {
00059
00060 if ($this->n === '+0' || $this->n === '-0') $this->n = '0';
00061 if ($this->n === '0' && $this->unit === false) return true;
00062 if (!ctype_lower($this->unit)) $this->unit = strtolower($this->unit);
00063 if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) return false;
00064
00065 $def = new HTMLPurifier_AttrDef_CSS_Number();
00066 $result = $def->validate($this->n, false, false);
00067 if ($result === false) return false;
00068 $this->n = $result;
00069 return true;
00070 }
00071
00075 public function toString() {
00076 if (!$this->isValid()) return false;
00077 return $this->n . $this->unit;
00078 }
00079
00083 public function getN() {return $this->n;}
00084
00088 public function getUnit() {return $this->unit;}
00089
00093 public function isValid() {
00094 if ($this->isValid === null) $this->isValid = $this->validate();
00095 return $this->isValid;
00096 }
00097
00103 public function compareTo($l) {
00104 if ($l === false) return false;
00105 if ($l->unit !== $this->unit) {
00106 $converter = new HTMLPurifier_UnitConverter();
00107 $l = $converter->convert($l, $this->unit);
00108 if ($l === false) return false;
00109 }
00110 return $this->n - $l->n;
00111 }
00112
00113 }