friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/Length.php

161 lines
3.7 KiB
PHP
Raw Normal View History

2010-09-09 05:14:17 +02:00
<?php
/**
* Represents a measurable length, with a string numeric magnitude
* and a unit. This object is immutable.
*/
class HTMLPurifier_Length
{
/**
* String numeric magnitude.
2016-02-09 11:06:17 +01:00
* @type string
2010-09-09 05:14:17 +02:00
*/
protected $n;
/**
* String unit. False is permitted if $n = 0.
2016-02-09 11:06:17 +01:00
* @type string|bool
2010-09-09 05:14:17 +02:00
*/
protected $unit;
/**
* Whether or not this length is valid. Null if not calculated yet.
2016-02-09 11:06:17 +01:00
* @type bool
2010-09-09 05:14:17 +02:00
*/
protected $isValid;
/**
2016-02-09 11:06:17 +01:00
* Array Lookup array of units recognized by CSS 2.1
* @type array
2010-09-09 05:14:17 +02:00
*/
protected static $allowedUnits = array(
'em' => true, 'ex' => true, 'px' => true, 'in' => true,
'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
);
/**
2016-02-09 11:06:17 +01:00
* @param string $n Magnitude
* @param bool|string $u Unit
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function __construct($n = '0', $u = false)
{
2010-09-09 05:14:17 +02:00
$this->n = (string) $n;
$this->unit = $u !== false ? (string) $u : false;
}
/**
* @param string $s Unit string, like '2em' or '3.4in'
2016-02-09 11:06:17 +01:00
* @return HTMLPurifier_Length
2010-09-09 05:14:17 +02:00
* @warning Does not perform validation.
*/
2016-02-09 11:06:17 +01:00
public static function make($s)
{
if ($s instanceof HTMLPurifier_Length) {
return $s;
}
2010-09-09 05:14:17 +02:00
$n_length = strspn($s, '1234567890.+-');
$n = substr($s, 0, $n_length);
$unit = substr($s, $n_length);
2016-02-09 11:06:17 +01:00
if ($unit === '') {
$unit = false;
}
2010-09-09 05:14:17 +02:00
return new HTMLPurifier_Length($n, $unit);
}
/**
* Validates the number and unit.
2016-02-09 11:06:17 +01:00
* @return bool
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function validate()
{
2010-09-09 05:14:17 +02:00
// Special case:
2016-02-09 11:06:17 +01:00
if ($this->n === '+0' || $this->n === '-0') {
$this->n = '0';
}
if ($this->n === '0' && $this->unit === false) {
return true;
}
if (!ctype_lower($this->unit)) {
$this->unit = strtolower($this->unit);
}
if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {
return false;
}
2010-09-09 05:14:17 +02:00
// Hack:
$def = new HTMLPurifier_AttrDef_CSS_Number();
$result = $def->validate($this->n, false, false);
2016-02-09 11:06:17 +01:00
if ($result === false) {
return false;
}
2010-09-09 05:14:17 +02:00
$this->n = $result;
return true;
}
/**
* Returns string representation of number.
2016-02-09 11:06:17 +01:00
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function toString()
{
if (!$this->isValid()) {
return false;
}
2010-09-09 05:14:17 +02:00
return $this->n . $this->unit;
}
/**
* Retrieves string numeric magnitude.
2016-02-09 11:06:17 +01:00
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function getN()
{
return $this->n;
}
2010-09-09 05:14:17 +02:00
/**
* Retrieves string unit.
2016-02-09 11:06:17 +01:00
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function getUnit()
{
return $this->unit;
}
2010-09-09 05:14:17 +02:00
/**
* Returns true if this length unit is valid.
2016-02-09 11:06:17 +01:00
* @return bool
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function isValid()
{
if ($this->isValid === null) {
$this->isValid = $this->validate();
}
2010-09-09 05:14:17 +02:00
return $this->isValid;
}
/**
* Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Length $l
* @return int
2010-09-09 05:14:17 +02:00
* @warning If both values are too large or small, this calculation will
* not work properly
*/
2016-02-09 11:06:17 +01:00
public function compareTo($l)
{
if ($l === false) {
return false;
}
2010-09-09 05:14:17 +02:00
if ($l->unit !== $this->unit) {
$converter = new HTMLPurifier_UnitConverter();
$l = $converter->convert($l, $this->unit);
2016-02-09 11:06:17 +01:00
if ($l === false) {
return false;
}
2010-09-09 05:14:17 +02:00
}
return $this->n - $l->n;
}
}
// vim: et sw=4 sts=4