HTMLPurifier 4.4.0
|
00001 <?php 00002 00018 class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform 00019 { 00020 00021 public $transform_to = 'span'; 00022 00023 protected $_size_lookup = array( 00024 '0' => 'xx-small', 00025 '1' => 'xx-small', 00026 '2' => 'small', 00027 '3' => 'medium', 00028 '4' => 'large', 00029 '5' => 'x-large', 00030 '6' => 'xx-large', 00031 '7' => '300%', 00032 '-1' => 'smaller', 00033 '-2' => '60%', 00034 '+1' => 'larger', 00035 '+2' => '150%', 00036 '+3' => '200%', 00037 '+4' => '300%' 00038 ); 00039 00040 public function transform($tag, $config, $context) { 00041 00042 if ($tag instanceof HTMLPurifier_Token_End) { 00043 $new_tag = clone $tag; 00044 $new_tag->name = $this->transform_to; 00045 return $new_tag; 00046 } 00047 00048 $attr = $tag->attr; 00049 $prepend_style = ''; 00050 00051 // handle color transform 00052 if (isset($attr['color'])) { 00053 $prepend_style .= 'color:' . $attr['color'] . ';'; 00054 unset($attr['color']); 00055 } 00056 00057 // handle face transform 00058 if (isset($attr['face'])) { 00059 $prepend_style .= 'font-family:' . $attr['face'] . ';'; 00060 unset($attr['face']); 00061 } 00062 00063 // handle size transform 00064 if (isset($attr['size'])) { 00065 // normalize large numbers 00066 if ($attr['size'] !== '') { 00067 if ($attr['size']{0} == '+' || $attr['size']{0} == '-') { 00068 $size = (int) $attr['size']; 00069 if ($size < -2) $attr['size'] = '-2'; 00070 if ($size > 4) $attr['size'] = '+4'; 00071 } else { 00072 $size = (int) $attr['size']; 00073 if ($size > 7) $attr['size'] = '7'; 00074 } 00075 } 00076 if (isset($this->_size_lookup[$attr['size']])) { 00077 $prepend_style .= 'font-size:' . 00078 $this->_size_lookup[$attr['size']] . ';'; 00079 } 00080 unset($attr['size']); 00081 } 00082 00083 if ($prepend_style) { 00084 $attr['style'] = isset($attr['style']) ? 00085 $prepend_style . $attr['style'] : 00086 $prepend_style; 00087 } 00088 00089 $new_tag = clone $tag; 00090 $new_tag->name = $this->transform_to; 00091 $new_tag->attr = $attr; 00092 00093 return $new_tag; 00094 00095 } 00096 } 00097 00098 // vim: et sw=4 sts=4