3 // OUT OF DATE, NEEDS UPDATING!
6 class HTMLPurifier_Printer
10 * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
15 * Instance of HTMLPurifier_Config, for easy access
20 * Initialize $generator.
22 public function __construct() {
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);
35 * Main function that renders object or aspect of that object
36 * @note Parameters vary depending on printer
38 // function render() {}
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())
53 * @param $tag Tag name
55 protected function end($tag) {
56 return $this->generator->generateFromToken(
57 new HTMLPurifier_Token_End($tag)
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) .
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)
87 * Prints a simple key/value row in a table.
91 protected function row($name, $value) {
92 if (is_bool($value)) $value = $value ? 'On' : 'Off';
94 $this->start('tr') . "\n" .
95 $this->element('th', $name) . "\n" .
96 $this->element('td', $value) . "\n" .
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');
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';
120 foreach ($array as $value) {
123 if ($i > 0 && !($polite && $i == 1)) $ret .= ', ';
124 if ($polite && $i == 1) $ret .= 'and ';
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 = '') {
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);
145 foreach ($obj->valid_values as $value => $bool) {
148 $class .= implode(', ', $values);
150 case 'css_composite':
152 foreach ($obj->defs as $def) {
153 $values[] = $this->getClass($def, $sec_prefix);
155 $class .= implode(', ', $values);
158 $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
161 case 'css_denyelementdecorator':
162 $class .= $this->getClass($obj->def, $sec_prefix) . ', ';
163 $class .= $obj->element;
165 case 'css_importantdecorator':
166 $class .= $this->getClass($obj->def, $sec_prefix);
167 if ($obj->allow) $class .= ', !important';
176 // vim: et sw=4 sts=4