3 // OUT OF DATE, NEEDS UPDATING!
6 class HTMLPurifier_Printer
10 * For HTML generation convenience funcs.
11 * @type HTMLPurifier_Generator
17 * @type HTMLPurifier_Config
22 * Initialize $generator.
24 public function __construct()
29 * Give generator necessary configuration if possible
30 * @param HTMLPurifier_Config $config
32 public function prepareGenerator($config)
34 $all = $config->getAll();
35 $context = new HTMLPurifier_Context();
36 $this->generator
= new HTMLPurifier_Generator($config, $context);
40 * Main function that renders object or aspect of that object
41 * @note Parameters vary depending on printer
43 // function render() {}
47 * @param string $tag Tag name
48 * @param array $attr Attribute array
51 protected function start($tag, $attr = array())
53 return $this->generator
->generateFromToken(
54 new HTMLPurifier_Token_Start($tag, $attr ?
$attr : array())
60 * @param string $tag Tag name
63 protected function end($tag)
65 return $this->generator
->generateFromToken(
66 new HTMLPurifier_Token_End($tag)
71 * Prints a complete element with content inside
72 * @param string $tag Tag name
73 * @param string $contents Element contents
74 * @param array $attr Tag attributes
75 * @param bool $escape whether or not to escape contents
78 protected function element($tag, $contents, $attr = array(), $escape = true)
80 return $this->start($tag, $attr) .
81 ($escape ?
$this->escape($contents) : $contents) .
90 protected function elementEmpty($tag, $attr = array())
92 return $this->generator
->generateFromToken(
93 new HTMLPurifier_Token_Empty($tag, $attr)
101 protected function text($text)
103 return $this->generator
->generateFromToken(
104 new HTMLPurifier_Token_Text($text)
109 * Prints a simple key/value row in a table.
110 * @param string $name Key
111 * @param mixed $value Value
114 protected function row($name, $value)
116 if (is_bool($value)) {
117 $value = $value ?
'On' : 'Off';
120 $this->start('tr') . "\n" .
121 $this->element('th', $name) . "\n" .
122 $this->element('td', $value) . "\n" .
127 * Escapes a string for HTML output.
128 * @param string $string String to escape
131 protected function escape($string)
133 $string = HTMLPurifier_Encoder
::cleanUTF8($string);
134 $string = htmlspecialchars($string, ENT_COMPAT
, 'UTF-8');
139 * Takes a list of strings and turns them into a single list
140 * @param string[] $array List of strings
141 * @param bool $polite Bool whether or not to add an end before the last
144 protected function listify($array, $polite = false)
151 foreach ($array as $value) {
154 if ($i > 0 && !($polite && $i == 1)) {
157 if ($polite && $i == 1) {
165 * Retrieves the class of an object without prefixes, as well as metadata
166 * @param object $obj Object to determine class of
167 * @param string $sec_prefix Further prefix to remove
170 protected function getClass($obj, $sec_prefix = '')
173 if ($five === null) {
174 $five = version_compare(PHP_VERSION
, '5', '>=');
176 $prefix = 'HTMLPurifier_' . $sec_prefix;
178 $prefix = strtolower($prefix);
180 $class = str_replace($prefix, '', get_class($obj));
181 $lclass = strtolower($class);
186 foreach ($obj->valid_values
as $value => $bool) {
189 $class .= implode(', ', $values);
191 case 'css_composite':
193 foreach ($obj->defs
as $def) {
194 $values[] = $this->getClass($def, $sec_prefix);
196 $class .= implode(', ', $values);
199 $class .= $this->getClass($obj->single
, $sec_prefix) . ', ';
202 case 'css_denyelementdecorator':
203 $class .= $this->getClass($obj->def
, $sec_prefix) . ', ';
204 $class .= $obj->element
;
206 case 'css_importantdecorator':
207 $class .= $this->getClass($obj->def
, $sec_prefix);
209 $class .= ', !important';
218 // vim: et sw=4 sts=4