Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS / Font.php
blob6ce18efb80930bccc5d89502da9c634a0fbf5f23
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
5 /**
6 * Validates shorthand CSS property font.
7 */
8 class HTMLPurifier_AttrDef_CSS_Font extends HTMLPurifier_AttrDef
11 /**
12 * Local copy of component validators.
14 * @note If we moved specific CSS property definitions to their own
15 * classes instead of having them be assembled at run time by
16 * CSSDefinition, this wouldn't be necessary. We'd instantiate
17 * our own copies.
19 var $info = array();
21 function HTMLPurifier_AttrDef_CSS_Font($config) {
22 $def = $config->getCSSDefinition();
23 $this->info['font-style'] = $def->info['font-style'];
24 $this->info['font-variant'] = $def->info['font-variant'];
25 $this->info['font-weight'] = $def->info['font-weight'];
26 $this->info['font-size'] = $def->info['font-size'];
27 $this->info['line-height'] = $def->info['line-height'];
28 $this->info['font-family'] = $def->info['font-family'];
31 function validate($string, $config, &$context) {
33 static $system_fonts = array(
34 'caption' => true,
35 'icon' => true,
36 'menu' => true,
37 'message-box' => true,
38 'small-caption' => true,
39 'status-bar' => true
42 // regular pre-processing
43 $string = $this->parseCDATA($string);
44 if ($string === '') return false;
46 // check if it's one of the keywords
47 $lowercase_string = strtolower($string);
48 if (isset($system_fonts[$lowercase_string])) {
49 return $lowercase_string;
52 $bits = explode(' ', $string); // bits to process
53 $stage = 0; // this indicates what we're looking for
54 $caught = array(); // which stage 0 properties have we caught?
55 $stage_1 = array('font-style', 'font-variant', 'font-weight');
56 $final = ''; // output
58 for ($i = 0, $size = count($bits); $i < $size; $i++) {
59 if ($bits[$i] === '') continue;
60 switch ($stage) {
62 // attempting to catch font-style, font-variant or font-weight
63 case 0:
64 foreach ($stage_1 as $validator_name) {
65 if (isset($caught[$validator_name])) continue;
66 $r = $this->info[$validator_name]->validate(
67 $bits[$i], $config, $context);
68 if ($r !== false) {
69 $final .= $r . ' ';
70 $caught[$validator_name] = true;
71 break;
74 // all three caught, continue on
75 if (count($caught) >= 3) $stage = 1;
76 if ($r !== false) break;
78 // attempting to catch font-size and perhaps line-height
79 case 1:
80 $found_slash = false;
81 if (strpos($bits[$i], '/') !== false) {
82 list($font_size, $line_height) =
83 explode('/', $bits[$i]);
84 if ($line_height === '') {
85 // ooh, there's a space after the slash!
86 $line_height = false;
87 $found_slash = true;
89 } else {
90 $font_size = $bits[$i];
91 $line_height = false;
93 $r = $this->info['font-size']->validate(
94 $font_size, $config, $context);
95 if ($r !== false) {
96 $final .= $r;
97 // attempt to catch line-height
98 if ($line_height === false) {
99 // we need to scroll forward
100 for ($j = $i + 1; $j < $size; $j++) {
101 if ($bits[$j] === '') continue;
102 if ($bits[$j] === '/') {
103 if ($found_slash) {
104 return false;
105 } else {
106 $found_slash = true;
107 continue;
110 $line_height = $bits[$j];
111 break;
113 } else {
114 // slash already found
115 $found_slash = true;
116 $j = $i;
118 if ($found_slash) {
119 $i = $j;
120 $r = $this->info['line-height']->validate(
121 $line_height, $config, $context);
122 if ($r !== false) {
123 $final .= '/' . $r;
126 $final .= ' ';
127 $stage = 2;
128 break;
130 return false;
132 // attempting to catch font-family
133 case 2:
134 $font_family =
135 implode(' ', array_slice($bits, $i, $size - $i));
136 $r = $this->info['font-family']->validate(
137 $font_family, $config, $context);
138 if ($r !== false) {
139 $final .= $r . ' ';
140 // processing completed successfully
141 return rtrim($final);
143 return false;
146 return false;