]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * Transforms FONT tags to the proper form (SPAN with CSS styling)\r | |
5 | *\r | |
6 | * This transformation takes the three proprietary attributes of FONT and\r | |
7 | * transforms them into their corresponding CSS attributes. These are color,\r | |
8 | * face, and size.\r | |
9 | *\r | |
10 | * @note Size is an interesting case because it doesn't map cleanly to CSS.\r | |
11 | * Thanks to\r | |
12 | * http://style.cleverchimp.com/font_size_intervals/altintervals.html\r | |
13 | * for reasonable mappings.\r | |
14 | * @warning This doesn't work completely correctly; specifically, this\r | |
15 | * TagTransform operates before well-formedness is enforced, so\r | |
16 | * the "active formatting elements" algorithm doesn't get applied.\r | |
17 | */\r | |
18 | class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform\r | |
19 | {\r | |
20 | /**\r | |
21 | * @type string\r | |
22 | */\r | |
23 | public $transform_to = 'span';\r | |
24 | \r | |
25 | /**\r | |
26 | * @type array\r | |
27 | */\r | |
28 | protected $_size_lookup = array(\r | |
29 | '0' => 'xx-small',\r | |
30 | '1' => 'xx-small',\r | |
31 | '2' => 'small',\r | |
32 | '3' => 'medium',\r | |
33 | '4' => 'large',\r | |
34 | '5' => 'x-large',\r | |
35 | '6' => 'xx-large',\r | |
36 | '7' => '300%',\r | |
37 | '-1' => 'smaller',\r | |
38 | '-2' => '60%',\r | |
39 | '+1' => 'larger',\r | |
40 | '+2' => '150%',\r | |
41 | '+3' => '200%',\r | |
42 | '+4' => '300%'\r | |
43 | );\r | |
44 | \r | |
45 | /**\r | |
46 | * @param HTMLPurifier_Token_Tag $tag\r | |
47 | * @param HTMLPurifier_Config $config\r | |
48 | * @param HTMLPurifier_Context $context\r | |
49 | * @return HTMLPurifier_Token_End|string\r | |
50 | */\r | |
51 | public function transform($tag, $config, $context)\r | |
52 | {\r | |
53 | if ($tag instanceof HTMLPurifier_Token_End) {\r | |
54 | $new_tag = clone $tag;\r | |
55 | $new_tag->name = $this->transform_to;\r | |
56 | return $new_tag;\r | |
57 | }\r | |
58 | \r | |
59 | $attr = $tag->attr;\r | |
60 | $prepend_style = '';\r | |
61 | \r | |
62 | // handle color transform\r | |
63 | if (isset($attr['color'])) {\r | |
64 | $prepend_style .= 'color:' . $attr['color'] . ';';\r | |
65 | unset($attr['color']);\r | |
66 | }\r | |
67 | \r | |
68 | // handle face transform\r | |
69 | if (isset($attr['face'])) {\r | |
70 | $prepend_style .= 'font-family:' . $attr['face'] . ';';\r | |
71 | unset($attr['face']);\r | |
72 | }\r | |
73 | \r | |
74 | // handle size transform\r | |
75 | if (isset($attr['size'])) {\r | |
76 | // normalize large numbers\r | |
77 | if ($attr['size'] !== '') {\r | |
78 | if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {\r | |
79 | $size = (int)$attr['size'];\r | |
80 | if ($size < -2) {\r | |
81 | $attr['size'] = '-2';\r | |
82 | }\r | |
83 | if ($size > 4) {\r | |
84 | $attr['size'] = '+4';\r | |
85 | }\r | |
86 | } else {\r | |
87 | $size = (int)$attr['size'];\r | |
88 | if ($size > 7) {\r | |
89 | $attr['size'] = '7';\r | |
90 | }\r | |
91 | }\r | |
92 | }\r | |
93 | if (isset($this->_size_lookup[$attr['size']])) {\r | |
94 | $prepend_style .= 'font-size:' .\r | |
95 | $this->_size_lookup[$attr['size']] . ';';\r | |
96 | }\r | |
97 | unset($attr['size']);\r | |
98 | }\r | |
99 | \r | |
100 | if ($prepend_style) {\r | |
101 | $attr['style'] = isset($attr['style']) ?\r | |
102 | $prepend_style . $attr['style'] :\r | |
103 | $prepend_style;\r | |
104 | }\r | |
105 | \r | |
106 | $new_tag = clone $tag;\r | |
107 | $new_tag->name = $this->transform_to;\r | |
108 | $new_tag->attr = $attr;\r | |
109 | \r | |
110 | return $new_tag;\r | |
111 | }\r | |
112 | }\r | |
113 | \r | |
114 | // vim: et sw=4 sts=4\r |