HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/Printer/HTMLDefinition.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class HTMLPurifier_Printer_HTMLDefinition extends HTMLPurifier_Printer
00004 {
00005 
00009     protected $def;
00010 
00011     public function render($config) {
00012         $ret = '';
00013         $this->config =& $config;
00014 
00015         $this->def = $config->getHTMLDefinition();
00016 
00017         $ret .= $this->start('div', array('class' => 'HTMLPurifier_Printer'));
00018 
00019         $ret .= $this->renderDoctype();
00020         $ret .= $this->renderEnvironment();
00021         $ret .= $this->renderContentSets();
00022         $ret .= $this->renderInfo();
00023 
00024         $ret .= $this->end('div');
00025 
00026         return $ret;
00027     }
00028 
00032     protected function renderDoctype() {
00033         $doctype = $this->def->doctype;
00034         $ret = '';
00035         $ret .= $this->start('table');
00036         $ret .= $this->element('caption', 'Doctype');
00037         $ret .= $this->row('Name', $doctype->name);
00038         $ret .= $this->row('XML', $doctype->xml ? 'Yes' : 'No');
00039         $ret .= $this->row('Default Modules', implode($doctype->modules, ', '));
00040         $ret .= $this->row('Default Tidy Modules', implode($doctype->tidyModules, ', '));
00041         $ret .= $this->end('table');
00042         return $ret;
00043     }
00044 
00045 
00049     protected function renderEnvironment() {
00050         $def = $this->def;
00051 
00052         $ret = '';
00053 
00054         $ret .= $this->start('table');
00055         $ret .= $this->element('caption', 'Environment');
00056 
00057         $ret .= $this->row('Parent of fragment', $def->info_parent);
00058         $ret .= $this->renderChildren($def->info_parent_def->child);
00059         $ret .= $this->row('Block wrap name', $def->info_block_wrapper);
00060 
00061         $ret .= $this->start('tr');
00062             $ret .= $this->element('th', 'Global attributes');
00063             $ret .= $this->element('td', $this->listifyAttr($def->info_global_attr),0,0);
00064         $ret .= $this->end('tr');
00065 
00066         $ret .= $this->start('tr');
00067             $ret .= $this->element('th', 'Tag transforms');
00068             $list = array();
00069             foreach ($def->info_tag_transform as $old => $new) {
00070                 $new = $this->getClass($new, 'TagTransform_');
00071                 $list[] = "<$old> with $new";
00072             }
00073             $ret .= $this->element('td', $this->listify($list));
00074         $ret .= $this->end('tr');
00075 
00076         $ret .= $this->start('tr');
00077             $ret .= $this->element('th', 'Pre-AttrTransform');
00078             $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_pre));
00079         $ret .= $this->end('tr');
00080 
00081         $ret .= $this->start('tr');
00082             $ret .= $this->element('th', 'Post-AttrTransform');
00083             $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_post));
00084         $ret .= $this->end('tr');
00085 
00086         $ret .= $this->end('table');
00087         return $ret;
00088     }
00089 
00093     protected function renderContentSets() {
00094         $ret = '';
00095         $ret .= $this->start('table');
00096         $ret .= $this->element('caption', 'Content Sets');
00097         foreach ($this->def->info_content_sets as $name => $lookup) {
00098             $ret .= $this->heavyHeader($name);
00099             $ret .= $this->start('tr');
00100             $ret .= $this->element('td', $this->listifyTagLookup($lookup));
00101             $ret .= $this->end('tr');
00102         }
00103         $ret .= $this->end('table');
00104         return $ret;
00105     }
00106 
00110     protected function renderInfo() {
00111         $ret = '';
00112         $ret .= $this->start('table');
00113         $ret .= $this->element('caption', 'Elements ($info)');
00114         ksort($this->def->info);
00115         $ret .= $this->heavyHeader('Allowed tags', 2);
00116         $ret .= $this->start('tr');
00117         $ret .= $this->element('td', $this->listifyTagLookup($this->def->info), array('colspan' => 2));
00118         $ret .= $this->end('tr');
00119         foreach ($this->def->info as $name => $def) {
00120             $ret .= $this->start('tr');
00121                 $ret .= $this->element('th', "<$name>", array('class'=>'heavy', 'colspan' => 2));
00122             $ret .= $this->end('tr');
00123             $ret .= $this->start('tr');
00124                 $ret .= $this->element('th', 'Inline content');
00125                 $ret .= $this->element('td', $def->descendants_are_inline ? 'Yes' : 'No');
00126             $ret .= $this->end('tr');
00127             if (!empty($def->excludes)) {
00128                 $ret .= $this->start('tr');
00129                     $ret .= $this->element('th', 'Excludes');
00130                     $ret .= $this->element('td', $this->listifyTagLookup($def->excludes));
00131                 $ret .= $this->end('tr');
00132             }
00133             if (!empty($def->attr_transform_pre)) {
00134                 $ret .= $this->start('tr');
00135                     $ret .= $this->element('th', 'Pre-AttrTransform');
00136                     $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_pre));
00137                 $ret .= $this->end('tr');
00138             }
00139             if (!empty($def->attr_transform_post)) {
00140                 $ret .= $this->start('tr');
00141                     $ret .= $this->element('th', 'Post-AttrTransform');
00142                     $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_post));
00143                 $ret .= $this->end('tr');
00144             }
00145             if (!empty($def->auto_close)) {
00146                 $ret .= $this->start('tr');
00147                     $ret .= $this->element('th', 'Auto closed by');
00148                     $ret .= $this->element('td', $this->listifyTagLookup($def->auto_close));
00149                 $ret .= $this->end('tr');
00150             }
00151             $ret .= $this->start('tr');
00152                 $ret .= $this->element('th', 'Allowed attributes');
00153                 $ret .= $this->element('td',$this->listifyAttr($def->attr), array(), 0);
00154             $ret .= $this->end('tr');
00155 
00156             if (!empty($def->required_attr)) {
00157                 $ret .= $this->row('Required attributes', $this->listify($def->required_attr));
00158             }
00159 
00160             $ret .= $this->renderChildren($def->child);
00161         }
00162         $ret .= $this->end('table');
00163         return $ret;
00164     }
00165 
00170     protected function renderChildren($def) {
00171         $context = new HTMLPurifier_Context();
00172         $ret = '';
00173         $ret .= $this->start('tr');
00174             $elements = array();
00175             $attr = array();
00176             if (isset($def->elements)) {
00177                 if ($def->type == 'strictblockquote') {
00178                     $def->validateChildren(array(), $this->config, $context);
00179                 }
00180                 $elements = $def->elements;
00181             }
00182             if ($def->type == 'chameleon') {
00183                 $attr['rowspan'] = 2;
00184             } elseif ($def->type == 'empty') {
00185                 $elements = array();
00186             } elseif ($def->type == 'table') {
00187                 $elements = array_flip(array('col', 'caption', 'colgroup', 'thead',
00188                     'tfoot', 'tbody', 'tr'));
00189             }
00190             $ret .= $this->element('th', 'Allowed children', $attr);
00191 
00192             if ($def->type == 'chameleon') {
00193 
00194                 $ret .= $this->element('td',
00195                     '<em>Block</em>: ' .
00196                     $this->escape($this->listifyTagLookup($def->block->elements)),0,0);
00197                 $ret .= $this->end('tr');
00198                 $ret .= $this->start('tr');
00199                 $ret .= $this->element('td',
00200                     '<em>Inline</em>: ' .
00201                     $this->escape($this->listifyTagLookup($def->inline->elements)),0,0);
00202 
00203             } elseif ($def->type == 'custom') {
00204 
00205                 $ret .= $this->element('td', '<em>'.ucfirst($def->type).'</em>: ' .
00206                     $def->dtd_regex);
00207 
00208             } else {
00209                 $ret .= $this->element('td',
00210                     '<em>'.ucfirst($def->type).'</em>: ' .
00211                     $this->escape($this->listifyTagLookup($elements)),0,0);
00212             }
00213         $ret .= $this->end('tr');
00214         return $ret;
00215     }
00216 
00221     protected function listifyTagLookup($array) {
00222         ksort($array);
00223         $list = array();
00224         foreach ($array as $name => $discard) {
00225             if ($name !== '#PCDATA' && !isset($this->def->info[$name])) continue;
00226             $list[] = $name;
00227         }
00228         return $this->listify($list);
00229     }
00230 
00236     protected function listifyObjectList($array) {
00237         ksort($array);
00238         $list = array();
00239         foreach ($array as $discard => $obj) {
00240             $list[] = $this->getClass($obj, 'AttrTransform_');
00241         }
00242         return $this->listify($list);
00243     }
00244 
00249     protected function listifyAttr($array) {
00250         ksort($array);
00251         $list = array();
00252         foreach ($array as $name => $obj) {
00253             if ($obj === false) continue;
00254             $list[] = "$name&nbsp;=&nbsp;<i>" . $this->getClass($obj, 'AttrDef_') . '</i>';
00255         }
00256         return $this->listify($list);
00257     }
00258 
00262     protected function heavyHeader($text, $num = 1) {
00263         $ret = '';
00264         $ret .= $this->start('tr');
00265         $ret .= $this->element('th', $text, array('colspan' => $num, 'class' => 'heavy'));
00266         $ret .= $this->end('tr');
00267         return $ret;
00268     }
00269 
00270 }
00271 
00272 // vim: et sw=4 sts=4