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
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(
37 'message-box' => true,
38 'small-caption' => 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;
62 // attempting to catch font-style, font-variant or font-weight
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);
70 $caught[$validator_name] = true;
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
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!
90 $font_size = $bits[$i];
93 $r = $this->info
['font-size']->validate(
94 $font_size, $config, $context);
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] === '/') {
110 $line_height = $bits[$j];
114 // slash already found
120 $r = $this->info
['line-height']->validate(
121 $line_height, $config, $context);
132 // attempting to catch font-family
135 implode(' ', array_slice($bits, $i, $size - $i));
136 $r = $this->info
['font-family']->validate(
137 $font_family, $config, $context);
140 // processing completed successfully
141 return rtrim($final);