ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f87
HTMLPurifier_Length Class Reference

Represents a measurable length, with a string numeric magnitude and a unit. More...

+ Collaboration diagram for HTMLPurifier_Length:

Public Member Functions

 __construct ($n='0', $u=false)
 
 toString ()
 Returns string representation of number. More...
 
 getN ()
 Retrieves string numeric magnitude. More...
 
 getUnit ()
 Retrieves string unit. More...
 
 isValid ()
 Returns true if this length unit is valid. More...
 
 compareTo ($l)
 Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal. More...
 

Static Public Member Functions

static make ($s)
 

Protected Member Functions

 validate ()
 Validates the number and unit. More...
 

Protected Attributes

 $n
 String numeric magnitude. More...
 
 $unit
 String unit. More...
 
 $isValid
 Whether or not this length is valid. More...
 

Static Protected Attributes

static $allowedUnits
 Array Lookup array of units recognized by CSS 2.1 array. More...
 

Detailed Description

Represents a measurable length, with a string numeric magnitude and a unit.

This object is immutable.

Definition at line 7 of file Length.php.

Constructor & Destructor Documentation

◆ __construct()

HTMLPurifier_Length::__construct (   $n = '0',
  $u = false 
)
Parameters
string$nMagnitude
bool | string$uUnit

Definition at line 41 of file Length.php.

References $n, and n.

42  {
43  $this->n = (string) $n;
44  $this->unit = $u !== false ? (string) $u : false;
45  }
$n
String numeric magnitude.
Definition: Length.php:14
if(! $in) print Initializing normalization quick check tables n

Member Function Documentation

◆ compareTo()

HTMLPurifier_Length::compareTo (   $l)

Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.

Parameters
HTMLPurifier_Length$l
Returns
int
Warning
If both values are too large or small, this calculation will not work properly

Definition at line 144 of file Length.php.

References n.

145  {
146  if ($l === false) {
147  return false;
148  }
149  if ($l->unit !== $this->unit) {
150  $converter = new HTMLPurifier_UnitConverter();
151  $l = $converter->convert($l, $this->unit);
152  if ($l === false) {
153  return false;
154  }
155  }
156  return $this->n - $l->n;
157  }
Class for converting between different unit-lengths as specified by CSS.
if(! $in) print Initializing normalization quick check tables n

◆ getN()

HTMLPurifier_Length::getN ( )

Retrieves string numeric magnitude.

Returns
string

Definition at line 111 of file Length.php.

References $n.

112  {
113  return $this->n;
114  }
$n
String numeric magnitude.
Definition: Length.php:14

◆ getUnit()

HTMLPurifier_Length::getUnit ( )

Retrieves string unit.

Returns
string

Definition at line 120 of file Length.php.

References $unit.

121  {
122  return $this->unit;
123  }
$unit
String unit.
Definition: Length.php:20

◆ isValid()

HTMLPurifier_Length::isValid ( )

Returns true if this length unit is valid.

Returns
bool

Definition at line 129 of file Length.php.

References $isValid, and validate().

Referenced by toString().

130  {
131  if ($this->isValid === null) {
132  $this->isValid = $this->validate();
133  }
134  return $this->isValid;
135  }
$isValid
Whether or not this length is valid.
Definition: Length.php:26
validate()
Validates the number and unit.
Definition: Length.php:70
isValid()
Returns true if this length unit is valid.
Definition: Length.php:129
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ make()

static HTMLPurifier_Length::make (   $s)
static
Parameters
string$sUnit string, like '2em' or '3.4in'
Returns
HTMLPurifier_Length
Warning
Does not perform validation.

Definition at line 52 of file Length.php.

References $n, and $unit.

Referenced by HTMLPurifier_AttrDef_CSS_Length\__construct(), and HTMLPurifier_AttrDef_CSS_Length\validate().

53  {
54  if ($s instanceof HTMLPurifier_Length) {
55  return $s;
56  }
57  $n_length = strspn($s, '1234567890.+-');
58  $n = substr($s, 0, $n_length);
59  $unit = substr($s, $n_length);
60  if ($unit === '') {
61  $unit = false;
62  }
63  return new HTMLPurifier_Length($n, $unit);
64  }
$n
String numeric magnitude.
Definition: Length.php:14
Represents a measurable length, with a string numeric magnitude and a unit.
Definition: Length.php:7
$unit
String unit.
Definition: Length.php:20
+ Here is the caller graph for this function:

◆ toString()

HTMLPurifier_Length::toString ( )

Returns string representation of number.

Returns
string

Definition at line 99 of file Length.php.

References $unit, isValid(), and n.

100  {
101  if (!$this->isValid()) {
102  return false;
103  }
104  return $this->n . $this->unit;
105  }
$unit
String unit.
Definition: Length.php:20
isValid()
Returns true if this length unit is valid.
Definition: Length.php:129
if(! $in) print Initializing normalization quick check tables n
+ Here is the call graph for this function:

◆ validate()

HTMLPurifier_Length::validate ( )
protected

Validates the number and unit.

Returns
bool

Definition at line 70 of file Length.php.

References $allowedUnits, $result, and n.

Referenced by isValid().

71  {
72  // Special case:
73  if ($this->n === '+0' || $this->n === '-0') {
74  $this->n = '0';
75  }
76  if ($this->n === '0' && $this->unit === false) {
77  return true;
78  }
79  if (!ctype_lower($this->unit)) {
80  $this->unit = strtolower($this->unit);
81  }
82  if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {
83  return false;
84  }
85  // Hack:
87  $result = $def->validate($this->n, false, false);
88  if ($result === false) {
89  return false;
90  }
91  $this->n = $result;
92  return true;
93  }
$result
Validates a number as defined by the CSS spec.
Definition: Number.php:6
static $allowedUnits
Array Lookup array of units recognized by CSS 2.1 array.
Definition: Length.php:32
if(! $in) print Initializing normalization quick check tables n
+ Here is the caller graph for this function:

Field Documentation

◆ $allowedUnits

HTMLPurifier_Length::$allowedUnits
staticprotected
Initial value:
= array(
'em' => true, 'ex' => true, 'px' => true, 'in' => true,
'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
)

Array Lookup array of units recognized by CSS 2.1 array.

Definition at line 32 of file Length.php.

Referenced by validate().

◆ $isValid

HTMLPurifier_Length::$isValid
protected

Whether or not this length is valid.

Null if not calculated yet. bool

Definition at line 26 of file Length.php.

Referenced by isValid().

◆ $n

HTMLPurifier_Length::$n
protected

String numeric magnitude.

string

Definition at line 14 of file Length.php.

Referenced by __construct(), getN(), and make().

◆ $unit

HTMLPurifier_Length::$unit
protected

String unit.

False is permitted if $n = 0. string|bool

Definition at line 20 of file Length.php.

Referenced by getUnit(), make(), and toString().


The documentation for this class was generated from the following file: