Fix two minor bugs, updating Phorum and removing unused $dir variable.
[htmlpurifier/darkodev.git] / library / HTMLPurifier / Printer.php
blobe7eb82e83e480d9d55bacf38a2a12e1cbfd8b23e
1 <?php
3 // OUT OF DATE, NEEDS UPDATING!
4 // USE XMLWRITER!
6 class HTMLPurifier_Printer
9 /**
10 * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
12 protected $generator;
14 /**
15 * Instance of HTMLPurifier_Config, for easy access
17 protected $config;
19 /**
20 * Initialize $generator.
22 public function __construct() {
25 /**
26 * Give generator necessary configuration if possible
28 public function prepareGenerator($config) {
29 $all = $config->getAll();
30 $context = new HTMLPurifier_Context();
31 $this->generator = new HTMLPurifier_Generator($config, $context);
34 /**
35 * Main function that renders object or aspect of that object
36 * @note Parameters vary depending on printer
38 // function render() {}
40 /**
41 * Returns a start tag
42 * @param $tag Tag name
43 * @param $attr Attribute array
45 protected function start($tag, $attr = array()) {
46 return $this->generator->generateFromToken(
47 new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
51 /**
52 * Returns an end teg
53 * @param $tag Tag name
55 protected function end($tag) {
56 return $this->generator->generateFromToken(
57 new HTMLPurifier_Token_End($tag)
61 /**
62 * Prints a complete element with content inside
63 * @param $tag Tag name
64 * @param $contents Element contents
65 * @param $attr Tag attributes
66 * @param $escape Bool whether or not to escape contents
68 protected function element($tag, $contents, $attr = array(), $escape = true) {
69 return $this->start($tag, $attr) .
70 ($escape ? $this->escape($contents) : $contents) .
71 $this->end($tag);
74 protected function elementEmpty($tag, $attr = array()) {
75 return $this->generator->generateFromToken(
76 new HTMLPurifier_Token_Empty($tag, $attr)
80 protected function text($text) {
81 return $this->generator->generateFromToken(
82 new HTMLPurifier_Token_Text($text)
86 /**
87 * Prints a simple key/value row in a table.
88 * @param $name Key
89 * @param $value Value
91 protected function row($name, $value) {
92 if (is_bool($value)) $value = $value ? 'On' : 'Off';
93 return
94 $this->start('tr') . "\n" .
95 $this->element('th', $name) . "\n" .
96 $this->element('td', $value) . "\n" .
97 $this->end('tr')
102 * Escapes a string for HTML output.
103 * @param $string String to escape
105 protected function escape($string) {
106 $string = HTMLPurifier_Encoder::cleanUTF8($string);
107 $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
108 return $string;
112 * Takes a list of strings and turns them into a single list
113 * @param $array List of strings
114 * @param $polite Bool whether or not to add an end before the last
116 protected function listify($array, $polite = false) {
117 if (empty($array)) return 'None';
118 $ret = '';
119 $i = count($array);
120 foreach ($array as $value) {
121 $i--;
122 $ret .= $value;
123 if ($i > 0 && !($polite && $i == 1)) $ret .= ', ';
124 if ($polite && $i == 1) $ret .= 'and ';
126 return $ret;
130 * Retrieves the class of an object without prefixes, as well as metadata
131 * @param $obj Object to determine class of
132 * @param $prefix Further prefix to remove
134 protected function getClass($obj, $sec_prefix = '') {
135 static $five = null;
136 if ($five === null) $five = version_compare(PHP_VERSION, '5', '>=');
137 $prefix = 'HTMLPurifier_' . $sec_prefix;
138 if (!$five) $prefix = strtolower($prefix);
139 $class = str_replace($prefix, '', get_class($obj));
140 $lclass = strtolower($class);
141 $class .= '(';
142 switch ($lclass) {
143 case 'enum':
144 $values = array();
145 foreach ($obj->valid_values as $value => $bool) {
146 $values[] = $value;
148 $class .= implode(', ', $values);
149 break;
150 case 'css_composite':
151 $values = array();
152 foreach ($obj->defs as $def) {
153 $values[] = $this->getClass($def, $sec_prefix);
155 $class .= implode(', ', $values);
156 break;
157 case 'css_multiple':
158 $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
159 $class .= $obj->max;
160 break;
161 case 'css_denyelementdecorator':
162 $class .= $this->getClass($obj->def, $sec_prefix) . ', ';
163 $class .= $obj->element;
164 break;
165 case 'css_importantdecorator':
166 $class .= $this->getClass($obj->def, $sec_prefix);
167 if ($obj->allow) $class .= ', !important';
168 break;
170 $class .= ')';
171 return $class;
176 // vim: et sw=4 sts=4