]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/3rdparty/htmlpurifier/HTMLPurifier/Length.php
[add] HTML Purifier added to clean code
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / Length.php
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/Length.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/Length.php
new file mode 100644 (file)
index 0000000..149fcf4
--- /dev/null
@@ -0,0 +1,160 @@
+<?php\r
+\r
+/**\r
+ * Represents a measurable length, with a string numeric magnitude\r
+ * and a unit. This object is immutable.\r
+ */\r
+class HTMLPurifier_Length\r
+{\r
+\r
+    /**\r
+     * String numeric magnitude.\r
+     * @type string\r
+     */\r
+    protected $n;\r
+\r
+    /**\r
+     * String unit. False is permitted if $n = 0.\r
+     * @type string|bool\r
+     */\r
+    protected $unit;\r
+\r
+    /**\r
+     * Whether or not this length is valid. Null if not calculated yet.\r
+     * @type bool\r
+     */\r
+    protected $isValid;\r
+\r
+    /**\r
+     * Array Lookup array of units recognized by CSS 2.1\r
+     * @type array\r
+     */\r
+    protected static $allowedUnits = array(\r
+        'em' => true, 'ex' => true, 'px' => true, 'in' => true,\r
+        'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true\r
+    );\r
+\r
+    /**\r
+     * @param string $n Magnitude\r
+     * @param bool|string $u Unit\r
+     */\r
+    public function __construct($n = '0', $u = false)\r
+    {\r
+        $this->n = (string) $n;\r
+        $this->unit = $u !== false ? (string) $u : false;\r
+    }\r
+\r
+    /**\r
+     * @param string $s Unit string, like '2em' or '3.4in'\r
+     * @return HTMLPurifier_Length\r
+     * @warning Does not perform validation.\r
+     */\r
+    public static function make($s)\r
+    {\r
+        if ($s instanceof HTMLPurifier_Length) {\r
+            return $s;\r
+        }\r
+        $n_length = strspn($s, '1234567890.+-');\r
+        $n = substr($s, 0, $n_length);\r
+        $unit = substr($s, $n_length);\r
+        if ($unit === '') {\r
+            $unit = false;\r
+        }\r
+        return new HTMLPurifier_Length($n, $unit);\r
+    }\r
+\r
+    /**\r
+     * Validates the number and unit.\r
+     * @return bool\r
+     */\r
+    protected function validate()\r
+    {\r
+        // Special case:\r
+        if ($this->n === '+0' || $this->n === '-0') {\r
+            $this->n = '0';\r
+        }\r
+        if ($this->n === '0' && $this->unit === false) {\r
+            return true;\r
+        }\r
+        if (!ctype_lower($this->unit)) {\r
+            $this->unit = strtolower($this->unit);\r
+        }\r
+        if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {\r
+            return false;\r
+        }\r
+        // Hack:\r
+        $def = new HTMLPurifier_AttrDef_CSS_Number();\r
+        $result = $def->validate($this->n, false, false);\r
+        if ($result === false) {\r
+            return false;\r
+        }\r
+        $this->n = $result;\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Returns string representation of number.\r
+     * @return string\r
+     */\r
+    public function toString()\r
+    {\r
+        if (!$this->isValid()) {\r
+            return false;\r
+        }\r
+        return $this->n . $this->unit;\r
+    }\r
+\r
+    /**\r
+     * Retrieves string numeric magnitude.\r
+     * @return string\r
+     */\r
+    public function getN()\r
+    {\r
+        return $this->n;\r
+    }\r
+\r
+    /**\r
+     * Retrieves string unit.\r
+     * @return string\r
+     */\r
+    public function getUnit()\r
+    {\r
+        return $this->unit;\r
+    }\r
+\r
+    /**\r
+     * Returns true if this length unit is valid.\r
+     * @return bool\r
+     */\r
+    public function isValid()\r
+    {\r
+        if ($this->isValid === null) {\r
+            $this->isValid = $this->validate();\r
+        }\r
+        return $this->isValid;\r
+    }\r
+\r
+    /**\r
+     * Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.\r
+     * @param HTMLPurifier_Length $l\r
+     * @return int\r
+     * @warning If both values are too large or small, this calculation will\r
+     *          not work properly\r
+     */\r
+    public function compareTo($l)\r
+    {\r
+        if ($l === false) {\r
+            return false;\r
+        }\r
+        if ($l->unit !== $this->unit) {\r
+            $converter = new HTMLPurifier_UnitConverter();\r
+            $l = $converter->convert($l, $this->unit);\r
+            if ($l === false) {\r
+                return false;\r
+            }\r
+        }\r
+        return $this->n - $l->n;\r
+    }\r
+}\r
+\r
+// vim: et sw=4 sts=4\r