3 require_once 'HTMLPurifier/Generator.php';
4 require_once 'HTMLPurifier/Token.php';
5 require_once 'HTMLPurifier/Encoder.php';
7 class HTMLPurifier_Printer
11 * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
16 * Instance of HTMLPurifier_Config, for easy access
21 * Initialize $generator.
23 function HTMLPurifier_Printer() {
24 $this->generator
= new HTMLPurifier_Generator();
28 * Main function that renders object or aspect of that object
29 * @param $config Configuration object
31 function render($config) {}
35 * @param $tag Tag name
36 * @param $attr Attribute array
38 function start($tag, $attr = array()) {
39 return $this->generator
->generateFromToken(
40 new HTMLPurifier_Token_Start($tag, $attr ?
$attr : array())
46 * @param $tag Tag name
49 return $this->generator
->generateFromToken(
50 new HTMLPurifier_Token_End($tag)
55 * Prints a complete element with content inside
56 * @param $tag Tag name
57 * @param $contents Element contents
58 * @param $attr Tag attributes
59 * @param $escape Bool whether or not to escape contents
61 function element($tag, $contents, $attr = array(), $escape = true) {
62 return $this->start($tag, $attr) .
63 ($escape ?
$this->escape($contents) : $contents) .
68 * Prints a simple key/value row in a table.
72 function row($name, $value) {
73 if (is_bool($value)) $value = $value ?
'On' : 'Off';
75 $this->start('tr') . "\n" .
76 $this->element('th', $name) . "\n" .
77 $this->element('td', $value) . "\n" .
83 * Escapes a string for HTML output.
84 * @param $string String to escape
86 function escape($string) {
87 $string = HTMLPurifier_Encoder
::cleanUTF8($string);
88 $string = htmlspecialchars($string, ENT_COMPAT
, 'UTF-8');
93 * Takes a list of strings and turns them into a single list
94 * @param $array List of strings
95 * @param $polite Bool whether or not to add an end before the last
97 function listify($array, $polite = false) {
98 if (empty($array)) return 'None';
101 foreach ($array as $value) {
104 if ($i > 0 && !($polite && $i == 1)) $ret .= ', ';
105 if ($polite && $i == 1) $ret .= 'and ';
111 * Retrieves the class of an object without prefixes, as well as metadata
112 * @param $obj Object to determine class of
113 * @param $prefix Further prefix to remove
115 function getClass($obj, $sec_prefix = '') {
117 if ($five === null) $five = version_compare(PHP_VERSION
, '5', '>=');
118 $prefix = 'HTMLPurifier_' . $sec_prefix;
119 if (!$five) $prefix = strtolower($prefix);
120 $class = str_replace($prefix, '', get_class($obj));
121 $lclass = strtolower($class);
126 foreach ($obj->valid_values
as $value => $bool) {
129 $class .= implode(', ', $values);
133 foreach ($obj->defs
as $def) {
134 $values[] = $this->getClass($def, $sec_prefix);
136 $class .= implode(', ', $values);
139 $class .= $this->getClass($obj->single
, $sec_prefix) . ', ';