File indexing completed on 2024-12-22 05:36:20

0001 <?php
0002 
0003 /**
0004  * Transforms FONT tags to the proper form (SPAN with CSS styling)
0005  *
0006  * This transformation takes the three proprietary attributes of FONT and
0007  * transforms them into their corresponding CSS attributes.  These are color,
0008  * face, and size.
0009  *
0010  * @note Size is an interesting case because it doesn't map cleanly to CSS.
0011  *       Thanks to
0012  *       http://style.cleverchimp.com/font_size_intervals/altintervals.html
0013  *       for reasonable mappings.
0014  * @warning This doesn't work completely correctly; specifically, this
0015  *          TagTransform operates before well-formedness is enforced, so
0016  *          the "active formatting elements" algorithm doesn't get applied.
0017  */
0018 class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
0019 {
0020     /**
0021      * @type string
0022      */
0023     public $transform_to = 'span';
0024 
0025     /**
0026      * @type array
0027      */
0028     protected $_size_lookup = array(
0029         '0' => 'xx-small',
0030         '1' => 'xx-small',
0031         '2' => 'small',
0032         '3' => 'medium',
0033         '4' => 'large',
0034         '5' => 'x-large',
0035         '6' => 'xx-large',
0036         '7' => '300%',
0037         '-1' => 'smaller',
0038         '-2' => '60%',
0039         '+1' => 'larger',
0040         '+2' => '150%',
0041         '+3' => '200%',
0042         '+4' => '300%'
0043     );
0044 
0045     /**
0046      * @param HTMLPurifier_Token_Tag $tag
0047      * @param HTMLPurifier_Config $config
0048      * @param HTMLPurifier_Context $context
0049      * @return HTMLPurifier_Token_End|string
0050      */
0051     public function transform($tag, $config, $context)
0052     {
0053         if ($tag instanceof HTMLPurifier_Token_End) {
0054             $new_tag = clone $tag;
0055             $new_tag->name = $this->transform_to;
0056             return $new_tag;
0057         }
0058 
0059         $attr = $tag->attr;
0060         $prepend_style = '';
0061 
0062         // handle color transform
0063         if (isset($attr['color'])) {
0064             $prepend_style .= 'color:' . $attr['color'] . ';';
0065             unset($attr['color']);
0066         }
0067 
0068         // handle face transform
0069         if (isset($attr['face'])) {
0070             $prepend_style .= 'font-family:' . $attr['face'] . ';';
0071             unset($attr['face']);
0072         }
0073 
0074         // handle size transform
0075         if (isset($attr['size'])) {
0076             // normalize large numbers
0077             if ($attr['size'] !== '') {
0078                 if ($attr['size']{0} == '+' || $attr['size']{0} == '-') {
0079                     $size = (int)$attr['size'];
0080                     if ($size < -2) {
0081                         $attr['size'] = '-2';
0082                     }
0083                     if ($size > 4) {
0084                         $attr['size'] = '+4';
0085                     }
0086                 } else {
0087                     $size = (int)$attr['size'];
0088                     if ($size > 7) {
0089                         $attr['size'] = '7';
0090                     }
0091                 }
0092             }
0093             if (isset($this->_size_lookup[$attr['size']])) {
0094                 $prepend_style .= 'font-size:' .
0095                     $this->_size_lookup[$attr['size']] . ';';
0096             }
0097             unset($attr['size']);
0098         }
0099 
0100         if ($prepend_style) {
0101             $attr['style'] = isset($attr['style']) ?
0102                 $prepend_style . $attr['style'] :
0103                 $prepend_style;
0104         }
0105 
0106         $new_tag = clone $tag;
0107         $new_tag->name = $this->transform_to;
0108         $new_tag->attr = $attr;
0109 
0110         return $new_tag;
0111     }
0112 }
0113 
0114 // vim: et sw=4 sts=4