1
0
Fork 0
friendica_2020-09-1_sharedH.../vendor/ezyang/htmlpurifier/library/HTMLPurifier/Printer.php

219 lines
5.8 KiB
PHP
Raw Normal View History

2010-09-09 05:14:17 +02:00
<?php
// OUT OF DATE, NEEDS UPDATING!
// USE XMLWRITER!
class HTMLPurifier_Printer
{
/**
2016-02-09 11:06:17 +01:00
* For HTML generation convenience funcs.
* @type HTMLPurifier_Generator
2010-09-09 05:14:17 +02:00
*/
protected $generator;
/**
2016-02-09 11:06:17 +01:00
* For easy access.
* @type HTMLPurifier_Config
2010-09-09 05:14:17 +02:00
*/
protected $config;
/**
* Initialize $generator.
*/
2016-02-09 11:06:17 +01:00
public function __construct()
{
2010-09-09 05:14:17 +02:00
}
/**
* Give generator necessary configuration if possible
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function prepareGenerator($config)
{
2010-09-09 05:14:17 +02:00
$all = $config->getAll();
$context = new HTMLPurifier_Context();
$this->generator = new HTMLPurifier_Generator($config, $context);
}
/**
* Main function that renders object or aspect of that object
* @note Parameters vary depending on printer
*/
// function render() {}
/**
* Returns a start tag
2016-02-09 11:06:17 +01:00
* @param string $tag Tag name
* @param array $attr Attribute array
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function start($tag, $attr = array())
{
2010-09-09 05:14:17 +02:00
return $this->generator->generateFromToken(
2016-02-09 11:06:17 +01:00
new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
);
2010-09-09 05:14:17 +02:00
}
/**
2016-02-09 11:06:17 +01:00
* Returns an end tag
* @param string $tag Tag name
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function end($tag)
{
2010-09-09 05:14:17 +02:00
return $this->generator->generateFromToken(
2016-02-09 11:06:17 +01:00
new HTMLPurifier_Token_End($tag)
);
2010-09-09 05:14:17 +02:00
}
/**
* Prints a complete element with content inside
2016-02-09 11:06:17 +01:00
* @param string $tag Tag name
* @param string $contents Element contents
* @param array $attr Tag attributes
* @param bool $escape whether or not to escape contents
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function element($tag, $contents, $attr = array(), $escape = true)
{
2010-09-09 05:14:17 +02:00
return $this->start($tag, $attr) .
2016-02-09 11:06:17 +01:00
($escape ? $this->escape($contents) : $contents) .
$this->end($tag);
2010-09-09 05:14:17 +02:00
}
2016-02-09 11:06:17 +01:00
/**
* @param string $tag
* @param array $attr
* @return string
*/
protected function elementEmpty($tag, $attr = array())
{
2010-09-09 05:14:17 +02:00
return $this->generator->generateFromToken(
new HTMLPurifier_Token_Empty($tag, $attr)
);
}
2016-02-09 11:06:17 +01:00
/**
* @param string $text
* @return string
*/
protected function text($text)
{
2010-09-09 05:14:17 +02:00
return $this->generator->generateFromToken(
new HTMLPurifier_Token_Text($text)
);
}
/**
* Prints a simple key/value row in a table.
2016-02-09 11:06:17 +01:00
* @param string $name Key
* @param mixed $value Value
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function row($name, $value)
{
if (is_bool($value)) {
$value = $value ? 'On' : 'Off';
}
2010-09-09 05:14:17 +02:00
return
$this->start('tr') . "\n" .
2016-02-09 11:06:17 +01:00
$this->element('th', $name) . "\n" .
$this->element('td', $value) . "\n" .
$this->end('tr');
2010-09-09 05:14:17 +02:00
}
/**
* Escapes a string for HTML output.
2016-02-09 11:06:17 +01:00
* @param string $string String to escape
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function escape($string)
{
2010-09-09 05:14:17 +02:00
$string = HTMLPurifier_Encoder::cleanUTF8($string);
$string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
return $string;
}
/**
* Takes a list of strings and turns them into a single list
2016-02-09 11:06:17 +01:00
* @param string[] $array List of strings
* @param bool $polite Bool whether or not to add an end before the last
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function listify($array, $polite = false)
{
if (empty($array)) {
return 'None';
}
2010-09-09 05:14:17 +02:00
$ret = '';
$i = count($array);
foreach ($array as $value) {
$i--;
$ret .= $value;
2016-02-09 11:06:17 +01:00
if ($i > 0 && !($polite && $i == 1)) {
$ret .= ', ';
}
if ($polite && $i == 1) {
$ret .= 'and ';
}
2010-09-09 05:14:17 +02:00
}
return $ret;
}
/**
* Retrieves the class of an object without prefixes, as well as metadata
2016-02-09 11:06:17 +01:00
* @param object $obj Object to determine class of
* @param string $sec_prefix Further prefix to remove
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function getClass($obj, $sec_prefix = '')
{
2010-09-09 05:14:17 +02:00
static $five = null;
2016-02-09 11:06:17 +01:00
if ($five === null) {
$five = version_compare(PHP_VERSION, '5', '>=');
}
2010-09-09 05:14:17 +02:00
$prefix = 'HTMLPurifier_' . $sec_prefix;
2016-02-09 11:06:17 +01:00
if (!$five) {
$prefix = strtolower($prefix);
}
2010-09-09 05:14:17 +02:00
$class = str_replace($prefix, '', get_class($obj));
$lclass = strtolower($class);
$class .= '(';
switch ($lclass) {
case 'enum':
$values = array();
foreach ($obj->valid_values as $value => $bool) {
$values[] = $value;
}
$class .= implode(', ', $values);
break;
case 'css_composite':
$values = array();
foreach ($obj->defs as $def) {
$values[] = $this->getClass($def, $sec_prefix);
}
$class .= implode(', ', $values);
break;
case 'css_multiple':
$class .= $this->getClass($obj->single, $sec_prefix) . ', ';
$class .= $obj->max;
break;
case 'css_denyelementdecorator':
$class .= $this->getClass($obj->def, $sec_prefix) . ', ';
$class .= $obj->element;
break;
case 'css_importantdecorator':
$class .= $this->getClass($obj->def, $sec_prefix);
2016-02-09 11:06:17 +01:00
if ($obj->allow) {
$class .= ', !important';
}
2010-09-09 05:14:17 +02:00
break;
}
$class .= ')';
return $class;
}
}
// vim: et sw=4 sts=4