Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / CSS / Font.php
blob1b3b090503b522eed7d2ce7276f82fd0f642882b
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 /**
22 * System font keywords.
24 var $system_fonts = array(
25 'caption' => true,
26 'icon' => true,
27 'menu' => true,
28 'message-box' => true,
29 'small-caption' => true,
30 'status-bar' => 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;
63 switch ($stage) {
65 // attempting to catch font-style, font-variant or font-weight
66 case 0:
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);
71 if ($r !== false) {
72 $final .= $r . ' ';
73 $caught[$validator_name] = true;
74 break;
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
82 case 1:
83 $found_slash = false;
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!
89 $line_height = false;
90 $found_slash = true;
92 } else {
93 $font_size = $bits[$i];
94 $line_height = false;
96 $r = $this->info['font-size']->validate(
97 $font_size, $config, $context);
98 if ($r !== false) {
99 $final .= $r;
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] === '/') {
106 if ($found_slash) {
107 return false;
108 } else {
109 $found_slash = true;
110 continue;
113 $line_height = $bits[$j];
114 break;
116 } else {
117 // slash already found
118 $found_slash = true;
119 $j = $i;
121 if ($found_slash) {
122 $i = $j;
123 $r = $this->info['line-height']->validate(
124 $line_height, $config, $context);
125 if ($r !== false) {
126 $final .= '/' . $r;
129 $final .= ' ';
130 $stage = 2;
131 break;
133 return false;
135 // attempting to catch font-family
136 case 2:
137 $font_family =
138 implode(' ', array_slice($bits, $i, $size - $i));
139 $r = $this->info['font-family']->validate(
140 $font_family, $config, $context);
141 if ($r !== false) {
142 $final .= $r . ' ';
143 // processing completed successfully
144 return rtrim($final);
146 return false;
149 return false;