Release 1.6.1, merged in 931 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / TagTransform / Font.php
blobdedaf8b245a069c584c5c4d1b31a0259d8bc1731
1 <?php
3 require_once 'HTMLPurifier/TagTransform.php';
5 /**
6 * Transforms FONT tags to the proper form (SPAN with CSS styling)
7 *
8 * This transformation takes the three proprietary attributes of FONT and
9 * transforms them into their corresponding CSS attributes. These are color,
10 * face, and size.
12 * @note Size is an interesting case because it doesn't map cleanly to CSS.
13 * Thanks to
14 * http://style.cleverchimp.com/font_size_intervals/altintervals.html
15 * for reasonable mappings.
17 class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
20 var $transform_to = 'span';
22 var $_size_lookup = array(
23 '0' => 'xx-small',
24 '1' => 'xx-small',
25 '2' => 'small',
26 '3' => 'medium',
27 '4' => 'large',
28 '5' => 'x-large',
29 '6' => 'xx-large',
30 '7' => '300%',
31 '-1' => 'smaller',
32 '-2' => '60%',
33 '+1' => 'larger',
34 '+2' => '150%',
35 '+3' => '200%',
36 '+4' => '300%'
39 function transform($tag, $config, &$context) {
41 if ($tag->type == 'end') {
42 $new_tag = new HTMLPurifier_Token_End($this->transform_to);
43 return $new_tag;
46 $attr = $tag->attr;
47 $prepend_style = '';
49 // handle color transform
50 if (isset($attr['color'])) {
51 $prepend_style .= 'color:' . $attr['color'] . ';';
52 unset($attr['color']);
55 // handle face transform
56 if (isset($attr['face'])) {
57 $prepend_style .= 'font-family:' . $attr['face'] . ';';
58 unset($attr['face']);
61 // handle size transform
62 if (isset($attr['size'])) {
63 // normalize large numbers
64 if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
65 $size = (int) $attr['size'];
66 if ($size < -2) $attr['size'] = '-2';
67 if ($size > 4) $attr['size'] = '+4';
68 } else {
69 $size = (int) $attr['size'];
70 if ($size > 7) $attr['size'] = '7';
72 if (isset($this->_size_lookup[$attr['size']])) {
73 $prepend_style .= 'font-size:' .
74 $this->_size_lookup[$attr['size']] . ';';
76 unset($attr['size']);
79 if ($prepend_style) {
80 $attr['style'] = isset($attr['style']) ?
81 $prepend_style . $attr['style'] :
82 $prepend_style;
85 $new_tag = $tag->copy();
86 $new_tag->name = $this->transform_to;
87 $new_tag->attr = $attr;
89 return $new_tag;