3 require_once 'HTMLPurifier/AttrDef.php';
6 * Validates shorthand CSS property font.
8 class HTMLPurifier_AttrDef_CSS_Font
extends HTMLPurifier_AttrDef
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
22 * System font keywords.
24 var $system_fonts = array(
28 'message-box' => true,
29 'small-caption' => true,
33 function HTMLPurifier_AttrDef_CSS_Font($config) {
34 $def = $config->getCSSDefinition();
35 $this->info
['font-style'] = $def->info
['font-style'];
36 $this->info
['font-variant'] = $def->info
['font-variant'];
37 $this->info
['font-weight'] = $def->info
['font-weight'];
38 $this->info
['font-size'] = $def->info
['font-size'];
39 $this->info
['line-height'] = $def->info
['line-height'];
40 $this->info
['font-family'] = $def->info
['font-family'];
43 function validate($string, $config, &$context) {
45 // regular pre-processing
46 $string = $this->parseCDATA($string);
47 if ($string === '') return false;
49 // check if it's one of the keywords
50 $lowercase_string = strtolower($string);
51 if (isset($this->system_fonts
[$lowercase_string])) {
52 return $lowercase_string;
55 $bits = explode(' ', $string); // bits to process
56 $stage = 0; // this indicates what we're looking for
57 $caught = array(); // which stage 0 properties have we caught?
58 $stage_1 = array('font-style', 'font-variant', 'font-weight');
59 $final = ''; // output
61 for ($i = 0, $size = count($bits); $i < $size; $i++
) {
62 if ($bits[$i] === '') continue;
65 // attempting to catch font-style, font-variant or font-weight
67 foreach ($stage_1 as $validator_name) {
68 if (isset($caught[$validator_name])) continue;
69 $r = $this->info
[$validator_name]->validate(
70 $bits[$i], $config, $context);
73 $caught[$validator_name] = true;
77 // all three caught, continue on
78 if (count($caught) >= 3) $stage = 1;
79 if ($r !== false) break;
81 // attempting to catch font-size and perhaps line-height
84 if (strpos($bits[$i], '/') !== false) {
85 list($font_size, $line_height) =
86 explode('/', $bits[$i]);
87 if ($line_height === '') {
88 // ooh, there's a space after the slash!
93 $font_size = $bits[$i];
96 $r = $this->info
['font-size']->validate(
97 $font_size, $config, $context);
100 // attempt to catch line-height
101 if ($line_height === false) {
102 // we need to scroll forward
103 for ($j = $i +
1; $j < $size; $j++
) {
104 if ($bits[$j] === '') continue;
105 if ($bits[$j] === '/') {
113 $line_height = $bits[$j];
117 // slash already found
123 $r = $this->info
['line-height']->validate(
124 $line_height, $config, $context);
135 // attempting to catch font-family
138 implode(' ', array_slice($bits, $i, $size - $i));
139 $r = $this->info
['font-family']->validate(
140 $font_family, $config, $context);
143 // processing completed successfully
144 return rtrim($final);