ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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
 Lookup array of units recognized by CSS 2.1. 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
number$nMagnitude
string$uUnit

Definition at line 37 of file Length.php.

References $n, and n.

37  {
38  $this->n = (string) $n;
39  $this->unit = $u !== false ? (string) $u : false;
40  }
$n
String numeric magnitude.
Definition: Length.php:13
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.

Warning
If both values are too large or small, this calculation will not work properly

Definition at line 103 of file Length.php.

References n.

103  {
104  if ($l === false) return false;
105  if ($l->unit !== $this->unit) {
106  $converter = new HTMLPurifier_UnitConverter();
107  $l = $converter->convert($l, $this->unit);
108  if ($l === false) return false;
109  }
110  return $this->n - $l->n;
111  }
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.

Definition at line 83 of file Length.php.

References $n.

83 {return $this->n;}
$n
String numeric magnitude.
Definition: Length.php:13

◆ getUnit()

HTMLPurifier_Length::getUnit ( )

Retrieves string unit.

Definition at line 88 of file Length.php.

References $unit.

88 {return $this->unit;}
$unit
String unit.
Definition: Length.php:18

◆ isValid()

HTMLPurifier_Length::isValid ( )

Returns true if this length unit is valid.

Definition at line 93 of file Length.php.

References $isValid, and validate().

Referenced by toString().

93  {
94  if ($this->isValid === null) $this->isValid = $this->validate();
95  return $this->isValid;
96  }
$isValid
Whether or not this length is valid.
Definition: Length.php:23
validate()
Validates the number and unit.
Definition: Length.php:58
isValid()
Returns true if this length unit is valid.
Definition: Length.php:93
+ 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'
Warning
Does not perform validation.

Definition at line 46 of file Length.php.

References $n, and $unit.

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

46  {
47  if ($s instanceof HTMLPurifier_Length) return $s;
48  $n_length = strspn($s, '1234567890.+-');
49  $n = substr($s, 0, $n_length);
50  $unit = substr($s, $n_length);
51  if ($unit === '') $unit = false;
52  return new HTMLPurifier_Length($n, $unit);
53  }
$n
String numeric magnitude.
Definition: Length.php:13
Represents a measurable length, with a string numeric magnitude and a unit.
Definition: Length.php:7
$unit
String unit.
Definition: Length.php:18
+ Here is the caller graph for this function:

◆ toString()

HTMLPurifier_Length::toString ( )

Returns string representation of number.

Definition at line 75 of file Length.php.

References $unit, isValid(), and n.

75  {
76  if (!$this->isValid()) return false;
77  return $this->n . $this->unit;
78  }
$unit
String unit.
Definition: Length.php:18
isValid()
Returns true if this length unit is valid.
Definition: Length.php:93
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.

Definition at line 58 of file Length.php.

References $allowedUnits, $result, and n.

Referenced by isValid().

58  {
59  // Special case:
60  if ($this->n === '+0' || $this->n === '-0') $this->n = '0';
61  if ($this->n === '0' && $this->unit === false) return true;
62  if (!ctype_lower($this->unit)) $this->unit = strtolower($this->unit);
63  if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) return false;
64  // Hack:
66  $result = $def->validate($this->n, false, false);
67  if ($result === false) return false;
68  $this->n = $result;
69  return true;
70  }
$result
Validates a number as defined by the CSS spec.
Definition: Number.php:6
static $allowedUnits
Lookup array of units recognized by CSS 2.1.
Definition: Length.php:28
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
)

Lookup array of units recognized by CSS 2.1.

Definition at line 28 of file Length.php.

Referenced by validate().

◆ $isValid

HTMLPurifier_Length::$isValid
protected

Whether or not this length is valid.

Null if not calculated yet.

Definition at line 23 of file Length.php.

Referenced by isValid().

◆ $n

HTMLPurifier_Length::$n
protected

String numeric magnitude.

Definition at line 13 of file Length.php.

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

◆ $unit

HTMLPurifier_Length::$unit
protected

String unit.

False is permitted if $n = 0.

Definition at line 18 of file Length.php.

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


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