]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/TagTransform/Font.php
added index to config and pconfig table
[friendica.git] / library / HTMLPurifier / TagTransform / Font.php
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  * @warning This doesn't work completely correctly; specifically, this
15  *          TagTransform operates before well-formedness is enforced, so
16  *          the "active formatting elements" algorithm doesn't get applied.
17  */
18 class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
19 {
20
21     public $transform_to = 'span';
22
23     protected $_size_lookup = array(
24         '0' => 'xx-small',
25         '1' => 'xx-small',
26         '2' => 'small',
27         '3' => 'medium',
28         '4' => 'large',
29         '5' => 'x-large',
30         '6' => 'xx-large',
31         '7' => '300%',
32         '-1' => 'smaller',
33         '-2' => '60%',
34         '+1' => 'larger',
35         '+2' => '150%',
36         '+3' => '200%',
37         '+4' => '300%'
38     );
39
40     public function transform($tag, $config, $context) {
41
42         if ($tag instanceof HTMLPurifier_Token_End) {
43             $new_tag = clone $tag;
44             $new_tag->name = $this->transform_to;
45             return $new_tag;
46         }
47
48         $attr = $tag->attr;
49         $prepend_style = '';
50
51         // handle color transform
52         if (isset($attr['color'])) {
53             $prepend_style .= 'color:' . $attr['color'] . ';';
54             unset($attr['color']);
55         }
56
57         // handle face transform
58         if (isset($attr['face'])) {
59             $prepend_style .= 'font-family:' . $attr['face'] . ';';
60             unset($attr['face']);
61         }
62
63         // handle size transform
64         if (isset($attr['size'])) {
65             // normalize large numbers
66             if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
67                 $size = (int) $attr['size'];
68                 if ($size < -2) $attr['size'] = '-2';
69                 if ($size > 4)  $attr['size'] = '+4';
70             } else {
71                 $size = (int) $attr['size'];
72                 if ($size > 7) $attr['size'] = '7';
73             }
74             if (isset($this->_size_lookup[$attr['size']])) {
75                 $prepend_style .= 'font-size:' .
76                   $this->_size_lookup[$attr['size']] . ';';
77             }
78             unset($attr['size']);
79         }
80
81         if ($prepend_style) {
82             $attr['style'] = isset($attr['style']) ?
83                 $prepend_style . $attr['style'] :
84                 $prepend_style;
85         }
86
87         $new_tag = clone $tag;
88         $new_tag->name = $this->transform_to;
89         $new_tag->attr = $attr;
90
91         return $new_tag;
92
93     }
94 }
95
96 // vim: et sw=4 sts=4