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

Validates Color as defined by CSS. More...

+ Inheritance diagram for HTMLPurifier_AttrDef_CSS_Color:
+ Collaboration diagram for HTMLPurifier_AttrDef_CSS_Color:

Public Member Functions

 validate ($color, $config, $context)
 
- Public Member Functions inherited from HTMLPurifier_AttrDef
 validate ($string, $config, $context)
 Validates and cleans passed string according to a definition. More...
 
 parseCDATA ($string)
 Convenience method that parses a string as if it were CDATA. More...
 
 make ($string)
 Factory method for creating this class from a string. More...
 

Additional Inherited Members

- Data Fields inherited from HTMLPurifier_AttrDef
 $minimized = false
 Tells us whether or not an HTML attribute is minimized. More...
 
 $required = false
 Tells us whether or not an HTML attribute is required. More...
 
- Protected Member Functions inherited from HTMLPurifier_AttrDef
 mungeRgb ($string)
 Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work properly. More...
 
 expandCSSEscape ($string)
 Parses a possibly escaped CSS string and returns the "pure" version of it. More...
 

Detailed Description

Validates Color as defined by CSS.

Definition at line 6 of file Color.php.

Member Function Documentation

◆ validate()

HTMLPurifier_AttrDef_CSS_Color::validate (   $color,
  $config,
  $context 
)
Parameters
string$color
HTMLPurifier_Config$config
HTMLPurifier_Context$context
Returns
bool|string

Definition at line 15 of file Color.php.

16  {
17  static $colors = null;
18  if ($colors === null) {
19  $colors = $config->get('Core.ColorKeywords');
20  }
21 
22  $color = trim($color);
23  if ($color === '') {
24  return false;
25  }
26 
27  $lower = strtolower($color);
28  if (isset($colors[$lower])) {
29  return $colors[$lower];
30  }
31 
32  if (strpos($color, 'rgb(') !== false) {
33  // rgb literal handling
34  $length = strlen($color);
35  if (strpos($color, ')') !== $length - 1) {
36  return false;
37  }
38  $triad = substr($color, 4, $length - 4 - 1);
39  $parts = explode(',', $triad);
40  if (count($parts) !== 3) {
41  return false;
42  }
43  $type = false; // to ensure that they're all the same type
44  $new_parts = array();
45  foreach ($parts as $part) {
46  $part = trim($part);
47  if ($part === '') {
48  return false;
49  }
50  $length = strlen($part);
51  if ($part[$length - 1] === '%') {
52  // handle percents
53  if (!$type) {
54  $type = 'percentage';
55  } elseif ($type !== 'percentage') {
56  return false;
57  }
58  $num = (float)substr($part, 0, $length - 1);
59  if ($num < 0) {
60  $num = 0;
61  }
62  if ($num > 100) {
63  $num = 100;
64  }
65  $new_parts[] = "$num%";
66  } else {
67  // handle integers
68  if (!$type) {
69  $type = 'integer';
70  } elseif ($type !== 'integer') {
71  return false;
72  }
73  $num = (int)$part;
74  if ($num < 0) {
75  $num = 0;
76  }
77  if ($num > 255) {
78  $num = 255;
79  }
80  $new_parts[] = (string)$num;
81  }
82  }
83  $new_triad = implode(',', $new_parts);
84  $color = "rgb($new_triad)";
85  } else {
86  // hexadecimal handling
87  if ($color[0] === '#') {
88  $hex = substr($color, 1);
89  } else {
90  $hex = $color;
91  $color = '#' . $color;
92  }
93  $length = strlen($hex);
94  if ($length !== 3 && $length !== 6) {
95  return false;
96  }
97  if (!ctype_xdigit($hex)) {
98  return false;
99  }
100  }
101  return $color;
102  }

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