ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 3 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 43 of file Length.php.

References $n, and n.

44  {
45  $this->n = (string) $n;
46  $this->unit = $u !== false ? (string) $u : false;
47  }
$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 146 of file Length.php.

References $l, and n.

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

◆ getN()

HTMLPurifier_Length::getN ( )

Retrieves string numeric magnitude.

Returns
string

Definition at line 113 of file Length.php.

References $n.

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

◆ getUnit()

HTMLPurifier_Length::getUnit ( )

Retrieves string unit.

Returns
string

Definition at line 122 of file Length.php.

References $unit.

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

◆ isValid()

HTMLPurifier_Length::isValid ( )

Returns true if this length unit is valid.

Returns
bool

Definition at line 131 of file Length.php.

References $isValid, and validate().

Referenced by toString().

132  {
133  if ($this->isValid === null) {
134  $this->isValid = $this->validate();
135  }
136  return $this->isValid;
137  }
$isValid
Whether or not this length is valid.
Definition: Length.php:26
validate()
Validates the number and unit.
Definition: Length.php:72
isValid()
Returns true if this length unit is valid.
Definition: Length.php:131
+ 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 54 of file Length.php.

References $n, $s, and $unit.

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

55  {
56  if ($s instanceof HTMLPurifier_Length) {
57  return $s;
58  }
59  $n_length = strspn($s, '1234567890.+-');
60  $n = substr($s, 0, $n_length);
61  $unit = substr($s, $n_length);
62  if ($unit === '') {
63  $unit = false;
64  }
65  return new HTMLPurifier_Length($n, $unit);
66  }
$n
String numeric magnitude.
Definition: Length.php:14
$s
Definition: pwgen.php:45
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 101 of file Length.php.

References $unit, isValid(), and n.

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

◆ validate()

HTMLPurifier_Length::validate ( )
protected

Validates the number and unit.

Returns
bool

Definition at line 72 of file Length.php.

References $allowedUnits, $def, $result, and n.

Referenced by isValid().

73  {
74  // Special case:
75  if ($this->n === '+0' || $this->n === '-0') {
76  $this->n = '0';
77  }
78  if ($this->n === '0' && $this->unit === false) {
79  return true;
80  }
81  if (!ctype_lower($this->unit)) {
82  $this->unit = strtolower($this->unit);
83  }
84  if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {
85  return false;
86  }
87  // Hack:
89  $result = $def->validate($this->n, false, false);
90  if ($result === false) {
91  return false;
92  }
93  $this->n = $result;
94  return true;
95  }
$result
Validates a number as defined by the CSS spec.
Definition: Number.php:6
static $allowedUnits
Array Lookup array of units recognized by CSS 3 array.
Definition: Length.php:32
if(! $in) print Initializing normalization quick check tables n
$def
Definition: croninfo.php:21
+ 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,
'ch' => true, 'rem' => true, 'vw' => true, 'vh' => true,
'vmin' => true, 'vmax' => true
)

Array Lookup array of units recognized by CSS 3 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: