HTMLDefinition.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. class HTMLPurifier_Printer_HTMLDefinition extends HTMLPurifier_Printer
  3. {
  4. /**
  5. * @type HTMLPurifier_HTMLDefinition, for easy access
  6. */
  7. protected $def;
  8. /**
  9. * @param HTMLPurifier_Config $config
  10. * @return string
  11. */
  12. public function render($config)
  13. {
  14. $ret = '';
  15. $this->config =& $config;
  16. $this->def = $config->getHTMLDefinition();
  17. $ret .= $this->start('div', array('class' => 'HTMLPurifier_Printer'));
  18. $ret .= $this->renderDoctype();
  19. $ret .= $this->renderEnvironment();
  20. $ret .= $this->renderContentSets();
  21. $ret .= $this->renderInfo();
  22. $ret .= $this->end('div');
  23. return $ret;
  24. }
  25. /**
  26. * Renders the Doctype table
  27. * @return string
  28. */
  29. protected function renderDoctype()
  30. {
  31. $doctype = $this->def->doctype;
  32. $ret = '';
  33. $ret .= $this->start('table');
  34. $ret .= $this->element('caption', 'Doctype');
  35. $ret .= $this->row('Name', $doctype->name);
  36. $ret .= $this->row('XML', $doctype->xml ? 'Yes' : 'No');
  37. $ret .= $this->row('Default Modules', implode(', ', $doctype->modules));
  38. $ret .= $this->row('Default Tidy Modules', implode(', ', $doctype->tidyModules));
  39. $ret .= $this->end('table');
  40. return $ret;
  41. }
  42. /**
  43. * Renders environment table, which is miscellaneous info
  44. * @return string
  45. */
  46. protected function renderEnvironment()
  47. {
  48. $def = $this->def;
  49. $ret = '';
  50. $ret .= $this->start('table');
  51. $ret .= $this->element('caption', 'Environment');
  52. $ret .= $this->row('Parent of fragment', $def->info_parent);
  53. $ret .= $this->renderChildren($def->info_parent_def->child);
  54. $ret .= $this->row('Block wrap name', $def->info_block_wrapper);
  55. $ret .= $this->start('tr');
  56. $ret .= $this->element('th', 'Global attributes');
  57. $ret .= $this->element('td', $this->listifyAttr($def->info_global_attr), null, 0);
  58. $ret .= $this->end('tr');
  59. $ret .= $this->start('tr');
  60. $ret .= $this->element('th', 'Tag transforms');
  61. $list = array();
  62. foreach ($def->info_tag_transform as $old => $new) {
  63. $new = $this->getClass($new, 'TagTransform_');
  64. $list[] = "<$old> with $new";
  65. }
  66. $ret .= $this->element('td', $this->listify($list));
  67. $ret .= $this->end('tr');
  68. $ret .= $this->start('tr');
  69. $ret .= $this->element('th', 'Pre-AttrTransform');
  70. $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_pre));
  71. $ret .= $this->end('tr');
  72. $ret .= $this->start('tr');
  73. $ret .= $this->element('th', 'Post-AttrTransform');
  74. $ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_post));
  75. $ret .= $this->end('tr');
  76. $ret .= $this->end('table');
  77. return $ret;
  78. }
  79. /**
  80. * Renders the Content Sets table
  81. * @return string
  82. */
  83. protected function renderContentSets()
  84. {
  85. $ret = '';
  86. $ret .= $this->start('table');
  87. $ret .= $this->element('caption', 'Content Sets');
  88. foreach ($this->def->info_content_sets as $name => $lookup) {
  89. $ret .= $this->heavyHeader($name);
  90. $ret .= $this->start('tr');
  91. $ret .= $this->element('td', $this->listifyTagLookup($lookup));
  92. $ret .= $this->end('tr');
  93. }
  94. $ret .= $this->end('table');
  95. return $ret;
  96. }
  97. /**
  98. * Renders the Elements ($info) table
  99. * @return string
  100. */
  101. protected function renderInfo()
  102. {
  103. $ret = '';
  104. $ret .= $this->start('table');
  105. $ret .= $this->element('caption', 'Elements ($info)');
  106. ksort($this->def->info);
  107. $ret .= $this->heavyHeader('Allowed tags', 2);
  108. $ret .= $this->start('tr');
  109. $ret .= $this->element('td', $this->listifyTagLookup($this->def->info), array('colspan' => 2));
  110. $ret .= $this->end('tr');
  111. foreach ($this->def->info as $name => $def) {
  112. $ret .= $this->start('tr');
  113. $ret .= $this->element('th', "<$name>", array('class' => 'heavy', 'colspan' => 2));
  114. $ret .= $this->end('tr');
  115. $ret .= $this->start('tr');
  116. $ret .= $this->element('th', 'Inline content');
  117. $ret .= $this->element('td', $def->descendants_are_inline ? 'Yes' : 'No');
  118. $ret .= $this->end('tr');
  119. if (!empty($def->excludes)) {
  120. $ret .= $this->start('tr');
  121. $ret .= $this->element('th', 'Excludes');
  122. $ret .= $this->element('td', $this->listifyTagLookup($def->excludes));
  123. $ret .= $this->end('tr');
  124. }
  125. if (!empty($def->attr_transform_pre)) {
  126. $ret .= $this->start('tr');
  127. $ret .= $this->element('th', 'Pre-AttrTransform');
  128. $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_pre));
  129. $ret .= $this->end('tr');
  130. }
  131. if (!empty($def->attr_transform_post)) {
  132. $ret .= $this->start('tr');
  133. $ret .= $this->element('th', 'Post-AttrTransform');
  134. $ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_post));
  135. $ret .= $this->end('tr');
  136. }
  137. if (!empty($def->auto_close)) {
  138. $ret .= $this->start('tr');
  139. $ret .= $this->element('th', 'Auto closed by');
  140. $ret .= $this->element('td', $this->listifyTagLookup($def->auto_close));
  141. $ret .= $this->end('tr');
  142. }
  143. $ret .= $this->start('tr');
  144. $ret .= $this->element('th', 'Allowed attributes');
  145. $ret .= $this->element('td', $this->listifyAttr($def->attr), array(), 0);
  146. $ret .= $this->end('tr');
  147. if (!empty($def->required_attr)) {
  148. $ret .= $this->row('Required attributes', $this->listify($def->required_attr));
  149. }
  150. $ret .= $this->renderChildren($def->child);
  151. }
  152. $ret .= $this->end('table');
  153. return $ret;
  154. }
  155. /**
  156. * Renders a row describing the allowed children of an element
  157. * @param HTMLPurifier_ChildDef $def HTMLPurifier_ChildDef of pertinent element
  158. * @return string
  159. */
  160. protected function renderChildren($def)
  161. {
  162. $context = new HTMLPurifier_Context();
  163. $ret = '';
  164. $ret .= $this->start('tr');
  165. $elements = array();
  166. $attr = array();
  167. if (isset($def->elements)) {
  168. if ($def->type == 'strictblockquote') {
  169. $def->validateChildren(array(), $this->config, $context);
  170. }
  171. $elements = $def->elements;
  172. }
  173. if ($def->type == 'chameleon') {
  174. $attr['rowspan'] = 2;
  175. } elseif ($def->type == 'empty') {
  176. $elements = array();
  177. } elseif ($def->type == 'table') {
  178. $elements = array_flip(
  179. array(
  180. 'col',
  181. 'caption',
  182. 'colgroup',
  183. 'thead',
  184. 'tfoot',
  185. 'tbody',
  186. 'tr'
  187. )
  188. );
  189. }
  190. $ret .= $this->element('th', 'Allowed children', $attr);
  191. if ($def->type == 'chameleon') {
  192. $ret .= $this->element(
  193. 'td',
  194. '<em>Block</em>: ' .
  195. $this->escape($this->listifyTagLookup($def->block->elements)),
  196. null,
  197. 0
  198. );
  199. $ret .= $this->end('tr');
  200. $ret .= $this->start('tr');
  201. $ret .= $this->element(
  202. 'td',
  203. '<em>Inline</em>: ' .
  204. $this->escape($this->listifyTagLookup($def->inline->elements)),
  205. null,
  206. 0
  207. );
  208. } elseif ($def->type == 'custom') {
  209. $ret .= $this->element(
  210. 'td',
  211. '<em>' . ucfirst($def->type) . '</em>: ' .
  212. $def->dtd_regex
  213. );
  214. } else {
  215. $ret .= $this->element(
  216. 'td',
  217. '<em>' . ucfirst($def->type) . '</em>: ' .
  218. $this->escape($this->listifyTagLookup($elements)),
  219. null,
  220. 0
  221. );
  222. }
  223. $ret .= $this->end('tr');
  224. return $ret;
  225. }
  226. /**
  227. * Listifies a tag lookup table.
  228. * @param array $array Tag lookup array in form of array('tagname' => true)
  229. * @return string
  230. */
  231. protected function listifyTagLookup($array)
  232. {
  233. ksort($array);
  234. $list = array();
  235. foreach ($array as $name => $discard) {
  236. if ($name !== '#PCDATA' && !isset($this->def->info[$name])) {
  237. continue;
  238. }
  239. $list[] = $name;
  240. }
  241. return $this->listify($list);
  242. }
  243. /**
  244. * Listifies a list of objects by retrieving class names and internal state
  245. * @param array $array List of objects
  246. * @return string
  247. * @todo Also add information about internal state
  248. */
  249. protected function listifyObjectList($array)
  250. {
  251. ksort($array);
  252. $list = array();
  253. foreach ($array as $obj) {
  254. $list[] = $this->getClass($obj, 'AttrTransform_');
  255. }
  256. return $this->listify($list);
  257. }
  258. /**
  259. * Listifies a hash of attributes to AttrDef classes
  260. * @param array $array Array hash in form of array('attrname' => HTMLPurifier_AttrDef)
  261. * @return string
  262. */
  263. protected function listifyAttr($array)
  264. {
  265. ksort($array);
  266. $list = array();
  267. foreach ($array as $name => $obj) {
  268. if ($obj === false) {
  269. continue;
  270. }
  271. $list[] = "$name&nbsp;=&nbsp;<i>" . $this->getClass($obj, 'AttrDef_') . '</i>';
  272. }
  273. return $this->listify($list);
  274. }
  275. /**
  276. * Creates a heavy header row
  277. * @param string $text
  278. * @param int $num
  279. * @return string
  280. */
  281. protected function heavyHeader($text, $num = 1)
  282. {
  283. $ret = '';
  284. $ret .= $this->start('tr');
  285. $ret .= $this->element('th', $text, array('colspan' => $num, 'class' => 'heavy'));
  286. $ret .= $this->end('tr');
  287. return $ret;
  288. }
  289. }
  290. // vim: et sw=4 sts=4