]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * Represents a measurable length, with a string numeric magnitude\r | |
5 | * and a unit. This object is immutable.\r | |
6 | */\r | |
7 | class HTMLPurifier_Length\r | |
8 | {\r | |
9 | \r | |
10 | /**\r | |
11 | * String numeric magnitude.\r | |
12 | * @type string\r | |
13 | */\r | |
14 | protected $n;\r | |
15 | \r | |
16 | /**\r | |
17 | * String unit. False is permitted if $n = 0.\r | |
18 | * @type string|bool\r | |
19 | */\r | |
20 | protected $unit;\r | |
21 | \r | |
22 | /**\r | |
23 | * Whether or not this length is valid. Null if not calculated yet.\r | |
24 | * @type bool\r | |
25 | */\r | |
26 | protected $isValid;\r | |
27 | \r | |
28 | /**\r | |
29 | * Array Lookup array of units recognized by CSS 2.1\r | |
30 | * @type array\r | |
31 | */\r | |
32 | protected static $allowedUnits = array(\r | |
33 | 'em' => true, 'ex' => true, 'px' => true, 'in' => true,\r | |
34 | 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true\r | |
35 | );\r | |
36 | \r | |
37 | /**\r | |
38 | * @param string $n Magnitude\r | |
39 | * @param bool|string $u Unit\r | |
40 | */\r | |
41 | public function __construct($n = '0', $u = false)\r | |
42 | {\r | |
43 | $this->n = (string) $n;\r | |
44 | $this->unit = $u !== false ? (string) $u : false;\r | |
45 | }\r | |
46 | \r | |
47 | /**\r | |
48 | * @param string $s Unit string, like '2em' or '3.4in'\r | |
49 | * @return HTMLPurifier_Length\r | |
50 | * @warning Does not perform validation.\r | |
51 | */\r | |
52 | public static function make($s)\r | |
53 | {\r | |
54 | if ($s instanceof HTMLPurifier_Length) {\r | |
55 | return $s;\r | |
56 | }\r | |
57 | $n_length = strspn($s, '1234567890.+-');\r | |
58 | $n = substr($s, 0, $n_length);\r | |
59 | $unit = substr($s, $n_length);\r | |
60 | if ($unit === '') {\r | |
61 | $unit = false;\r | |
62 | }\r | |
63 | return new HTMLPurifier_Length($n, $unit);\r | |
64 | }\r | |
65 | \r | |
66 | /**\r | |
67 | * Validates the number and unit.\r | |
68 | * @return bool\r | |
69 | */\r | |
70 | protected function validate()\r | |
71 | {\r | |
72 | // Special case:\r | |
73 | if ($this->n === '+0' || $this->n === '-0') {\r | |
74 | $this->n = '0';\r | |
75 | }\r | |
76 | if ($this->n === '0' && $this->unit === false) {\r | |
77 | return true;\r | |
78 | }\r | |
79 | if (!ctype_lower($this->unit)) {\r | |
80 | $this->unit = strtolower($this->unit);\r | |
81 | }\r | |
82 | if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {\r | |
83 | return false;\r | |
84 | }\r | |
85 | // Hack:\r | |
86 | $def = new HTMLPurifier_AttrDef_CSS_Number();\r | |
87 | $result = $def->validate($this->n, false, false);\r | |
88 | if ($result === false) {\r | |
89 | return false;\r | |
90 | }\r | |
91 | $this->n = $result;\r | |
92 | return true;\r | |
93 | }\r | |
94 | \r | |
95 | /**\r | |
96 | * Returns string representation of number.\r | |
97 | * @return string\r | |
98 | */\r | |
99 | public function toString()\r | |
100 | {\r | |
101 | if (!$this->isValid()) {\r | |
102 | return false;\r | |
103 | }\r | |
104 | return $this->n . $this->unit;\r | |
105 | }\r | |
106 | \r | |
107 | /**\r | |
108 | * Retrieves string numeric magnitude.\r | |
109 | * @return string\r | |
110 | */\r | |
111 | public function getN()\r | |
112 | {\r | |
113 | return $this->n;\r | |
114 | }\r | |
115 | \r | |
116 | /**\r | |
117 | * Retrieves string unit.\r | |
118 | * @return string\r | |
119 | */\r | |
120 | public function getUnit()\r | |
121 | {\r | |
122 | return $this->unit;\r | |
123 | }\r | |
124 | \r | |
125 | /**\r | |
126 | * Returns true if this length unit is valid.\r | |
127 | * @return bool\r | |
128 | */\r | |
129 | public function isValid()\r | |
130 | {\r | |
131 | if ($this->isValid === null) {\r | |
132 | $this->isValid = $this->validate();\r | |
133 | }\r | |
134 | return $this->isValid;\r | |
135 | }\r | |
136 | \r | |
137 | /**\r | |
138 | * Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.\r | |
139 | * @param HTMLPurifier_Length $l\r | |
140 | * @return int\r | |
141 | * @warning If both values are too large or small, this calculation will\r | |
142 | * not work properly\r | |
143 | */\r | |
144 | public function compareTo($l)\r | |
145 | {\r | |
146 | if ($l === false) {\r | |
147 | return false;\r | |
148 | }\r | |
149 | if ($l->unit !== $this->unit) {\r | |
150 | $converter = new HTMLPurifier_UnitConverter();\r | |
151 | $l = $converter->convert($l, $this->unit);\r | |
152 | if ($l === false) {\r | |
153 | return false;\r | |
154 | }\r | |
155 | }\r | |
156 | return $this->n - $l->n;\r | |
157 | }\r | |
158 | }\r | |
159 | \r | |
160 | // vim: et sw=4 sts=4\r |