friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/Printer/HTMLDefinition.php

325 lines
10 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
class HTMLPurifier_Printer_HTMLDefinition extends HTMLPurifier_Printer
{
/**
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_HTMLDefinition, for easy access
2010-09-09 03:14:17 +00:00
*/
protected $def;
2016-02-09 10:06:17 +00:00
/**
* @param HTMLPurifier_Config $config
* @return string
*/
public function render($config)
{
2010-09-09 03:14:17 +00:00
$ret = '';
$this->config =& $config;
$this->def = $config->getHTMLDefinition();
$ret .= $this->start('div', array('class' => 'HTMLPurifier_Printer'));
$ret .= $this->renderDoctype();
$ret .= $this->renderEnvironment();
$ret .= $this->renderContentSets();
$ret .= $this->renderInfo();
$ret .= $this->end('div');
return $ret;
}
/**
* Renders the Doctype table
2016-02-09 10:06:17 +00:00
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function renderDoctype()
{
2010-09-09 03:14:17 +00:00
$doctype = $this->def->doctype;
$ret = '';
$ret .= $this->start('table');
$ret .= $this->element('caption', 'Doctype');
$ret .= $this->row('Name', $doctype->name);
$ret .= $this->row('XML', $doctype->xml ? 'Yes' : 'No');
$ret .= $this->row('Default Modules', implode($doctype->modules, ', '));
$ret .= $this->row('Default Tidy Modules', implode($doctype->tidyModules, ', '));
$ret .= $this->end('table');
return $ret;
}
/**
* Renders environment table, which is miscellaneous info
2016-02-09 10:06:17 +00:00
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function renderEnvironment()
{
2010-09-09 03:14:17 +00:00
$def = $this->def;
$ret = '';
$ret .= $this->start('table');
$ret .= $this->element('caption', 'Environment');
$ret .= $this->row('Parent of fragment', $def->info_parent);
$ret .= $this->renderChildren($def->info_parent_def->child);
$ret .= $this->row('Block wrap name', $def->info_block_wrapper);
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Global attributes');
$ret .= $this->element('td', $this->listifyAttr($def->info_global_attr), null, 0);
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Tag transforms');
$list = array();
foreach ($def->info_tag_transform as $old => $new) {
$new = $this->getClass($new, 'TagTransform_');
$list[] = "<$old> with $new";
}
$ret .= $this->element('td', $this->listify($list));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Pre-AttrTransform');
$ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_pre));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Post-AttrTransform');
$ret .= $this->element('td', $this->listifyObjectList($def->info_attr_transform_post));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
$ret .= $this->end('table');
return $ret;
}
/**
* Renders the Content Sets table
2016-02-09 10:06:17 +00:00
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function renderContentSets()
{
2010-09-09 03:14:17 +00:00
$ret = '';
$ret .= $this->start('table');
$ret .= $this->element('caption', 'Content Sets');
foreach ($this->def->info_content_sets as $name => $lookup) {
$ret .= $this->heavyHeader($name);
$ret .= $this->start('tr');
$ret .= $this->element('td', $this->listifyTagLookup($lookup));
$ret .= $this->end('tr');
}
$ret .= $this->end('table');
return $ret;
}
/**
* Renders the Elements ($info) table
2016-02-09 10:06:17 +00:00
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function renderInfo()
{
2010-09-09 03:14:17 +00:00
$ret = '';
$ret .= $this->start('table');
$ret .= $this->element('caption', 'Elements ($info)');
ksort($this->def->info);
$ret .= $this->heavyHeader('Allowed tags', 2);
$ret .= $this->start('tr');
$ret .= $this->element('td', $this->listifyTagLookup($this->def->info), array('colspan' => 2));
$ret .= $this->end('tr');
foreach ($this->def->info as $name => $def) {
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', "<$name>", array('class' => 'heavy', 'colspan' => 2));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Inline content');
$ret .= $this->element('td', $def->descendants_are_inline ? 'Yes' : 'No');
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
if (!empty($def->excludes)) {
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Excludes');
$ret .= $this->element('td', $this->listifyTagLookup($def->excludes));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
}
if (!empty($def->attr_transform_pre)) {
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Pre-AttrTransform');
$ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_pre));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
}
if (!empty($def->attr_transform_post)) {
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Post-AttrTransform');
$ret .= $this->element('td', $this->listifyObjectList($def->attr_transform_post));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
}
if (!empty($def->auto_close)) {
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Auto closed by');
$ret .= $this->element('td', $this->listifyTagLookup($def->auto_close));
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
}
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$ret .= $this->element('th', 'Allowed attributes');
$ret .= $this->element('td', $this->listifyAttr($def->attr), array(), 0);
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
if (!empty($def->required_attr)) {
$ret .= $this->row('Required attributes', $this->listify($def->required_attr));
}
$ret .= $this->renderChildren($def->child);
}
$ret .= $this->end('table');
return $ret;
}
/**
* Renders a row describing the allowed children of an element
2016-02-09 10:06:17 +00:00
* @param HTMLPurifier_ChildDef $def HTMLPurifier_ChildDef of pertinent element
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function renderChildren($def)
{
2010-09-09 03:14:17 +00:00
$context = new HTMLPurifier_Context();
$ret = '';
$ret .= $this->start('tr');
2016-02-09 10:06:17 +00:00
$elements = array();
$attr = array();
if (isset($def->elements)) {
if ($def->type == 'strictblockquote') {
$def->validateChildren(array(), $this->config, $context);
2010-09-09 03:14:17 +00:00
}
2016-02-09 10:06:17 +00:00
$elements = $def->elements;
}
if ($def->type == 'chameleon') {
$attr['rowspan'] = 2;
} elseif ($def->type == 'empty') {
$elements = array();
} elseif ($def->type == 'table') {
$elements = array_flip(
array(
'col',
'caption',
'colgroup',
'thead',
'tfoot',
'tbody',
'tr'
)
);
}
$ret .= $this->element('th', 'Allowed children', $attr);
2010-09-09 03:14:17 +00:00
2016-02-09 10:06:17 +00:00
if ($def->type == 'chameleon') {
2010-09-09 03:14:17 +00:00
2016-02-09 10:06:17 +00:00
$ret .= $this->element(
'td',
'<em>Block</em>: ' .
$this->escape($this->listifyTagLookup($def->block->elements)),
null,
0
);
$ret .= $this->end('tr');
$ret .= $this->start('tr');
$ret .= $this->element(
'td',
'<em>Inline</em>: ' .
$this->escape($this->listifyTagLookup($def->inline->elements)),
null,
0
);
} elseif ($def->type == 'custom') {
$ret .= $this->element(
'td',
'<em>' . ucfirst($def->type) . '</em>: ' .
$def->dtd_regex
);
} else {
$ret .= $this->element(
'td',
'<em>' . ucfirst($def->type) . '</em>: ' .
$this->escape($this->listifyTagLookup($elements)),
null,
0
);
}
2010-09-09 03:14:17 +00:00
$ret .= $this->end('tr');
return $ret;
}
/**
* Listifies a tag lookup table.
2016-02-09 10:06:17 +00:00
* @param array $array Tag lookup array in form of array('tagname' => true)
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function listifyTagLookup($array)
{
2010-09-09 03:14:17 +00:00
ksort($array);
$list = array();
foreach ($array as $name => $discard) {
2016-02-09 10:06:17 +00:00
if ($name !== '#PCDATA' && !isset($this->def->info[$name])) {
continue;
}
2010-09-09 03:14:17 +00:00
$list[] = $name;
}
return $this->listify($list);
}
/**
* Listifies a list of objects by retrieving class names and internal state
2016-02-09 10:06:17 +00:00
* @param array $array List of objects
* @return string
2010-09-09 03:14:17 +00:00
* @todo Also add information about internal state
*/
2016-02-09 10:06:17 +00:00
protected function listifyObjectList($array)
{
2010-09-09 03:14:17 +00:00
ksort($array);
$list = array();
2016-02-09 10:06:17 +00:00
foreach ($array as $obj) {
2010-09-09 03:14:17 +00:00
$list[] = $this->getClass($obj, 'AttrTransform_');
}
return $this->listify($list);
}
/**
* Listifies a hash of attributes to AttrDef classes
2016-02-09 10:06:17 +00:00
* @param array $array Array hash in form of array('attrname' => HTMLPurifier_AttrDef)
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function listifyAttr($array)
{
2010-09-09 03:14:17 +00:00
ksort($array);
$list = array();
foreach ($array as $name => $obj) {
2016-02-09 10:06:17 +00:00
if ($obj === false) {
continue;
}
2010-09-09 03:14:17 +00:00
$list[] = "$name&nbsp;=&nbsp;<i>" . $this->getClass($obj, 'AttrDef_') . '</i>';
}
return $this->listify($list);
}
/**
* Creates a heavy header row
2016-02-09 10:06:17 +00:00
* @param string $text
* @param int $num
* @return string
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
protected function heavyHeader($text, $num = 1)
{
2010-09-09 03:14:17 +00:00
$ret = '';
$ret .= $this->start('tr');
$ret .= $this->element('th', $text, array('colspan' => $num, 'class' => 'heavy'));
$ret .= $this->end('tr');
return $ret;
}
}
// vim: et sw=4 sts=4