]> git.wh0rd.org - tt-rss.git/blob - lib/htmlpurifier/library/HTMLPurifier/TagTransform/Font.php
quickAddFeed: remove oauth notice, mention you can paste site URL
[tt-rss.git] / lib / htmlpurifier / 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'] !== '') {
67 if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
68 $size = (int) $attr['size'];
69 if ($size < -2) $attr['size'] = '-2';
70 if ($size > 4) $attr['size'] = '+4';
71 } else {
72 $size = (int) $attr['size'];
73 if ($size > 7) $attr['size'] = '7';
74 }
75 }
76 if (isset($this->_size_lookup[$attr['size']])) {
77 $prepend_style .= 'font-size:' .
78 $this->_size_lookup[$attr['size']] . ';';
79 }
80 unset($attr['size']);
81 }
82
83 if ($prepend_style) {
84 $attr['style'] = isset($attr['style']) ?
85 $prepend_style . $attr['style'] :
86 $prepend_style;
87 }
88
89 $new_tag = clone $tag;
90 $new_tag->name = $this->transform_to;
91 $new_tag->attr = $attr;
92
93 return $new_tag;
94
95 }
96 }
97
98 // vim: et sw=4 sts=4