fix image text
[pear] / HTML / Clean.php
1 <?php
2
3 /**
4  * This is a PHP implementation of the Roo HTMLEditorCore onPaste method - that cleans up HTML
5  * and replaces things like tables etc..
6  */
7
8 class HTML_Clean {
9     
10     static function fromHTML($str, $opts = array())
11     {
12         $str= self::cleanWordChars($str);
13         $dom = new DOMDocument('1.0', 'utf8');
14         $dom->loadHTML($str);
15         $opts['dom'] = $dom;
16         return new HTML_Clean($opts);    
17     }
18     static function cleanWordChars($str)
19     {
20         $swapCodes  = array(
21              8211 =>  "&#8211;" ,  
22              8212 =>  "&#8212;" ,  
23              8216 =>   "'" ,   
24              8217 =>  "'" ,   
25              8220 =>  '"' ,   
26              8221 =>  '"' ,   
27              8226 =>  "*" ,   
28              8230 =>  "..." 
29         );
30         foreach($swapCodes as $k=>$v) {
31             $str = str_replace(mb_chr($k), $v, $str);
32         }
33         return $str;
34     
35     }
36     
37     
38     var $dom; // Dom Document.
39     var $black = array(
40         'APPLET', // 
41         'BASE',   'BASEFONT', 'BGSOUND', 'BLINK',  'BODY', 
42         'FRAME',  'FRAMESET', 'HEAD',    'HTML',   'ILAYER', 
43         'IFRAME', 'LAYER',  'LINK',     'META',    'OBJECT',   
44         'SCRIPT', 'STYLE' ,'TITLE',  'XML',
45         //'FONT' // CLEAN LATER..
46         'COLGROUP', 'COL'   // messy tables.
47     ); // blacklist of elements.
48     
49     function __construct($opts)
50     {
51         foreach($opts as $k=>$v) {
52             $this->{$k} = $v;
53         }
54         $d = $this->dom->documentElement;
55         $this->filter('Word',array( 'node' =>  $d ));
56             
57         $this->filter('StyleToTag', array(
58             'node' =>  $d   // this could add nodes to tree, so not very good to nest the walk.
59             
60         ));
61         
62         $this->filter('Attributes',array(    // does walk as well.
63             'node' => $d,
64             'attrib_white' => array('href', 'src', 'name', 'align', 'colspan', 'rowspan', 'data-display', 'data-width', 'start'),
65             'attrib_clean' => array('href', 'src' ),
66             
67             'replaceComment' => true   // this is sneaked in here - as walk will get rid of comments at the same time.
68         ));
69         // is this used?!?!
70         $this->filter('Black', array( 'node' =>  $d, 'tag'  =>  $this->black ));
71         // we don't use the whitelist?
72         
73         
74         // should be fonts..
75         $this->filter('KeepChildren',array( 'node' =>  $d, 'tag'  =>   array(   'FONT', ':' )) );  
76         $this->filter('Paragraph',array( 'node' =>  $d ));
77         $this->filter('Span',array( 'node' =>  $d ));
78         $this->filter('LongBr',array( 'node' =>  $d ));
79          
80         $ar = $this->arrayFrom($d->getElementsByTagName('img'));
81         foreach($ar as $img) {
82             if ($this->findParent($img, 'figure')) {
83                 continue;
84             }
85             require_once 'HTML/Clean/BlockFigure.php';
86             $fig = new HTML_Clean_BlockFigure(array(
87                 'image_src' => $img->getAttribute('src')
88             ));
89             $fig->updateElement($img);
90             
91         }
92          
93         
94         
95         require_once 'HTML/Clean/Block.php';
96         HTML_Clean_Block::initAll($d);
97
98     }
99     
100     function filter($type, $args)
101     {
102         require_once 'HTML/Clean/Filter'. $type .'.php';
103         $cls = 'HTML_Clean_Filter'. $type;
104         new $cls($args);
105     }
106     
107     function toString()
108     {
109         $this->dom->saveHTML();
110     }
111     
112     
113 }