1: <?php
2:
3: 4: 5: 6:
7: class HTMLPurifier_Length
8: {
9:
10: 11: 12:
13: protected $n;
14:
15: 16: 17:
18: protected $unit;
19:
20: 21: 22:
23: protected $isValid;
24:
25: 26: 27:
28: protected static $allowedUnits = array(
29: 'em' => true, 'ex' => true, 'px' => true, 'in' => true,
30: 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
31: );
32:
33: 34: 35: 36:
37: public function __construct($n = '0', $u = false) {
38: $this->n = (string) $n;
39: $this->unit = $u !== false ? (string) $u : false;
40: }
41:
42: 43: 44: 45:
46: static public function make($s) {
47: if ($s instanceof HTMLPurifier_Length) return $s;
48: $n_length = strspn($s, '1234567890.+-');
49: $n = substr($s, 0, $n_length);
50: $unit = substr($s, $n_length);
51: if ($unit === '') $unit = false;
52: return new HTMLPurifier_Length($n, $unit);
53: }
54:
55: 56: 57:
58: protected function validate() {
59:
60: if ($this->n === '+0' || $this->n === '-0') $this->n = '0';
61: if ($this->n === '0' && $this->unit === false) return true;
62: if (!ctype_lower($this->unit)) $this->unit = strtolower($this->unit);
63: if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) return false;
64:
65: $def = new HTMLPurifier_AttrDef_CSS_Number();
66: $result = $def->validate($this->n, false, false);
67: if ($result === false) return false;
68: $this->n = $result;
69: return true;
70: }
71:
72: 73: 74:
75: public function toString() {
76: if (!$this->isValid()) return false;
77: return $this->n . $this->unit;
78: }
79:
80: 81: 82:
83: public function getN() {return $this->n;}
84:
85: 86: 87:
88: public function getUnit() {return $this->unit;}
89:
90: 91: 92:
93: public function isValid() {
94: if ($this->isValid === null) $this->isValid = $this->validate();
95: return $this->isValid;
96: }
97:
98: 99: 100: 101: 102:
103: public function compareTo($l) {
104: if ($l === false) return false;
105: if ($l->unit !== $this->unit) {
106: $converter = new HTMLPurifier_UnitConverter();
107: $l = $converter->convert($l, $this->unit);
108: if ($l === false) return false;
109: }
110: return $this->n - $l->n;
111: }
112:
113: }
114:
115:
116: