MDL-75708 reportbuilder: consider stress tests as requiring longtest.
[moodle.git] / lib / htmlpurifier / HTMLPurifier / Printer.php
blob16acd4157400c42c5aeeb3feb768afbdbc355edc
1 <?php
3 // OUT OF DATE, NEEDS UPDATING!
4 // USE XMLWRITER!
6 class HTMLPurifier_Printer
9 /**
10 * For HTML generation convenience funcs.
11 * @type HTMLPurifier_Generator
13 protected $generator;
15 /**
16 * For easy access.
17 * @type HTMLPurifier_Config
19 protected $config;
21 /**
22 * Initialize $generator.
24 public function __construct()
28 /**
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);
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 string $tag Tag name
48 * @param array $attr Attribute array
49 * @return string
51 protected function start($tag, $attr = array())
53 return $this->generator->generateFromToken(
54 new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
58 /**
59 * Returns an end tag
60 * @param string $tag Tag name
61 * @return string
63 protected function end($tag)
65 return $this->generator->generateFromToken(
66 new HTMLPurifier_Token_End($tag)
70 /**
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
76 * @return string
78 protected function element($tag, $contents, $attr = array(), $escape = true)
80 return $this->start($tag, $attr) .
81 ($escape ? $this->escape($contents) : $contents) .
82 $this->end($tag);
85 /**
86 * @param string $tag
87 * @param array $attr
88 * @return string
90 protected function elementEmpty($tag, $attr = array())
92 return $this->generator->generateFromToken(
93 new HTMLPurifier_Token_Empty($tag, $attr)
97 /**
98 * @param string $text
99 * @return string
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
112 * @return string
114 protected function row($name, $value)
116 if (is_bool($value)) {
117 $value = $value ? 'On' : 'Off';
119 return
120 $this->start('tr') . "\n" .
121 $this->element('th', $name) . "\n" .
122 $this->element('td', $value) . "\n" .
123 $this->end('tr');
127 * Escapes a string for HTML output.
128 * @param string $string String to escape
129 * @return string
131 protected function escape($string)
133 $string = HTMLPurifier_Encoder::cleanUTF8($string);
134 $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
135 return $string;
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
142 * @return string
144 protected function listify($array, $polite = false)
146 if (empty($array)) {
147 return 'None';
149 $ret = '';
150 $i = count($array);
151 foreach ($array as $value) {
152 $i--;
153 $ret .= $value;
154 if ($i > 0 && !($polite && $i == 1)) {
155 $ret .= ', ';
157 if ($polite && $i == 1) {
158 $ret .= 'and ';
161 return $ret;
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
168 * @return string
170 protected function getClass($obj, $sec_prefix = '')
172 static $five = null;
173 if ($five === null) {
174 $five = version_compare(PHP_VERSION, '5', '>=');
176 $prefix = 'HTMLPurifier_' . $sec_prefix;
177 if (!$five) {
178 $prefix = strtolower($prefix);
180 $class = str_replace($prefix, '', get_class($obj));
181 $lclass = strtolower($class);
182 $class .= '(';
183 switch ($lclass) {
184 case 'enum':
185 $values = array();
186 foreach ($obj->valid_values as $value => $bool) {
187 $values[] = $value;
189 $class .= implode(', ', $values);
190 break;
191 case 'css_composite':
192 $values = array();
193 foreach ($obj->defs as $def) {
194 $values[] = $this->getClass($def, $sec_prefix);
196 $class .= implode(', ', $values);
197 break;
198 case 'css_multiple':
199 $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
200 $class .= $obj->max;
201 break;
202 case 'css_denyelementdecorator':
203 $class .= $this->getClass($obj->def, $sec_prefix) . ', ';
204 $class .= $obj->element;
205 break;
206 case 'css_importantdecorator':
207 $class .= $this->getClass($obj->def, $sec_prefix);
208 if ($obj->allow) {
209 $class .= ', !important';
211 break;
213 $class .= ')';
214 return $class;
218 // vim: et sw=4 sts=4