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