Deal with old libxml incompatibilities.
[htmlpurifier.git] / library / HTMLPurifier / TagTransform / Font.php
blob7853d90bc64f08ca0900853ba53906a7f101b0cf
1 <?php
3 /**
4 * Transforms FONT tags to the proper form (SPAN with CSS styling)
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.
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.
18 class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
20 /**
21 * @type string
23 public $transform_to = 'span';
25 /**
26 * @type array
28 protected $_size_lookup = array(
29 '0' => 'xx-small',
30 '1' => 'xx-small',
31 '2' => 'small',
32 '3' => 'medium',
33 '4' => 'large',
34 '5' => 'x-large',
35 '6' => 'xx-large',
36 '7' => '300%',
37 '-1' => 'smaller',
38 '-2' => '60%',
39 '+1' => 'larger',
40 '+2' => '150%',
41 '+3' => '200%',
42 '+4' => '300%'
45 /**
46 * @param HTMLPurifier_Token_Tag $tag
47 * @param HTMLPurifier_Config $config
48 * @param HTMLPurifier_Context $context
49 * @return HTMLPurifier_Token_End|string
51 public function transform($tag, $config, $context)
53 if ($tag instanceof HTMLPurifier_Token_End) {
54 $new_tag = clone $tag;
55 $new_tag->name = $this->transform_to;
56 return $new_tag;
59 $attr = $tag->attr;
60 $prepend_style = '';
62 // handle color transform
63 if (isset($attr['color'])) {
64 $prepend_style .= 'color:' . $attr['color'] . ';';
65 unset($attr['color']);
68 // handle face transform
69 if (isset($attr['face'])) {
70 $prepend_style .= 'font-family:' . $attr['face'] . ';';
71 unset($attr['face']);
74 // handle size transform
75 if (isset($attr['size'])) {
76 // normalize large numbers
77 if ($attr['size'] !== '') {
78 if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
79 $size = (int)$attr['size'];
80 if ($size < -2) {
81 $attr['size'] = '-2';
83 if ($size > 4) {
84 $attr['size'] = '+4';
86 } else {
87 $size = (int)$attr['size'];
88 if ($size > 7) {
89 $attr['size'] = '7';
93 if (isset($this->_size_lookup[$attr['size']])) {
94 $prepend_style .= 'font-size:' .
95 $this->_size_lookup[$attr['size']] . ';';
97 unset($attr['size']);
100 if ($prepend_style) {
101 $attr['style'] = isset($attr['style']) ?
102 $prepend_style . $attr['style'] :
103 $prepend_style;
106 $new_tag = clone $tag;
107 $new_tag->name = $this->transform_to;
108 $new_tag->attr = $attr;
110 return $new_tag;
114 // vim: et sw=4 sts=4