Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Printer / HTMLDefinition.php
blob4a0a90f1525c74460942794e23f1e9d5b1010539
1 <?php
3 require_once 'HTMLPurifier/Printer.php';
5 class HTMLPurifier_Printer_HTMLDefinition extends HTMLPurifier_Printer
8 /**
9 * Instance of HTMLPurifier_HTMLDefinition, for easy access
11 var $def;
13 function render($config) {
14 $ret = '';
15 $this->config =& $config;
17 $this->def = $config->getHTMLDefinition();
19 $ret .= $this->start('div', array('class' => 'HTMLPurifier_Printer'));
21 $ret .= $this->renderDoctype();
22 $ret .= $this->renderEnvironment();
23 $ret .= $this->renderContentSets();
24 $ret .= $this->renderInfo();
26 $ret .= $this->end('div');
28 return $ret;
31 /**
32 * Renders the Doctype table
34 function renderDoctype() {
35 $doctype = $this->def->doctype;
36 $ret = '';
37 $ret .= $this->start('table');
38 $ret .= $this->element('caption', 'Doctype');
39 $ret .= $this->row('Name', $doctype->name);
40 $ret .= $this->row('XML', $doctype->xml ? 'Yes' : 'No');
41 $ret .= $this->row('Default Modules', implode($doctype->modules, ', '));
42 $ret .= $this->row('Default Tidy Modules', implode($doctype->tidyModules, ', '));
43 $ret .= $this->end('table');
44 return $ret;
48 /**
49 * Renders environment table, which is miscellaneous info
51 function renderEnvironment() {
52 $def = $this->def;
54 $ret = '';
56 $ret .= $this->start('table');
57 $ret .= $this->element('caption', 'Environment');
59 $ret .= $this->row('Parent of fragment', $def->info_parent);
60 $ret .= $this->renderChildren($def->info_parent_def->child);
61 $ret .= $this->row('Block wrap name', $def->info_block_wrapper);
63 $ret .= $this->start('tr');
64 $ret .= $this->element('th', 'Global attributes');
65 $ret .= $this->element('td', $this->listifyAttr($def->info_global_attr),0,0);
66 $ret .= $this->end('tr');
68 $ret .= $this->start('tr');
69 $ret .= $this->element('th', 'Tag transforms');
70 $list = array();
71 foreach ($def->info_tag_transform as $old => $new) {
72 $new = $this->getClass($new, 'TagTransform_');
73 $list[] = "<$old> with $new";
75 $ret .= $this->element('td', $this->listify($list));
76 $ret .= $this->end('tr');
78 $ret .= $this->start('tr');
79 $ret .= $this->element('th', 'Pre-AttrTransform');
80 $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_pre));
81 $ret .= $this->end('tr');
83 $ret .= $this->start('tr');
84 $ret .= $this->element('th', 'Post-AttrTransform');
85 $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_post));
86 $ret .= $this->end('tr');
88 $ret .= $this->end('table');
89 return $ret;
92 /**
93 * Renders the Content Sets table
95 function renderContentSets() {
96 $ret = '';
97 $ret .= $this->start('table');
98 $ret .= $this->element('caption', 'Content Sets');
99 foreach ($this->def->info_content_sets as $name => $lookup) {
100 $ret .= $this->heavyHeader($name);
101 $ret .= $this->start('tr');
102 $ret .= $this->element('td', $this->listifyTagLookup($lookup));
103 $ret .= $this->end('tr');
105 $ret .= $this->end('table');
106 return $ret;
110 * Renders the Elements ($info) table
112 function renderInfo() {
113 $ret = '';
114 $ret .= $this->start('table');
115 $ret .= $this->element('caption', 'Elements ($info)');
116 ksort($this->def->info);
117 $ret .= $this->heavyHeader('Allowed tags', 2);
118 $ret .= $this->start('tr');
119 $ret .= $this->element('td', $this->listifyTagLookup($this->def->info), array('colspan' => 2));
120 $ret .= $this->end('tr');
121 foreach ($this->def->info as $name => $def) {
122 $ret .= $this->start('tr');
123 $ret .= $this->element('th', "<$name>" . ($def->safe ? '' : ' (unsafe)'), array('class'=>'heavy' . ($def->safe ? '' : ' unsafe'), 'colspan' => 2));
124 $ret .= $this->end('tr');
125 $ret .= $this->start('tr');
126 $ret .= $this->element('th', 'Inline content');
127 $ret .= $this->element('td', $def->descendants_are_inline ? 'Yes' : 'No');
128 $ret .= $this->end('tr');
129 if (!empty($def->excludes)) {
130 $ret .= $this->start('tr');
131 $ret .= $this->element('th', 'Excludes');
132 $ret .= $this->element('td', $this->listifyTagLookup($def->excludes));
133 $ret .= $this->end('tr');
135 if (!empty($def->attr_transform_pre)) {
136 $ret .= $this->start('tr');
137 $ret .= $this->element('th', 'Pre-AttrTransform');
138 $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_pre));
139 $ret .= $this->end('tr');
141 if (!empty($def->attr_transform_post)) {
142 $ret .= $this->start('tr');
143 $ret .= $this->element('th', 'Post-AttrTransform');
144 $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_post));
145 $ret .= $this->end('tr');
147 if (!empty($def->auto_close)) {
148 $ret .= $this->start('tr');
149 $ret .= $this->element('th', 'Auto closed by');
150 $ret .= $this->element('td', $this->listifyTagLookup($def->auto_close));
151 $ret .= $this->end('tr');
153 $ret .= $this->start('tr');
154 $ret .= $this->element('th', 'Allowed attributes');
155 $ret .= $this->element('td',$this->listifyAttr($def->attr), array(), 0);
156 $ret .= $this->end('tr');
158 if (!empty($def->required_attr)) {
159 $ret .= $this->row('Required attributes', $this->listify($def->required_attr));
162 $ret .= $this->renderChildren($def->child);
164 $ret .= $this->end('table');
165 return $ret;
168 /**
169 * Renders a row describing the allowed children of an element
170 * @param $def HTMLPurifier_ChildDef of pertinent element
172 function renderChildren($def) {
173 $context = new HTMLPurifier_Context();
174 $ret = '';
175 $ret .= $this->start('tr');
176 $elements = array();
177 $attr = array();
178 if (isset($def->elements)) {
179 if ($def->type == 'strictblockquote') {
180 $def->validateChildren(array(), $this->config, $context);
182 $elements = $def->elements;
184 if ($def->type == 'chameleon') {
185 $attr['rowspan'] = 2;
186 } elseif ($def->type == 'empty') {
187 $elements = array();
188 } elseif ($def->type == 'table') {
189 $elements = array_flip(array('col', 'caption', 'colgroup', 'thead',
190 'tfoot', 'tbody', 'tr'));
192 $ret .= $this->element('th', 'Allowed children', $attr);
194 if ($def->type == 'chameleon') {
196 $ret .= $this->element('td',
197 '<em>Block</em>: ' .
198 $this->escape($this->listifyTagLookup($def->block->elements)),0,0);
199 $ret .= $this->end('tr');
200 $ret .= $this->start('tr');
201 $ret .= $this->element('td',
202 '<em>Inline</em>: ' .
203 $this->escape($this->listifyTagLookup($def->inline->elements)),0,0);
205 } elseif ($def->type == 'custom') {
207 $ret .= $this->element('td', '<em>'.ucfirst($def->type).'</em>: ' .
208 $def->dtd_regex);
210 } else {
211 $ret .= $this->element('td',
212 '<em>'.ucfirst($def->type).'</em>: ' .
213 $this->escape($this->listifyTagLookup($elements)),0,0);
215 $ret .= $this->end('tr');
216 return $ret;
219 /**
220 * Listifies a tag lookup table.
221 * @param $array Tag lookup array in form of array('tagname' => true)
223 function listifyTagLookup($array) {
224 ksort($array);
225 $list = array();
226 foreach ($array as $name => $discard) {
227 if ($name !== '#PCDATA' && !isset($this->def->info[$name])) continue;
228 $list[] = $name;
230 return $this->listify($list);
234 * Listifies a list of objects by retrieving class names and internal state
235 * @param $array List of objects
236 * @todo Also add information about internal state
238 function listifyObjectList($array) {
239 ksort($array);
240 $list = array();
241 foreach ($array as $discard => $obj) {
242 $list[] = $this->getClass($obj, 'AttrTransform_');
244 return $this->listify($list);
248 * Listifies a hash of attributes to AttrDef classes
249 * @param $array Array hash in form of array('attrname' => HTMLPurifier_AttrDef)
251 function listifyAttr($array) {
252 ksort($array);
253 $list = array();
254 foreach ($array as $name => $obj) {
255 if ($obj === false) continue;
256 $list[] = "$name&nbsp;=&nbsp;<i>" . $this->getClass($obj, 'AttrDef_') . '</i>';
258 return $this->listify($list);
262 * Creates a heavy header row
264 function heavyHeader($text, $num = 1) {
265 $ret = '';
266 $ret .= $this->start('tr');
267 $ret .= $this->element('th', $text, array('colspan' => $num, 'class' => 'heavy'));
268 $ret .= $this->end('tr');
269 return $ret;