Implement Tables Module.
[htmlpurifier.git] / library / HTMLPurifier / Printer / HTMLDefinition.php
blobfb1a357fb331034986d6c0c8a134aa3ea4c6c365
1 <?php
3 require_once 'HTMLPurifier/Printer.php';
4 require_once 'HTMLPurifier/XHTMLDefinition.php';
6 class HTMLPurifier_Printer_HTMLDefinition extends HTMLPurifier_Printer
9 /**
10 * Instance of HTMLPurifier_HTMLDefinition, for easy access
12 var $def;
14 function render($config) {
15 $ret = '';
16 $this->config =& $config;
18 if (isset($_GET['x'])) { // hidden settings
19 $this->def = new HTMLPurifier_XHTMLDefinition($config);
20 $this->def->setup($config);
21 } else {
22 $this->def = $config->getHTMLDefinition();
24 $def =& $this->def;
26 $ret .= $this->start('div', array('class' => 'HTMLPurifier_Printer'));
27 $ret .= $this->start('table');
28 $ret .= $this->element('caption', 'Environment');
30 $ret .= $this->row('Parent of fragment', $def->info_parent);
31 $ret .= $this->renderChildren($def->info_parent_def->child);
32 $ret .= $this->row('Strict mode', $def->strict);
33 if ($def->strict) $ret .= $this->row('Block wrap name', $def->info_block_wrapper);
35 $ret .= $this->start('tr');
36 $ret .= $this->element('th', 'Global attributes');
37 $ret .= $this->element('td', $this->listifyAttr($def->info_global_attr),0,0);
38 $ret .= $this->end('tr');
40 $ret .= $this->start('tr');
41 $ret .= $this->element('th', 'Tag transforms');
42 $list = array();
43 foreach ($def->info_tag_transform as $old => $new) {
44 $new = $this->getClass($new, 'TagTransform_');
45 $list[] = "<$old> with $new";
47 $ret .= $this->element('td', $this->listify($list));
48 $ret .= $this->end('tr');
50 $ret .= $this->start('tr');
51 $ret .= $this->element('th', 'Pre-AttrTransform');
52 $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_pre));
53 $ret .= $this->end('tr');
55 $ret .= $this->start('tr');
56 $ret .= $this->element('th', 'Post-AttrTransform');
57 $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_post));
58 $ret .= $this->end('tr');
60 $ret .= $this->end('table');
63 $ret .= $this->renderInfo();
66 $ret .= $this->end('div');
68 return $ret;
71 /**
72 * Renders the Elements ($info) table
74 function renderInfo() {
75 $ret = '';
76 $ret .= $this->start('table');
77 $ret .= $this->element('caption', 'Elements ($info)');
78 ksort($this->def->info);
79 $ret .= $this->start('tr');
80 $ret .= $this->element('th', 'Allowed tags', array('colspan' => 2, 'class' => 'heavy'));
81 $ret .= $this->end('tr');
82 $ret .= $this->start('tr');
83 $ret .= $this->element('td', $this->listifyTagLookup($this->def->info), array('colspan' => 2));
84 $ret .= $this->end('tr');
85 foreach ($this->def->info as $name => $def) {
86 $ret .= $this->start('tr');
87 $ret .= $this->element('th', "<$name>", array('class'=>'heavy', 'colspan' => 2));
88 $ret .= $this->end('tr');
89 $ret .= $this->start('tr');
90 $ret .= $this->element('th', 'Inline content');
91 $ret .= $this->element('td', $def->descendants_are_inline ? 'Yes' : 'No');
92 $ret .= $this->end('tr');
93 if (!empty($def->excludes)) {
94 $ret .= $this->start('tr');
95 $ret .= $this->element('th', 'Excludes');
96 $ret .= $this->element('td', $this->listifyTagLookup($def->excludes));
97 $ret .= $this->end('tr');
99 if (!empty($def->attr_transform_pre)) {
100 $ret .= $this->start('tr');
101 $ret .= $this->element('th', 'Pre-AttrTransform');
102 $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_pre));
103 $ret .= $this->end('tr');
105 if (!empty($def->attr_transform_post)) {
106 $ret .= $this->start('tr');
107 $ret .= $this->element('th', 'Post-AttrTransform');
108 $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_post));
109 $ret .= $this->end('tr');
111 if (!empty($def->auto_close)) {
112 $ret .= $this->start('tr');
113 $ret .= $this->element('th', 'Auto closed by');
114 $ret .= $this->element('td', $this->listifyTagLookup($def->auto_close));
115 $ret .= $this->end('tr');
117 $ret .= $this->start('tr');
118 $ret .= $this->element('th', 'Allowed attributes');
119 $ret .= $this->element('td',$this->listifyAttr($def->attr),0,0);
120 $ret .= $this->end('tr');
122 $ret .= $this->renderChildren($def->child);
124 $ret .= $this->end('table');
125 return $ret;
128 /**
129 * Renders a row describing the allowed children of an element
130 * @param $def HTMLPurifier_ChildDef of pertinent element
132 function renderChildren($def) {
133 $context = new HTMLPurifier_Context();
134 $ret = '';
135 $ret .= $this->start('tr');
136 $elements = array();
137 $attr = array();
138 if (isset($def->elements)) {
139 if ($def->type == 'strictblockquote') $def->validateChildren(array(), $this->config, $context);
140 $elements = $def->elements;
141 } elseif ($def->type == 'chameleon') {
142 $attr['rowspan'] = 2;
143 } elseif ($def->type == 'empty') {
144 $elements = array();
145 } elseif ($def->type == 'table') {
146 $elements = array_flip(array('col', 'caption', 'colgroup', 'thead',
147 'tfoot', 'tbody', 'tr'));
149 $ret .= $this->element('th', 'Allowed children', $attr);
151 if ($def->type == 'chameleon') {
153 $ret .= $this->element('td',
154 '<em>Block</em>: ' .
155 $this->escape($this->listifyTagLookup($def->block->elements)),0,0);
156 $ret .= $this->end('tr');
157 $ret .= $this->start('tr');
158 $ret .= $this->element('td',
159 '<em>Inline</em>: ' .
160 $this->escape($this->listifyTagLookup($def->inline->elements)),0,0);
162 } else {
163 $ret .= $this->element('td',
164 '<em>'.ucfirst($def->type).'</em>: ' .
165 $this->escape($this->listifyTagLookup($elements)),0,0);
167 $ret .= $this->end('tr');
168 return $ret;
171 /**
172 * Listifies a tag lookup table.
173 * @param $array Tag lookup array in form of array('tagname' => true)
175 function listifyTagLookup($array) {
176 $list = array();
177 foreach ($array as $name => $discard) {
178 if ($name !== '#PCDATA' && !isset($this->def->info[$name])) continue;
179 $list[] = $name;
181 return $this->listify($list);
185 * Listifies a list of objects by retrieving class names and internal state
186 * @param $array List of objects
187 * @todo Also add information about internal state
189 function listifyObjectList($array) {
190 $list = array();
191 foreach ($array as $discard => $obj) {
192 $list[] = $this->getClass($obj, 'AttrTransform_');
194 return $this->listify($list);
198 * Listifies a hash of attributes to AttrDef classes
199 * @param $array Array hash in form of array('attrname' => HTMLPurifier_AttrDef)
201 function listifyAttr($array) {
202 $list = array();
203 foreach ($array as $name => $obj) {
204 if ($obj === false) continue;
205 $list[] = "$name&nbsp;=&nbsp;<i>" . $this->getClass($obj, 'AttrDef_') . '</i>';
207 return $this->listify($list);