mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-06 06:07:26 +02:00
Finally stabilize the unit converter.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1750 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* Represents a measurable length, with a string numeric magnitude
|
||||
* and a unit.
|
||||
* and a unit. This object is immutable.
|
||||
*/
|
||||
class HTMLPurifier_Length
|
||||
{
|
||||
@@ -10,12 +10,17 @@ class HTMLPurifier_Length
|
||||
/**
|
||||
* String numeric magnitude.
|
||||
*/
|
||||
public $n;
|
||||
protected $n;
|
||||
|
||||
/**
|
||||
* String unit. False is permitted if $n = 0.
|
||||
*/
|
||||
public $unit;
|
||||
protected $unit;
|
||||
|
||||
/**
|
||||
* Whether or not this length is valid. Null if not calculated yet.
|
||||
*/
|
||||
protected $isValid;
|
||||
|
||||
/**
|
||||
* Lookup array of units recognized by CSS 2.1
|
||||
@@ -30,8 +35,8 @@ class HTMLPurifier_Length
|
||||
* @param string $u Unit
|
||||
*/
|
||||
public function __construct($n = '0', $u = false) {
|
||||
$this->n = $n;
|
||||
$this->unit = $u;
|
||||
$this->n = (string) $n;
|
||||
$this->unit = $u !== false ? (string) $u : false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,13 +56,15 @@ class HTMLPurifier_Length
|
||||
* @param bool $non_negative Whether or not to disable negative values.
|
||||
* @note Maybe should be put in another class.
|
||||
*/
|
||||
public function validate($non_negative = false, $config, $context) {
|
||||
protected function validate($non_negative = false) {
|
||||
// Special case:
|
||||
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;
|
||||
// Hack:
|
||||
$def = new HTMLPurifier_AttrDef_CSS_Number($non_negative);
|
||||
$result = $def->validate($this->n, $config, $context);
|
||||
$result = $def->validate($this->n, false, false);
|
||||
if ($result === false) return false;
|
||||
$this->n = $result;
|
||||
return true;
|
||||
@@ -67,7 +74,26 @@ class HTMLPurifier_Length
|
||||
* Returns string representation of number.
|
||||
*/
|
||||
public function toString() {
|
||||
if (!$this->isValid()) return false;
|
||||
return $this->n . $this->unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves string numeric magnitude.
|
||||
*/
|
||||
public function getN() {return $this->n;}
|
||||
|
||||
/**
|
||||
* Retrieves string unit.
|
||||
*/
|
||||
public function getUnit() {return $this->unit;}
|
||||
|
||||
/**
|
||||
* Returns true if this length unit is valid.
|
||||
*/
|
||||
public function isValid($non_negative = false) {
|
||||
if ($this->isValid === null) $this->isValid = $this->validate($non_negative);
|
||||
return $this->isValid;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user