Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / Printer.php
blob7e20daafe35adfd21424cf16929f9d429a9a7d1b
1 <?php
3 require_once 'HTMLPurifier/Generator.php';
4 require_once 'HTMLPurifier/Token.php';
5 require_once 'HTMLPurifier/Encoder.php';
7 // OUT OF DATE, NEEDS UPDATING!
9 class HTMLPurifier_Printer
12 /**
13 * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
15 var $generator;
17 /**
18 * Instance of HTMLPurifier_Config, for easy access
20 var $config;
22 /**
23 * Initialize $generator.
25 function HTMLPurifier_Printer() {
26 $this->generator = new HTMLPurifier_Generator();
29 /**
30 * Give generator necessary configuration if possible
32 function prepareGenerator($config) {
33 // hack for smoketests/configForm.php
34 if (empty($config->conf['HTML'])) return;
35 $context = new HTMLPurifier_Context();
36 $this->generator->generateFromTokens(array(), $config, $context);
39 /**
40 * Main function that renders object or aspect of that object
41 * @note Parameters vary depending on printer
43 // function render() {}
45 /**
46 * Returns a start tag
47 * @param $tag Tag name
48 * @param $attr Attribute array
50 function start($tag, $attr = array()) {
51 return $this->generator->generateFromToken(
52 new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
56 /**
57 * Returns an end teg
58 * @param $tag Tag name
60 function end($tag) {
61 return $this->generator->generateFromToken(
62 new HTMLPurifier_Token_End($tag)
66 /**
67 * Prints a complete element with content inside
68 * @param $tag Tag name
69 * @param $contents Element contents
70 * @param $attr Tag attributes
71 * @param $escape Bool whether or not to escape contents
73 function element($tag, $contents, $attr = array(), $escape = true) {
74 return $this->start($tag, $attr) .
75 ($escape ? $this->escape($contents) : $contents) .
76 $this->end($tag);
79 function elementEmpty($tag, $attr = array()) {
80 return $this->generator->generateFromToken(
81 new HTMLPurifier_Token_Empty($tag, $attr)
85 function text($text) {
86 return $this->generator->generateFromToken(
87 new HTMLPurifier_Token_Text($text)
91 /**
92 * Prints a simple key/value row in a table.
93 * @param $name Key
94 * @param $value Value
96 function row($name, $value) {
97 if (is_bool($value)) $value = $value ? 'On' : 'Off';
98 return
99 $this->start('tr') . "\n" .
100 $this->element('th', $name) . "\n" .
101 $this->element('td', $value) . "\n" .
102 $this->end('tr')
107 * Escapes a string for HTML output.
108 * @param $string String to escape
110 function escape($string) {
111 $string = HTMLPurifier_Encoder::cleanUTF8($string);
112 $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
113 return $string;
117 * Takes a list of strings and turns them into a single list
118 * @param $array List of strings
119 * @param $polite Bool whether or not to add an end before the last
121 function listify($array, $polite = false) {
122 if (empty($array)) return 'None';
123 $ret = '';
124 $i = count($array);
125 foreach ($array as $value) {
126 $i--;
127 $ret .= $value;
128 if ($i > 0 && !($polite && $i == 1)) $ret .= ', ';
129 if ($polite && $i == 1) $ret .= 'and ';
131 return $ret;
135 * Retrieves the class of an object without prefixes, as well as metadata
136 * @param $obj Object to determine class of
137 * @param $prefix Further prefix to remove
139 function getClass($obj, $sec_prefix = '') {
140 static $five = null;
141 if ($five === null) $five = version_compare(PHP_VERSION, '5', '>=');
142 $prefix = 'HTMLPurifier_' . $sec_prefix;
143 if (!$five) $prefix = strtolower($prefix);
144 $class = str_replace($prefix, '', get_class($obj));
145 $lclass = strtolower($class);
146 $class .= '(';
147 switch ($lclass) {
148 case 'enum':
149 $values = array();
150 foreach ($obj->valid_values as $value => $bool) {
151 $values[] = $value;
153 $class .= implode(', ', $values);
154 break;
155 case 'composite':
156 $values = array();
157 foreach ($obj->defs as $def) {
158 $values[] = $this->getClass($def, $sec_prefix);
160 $class .= implode(', ', $values);
161 break;
162 case 'multiple':
163 $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
164 $class .= $obj->max;
165 break;
167 $class .= ')';
168 return $class;