1 : <?php
2 :
3 : /**
4 : * Transforms FONT tags to the proper form (SPAN with CSS styling)
5 : *
6 : * This transformation takes the three proprietary attributes of FONT and
7 : * transforms them into their corresponding CSS attributes. These are color,
8 : * face, and size.
9 : *
10 : * @note Size is an interesting case because it doesn't map cleanly to CSS.
11 : * Thanks to
12 : * http://style.cleverchimp.com/font_size_intervals/altintervals.html
13 : * for reasonable mappings.
14 : */
15 1 : class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
16 : {
17 :
18 : public $transform_to = 'span';
19 :
20 : protected $_size_lookup = array(
21 : '0' => 'xx-small',
22 : '1' => 'xx-small',
23 : '2' => 'small',
24 : '3' => 'medium',
25 : '4' => 'large',
26 : '5' => 'x-large',
27 : '6' => 'xx-large',
28 : '7' => '300%',
29 : '-1' => 'smaller',
30 : '-2' => '60%',
31 : '+1' => 'larger',
32 : '+2' => '150%',
33 : '+3' => '200%',
34 : '+4' => '300%'
35 : );
36 :
37 : public function transform($tag, $config, $context) {
38 :
39 : if ($tag instanceof HTMLPurifier_Token_End) {
40 : $new_tag = clone $tag;
41 : $new_tag->name = $this->transform_to;
42 : return $new_tag;
43 : }
44 :
45 : $attr = $tag->attr;
46 : $prepend_style = '';
47 :
48 : // handle color transform
49 : if (isset($attr['color'])) {
50 : $prepend_style .= 'color:' . $attr['color'] . ';';
51 : unset($attr['color']);
52 : }
53 :
54 : // handle face transform
55 : if (isset($attr['face'])) {
56 : $prepend_style .= 'font-family:' . $attr['face'] . ';';
57 : unset($attr['face']);
58 : }
59 :
60 : // handle size transform
61 : if (isset($attr['size'])) {
62 : // normalize large numbers
63 : if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
64 : $size = (int) $attr['size'];
65 : if ($size < -2) $attr['size'] = '-2';
66 : if ($size > 4) $attr['size'] = '+4';
67 : } else {
68 : $size = (int) $attr['size'];
69 : if ($size > 7) $attr['size'] = '7';
70 : }
71 : if (isset($this->_size_lookup[$attr['size']])) {
72 : $prepend_style .= 'font-size:' .
73 : $this->_size_lookup[$attr['size']] . ';';
74 : }
75 : unset($attr['size']);
76 : }
77 :
78 : if ($prepend_style) {
79 : $attr['style'] = isset($attr['style']) ?
80 : $prepend_style . $attr['style'] :
81 : $prepend_style;
82 : }
83 :
84 : $new_tag = clone $tag;
85 : $new_tag->name = $this->transform_to;
86 : $new_tag->attr = $attr;
87 :
88 : return $new_tag;
89 :
90 : }
91 : }
92 :
|