Cheetah
Printer.php
Go to the documentation of this file.
1 <?php
2 
3 // OUT OF DATE, NEEDS UPDATING!
4 // USE XMLWRITER!
5 
7 {
8 
13  protected $generator;
14 
19  protected $config;
20 
24  public function __construct()
25  {
26  }
27 
32  public function prepareGenerator($config)
33  {
34  $all = $config->getAll();
35  $context = new HTMLPurifier_Context();
36  $this->generator = new HTMLPurifier_Generator($config, $context);
37  }
38 
43  // function render() {}
44 
51  protected function start($tag, $attr = array())
52  {
53  return $this->generator->generateFromToken(
54  new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
55  );
56  }
57 
63  protected function end($tag)
64  {
65  return $this->generator->generateFromToken(
66  new HTMLPurifier_Token_End($tag)
67  );
68  }
69 
78  protected function element($tag, $contents, $attr = array(), $escape = true)
79  {
80  return $this->start($tag, $attr) .
81  ($escape ? $this->escape($contents) : $contents) .
82  $this->end($tag);
83  }
84 
90  protected function elementEmpty($tag, $attr = array())
91  {
92  return $this->generator->generateFromToken(
93  new HTMLPurifier_Token_Empty($tag, $attr)
94  );
95  }
96 
101  protected function text($text)
102  {
103  return $this->generator->generateFromToken(
104  new HTMLPurifier_Token_Text($text)
105  );
106  }
107 
114  protected function row($name, $value)
115  {
116  if (is_bool($value)) {
117  $value = $value ? 'On' : 'Off';
118  }
119  return
120  $this->start('tr') . "\n" .
121  $this->element('th', $name) . "\n" .
122  $this->element('td', $value) . "\n" .
123  $this->end('tr');
124  }
125 
131  protected function escape($string)
132  {
133  $string = HTMLPurifier_Encoder::cleanUTF8($string);
134  $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
135  return $string;
136  }
137 
144  protected function listify($array, $polite = false)
145  {
146  if (empty($array)) {
147  return 'None';
148  }
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 .= ', ';
156  }
157  if ($polite && $i == 1) {
158  $ret .= 'and ';
159  }
160  }
161  return $ret;
162  }
163 
170  protected function getClass($obj, $sec_prefix = '')
171  {
172  static $five = null;
173  if ($five === null) {
174  $five = version_compare(PHP_VERSION, '5', '>=');
175  }
176  $prefix = 'HTMLPurifier_' . $sec_prefix;
177  if (!$five) {
178  $prefix = strtolower($prefix);
179  }
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;
188  }
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);
195  }
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';
210  }
211  break;
212  }
213  $class .= ')';
214  return $class;
215  }
216 }
217 
218 // vim: et sw=4 sts=4
HTMLPurifier_Printer\escape
escape($string)
Definition: Printer.php:131
HTMLPurifier_Encoder\cleanUTF8
static cleanUTF8($str, $force_php=false)
Definition: HTMLPurifier.standalone.php:3959
HTMLPurifier_Printer\listify
listify($array, $polite=false)
Definition: Printer.php:144
HTMLPurifier_Printer\__construct
__construct()
Definition: Printer.php:24
HTMLPurifier_Printer
Definition: Printer.php:7
HTMLPurifier_Token_Start
Definition: HTMLPurifier.standalone.php:21407
HTMLPurifier_Printer\end
end($tag)
Definition: Printer.php:63
$ret
$ret
Definition: index.php:39
php
$def
$def
Definition: HTML.DefinitionID.txt:17
HTMLPurifier_Context
Definition: HTMLPurifier.standalone.php:3069
HTMLPurifier_Printer\prepareGenerator
prepareGenerator($config)
Definition: Printer.php:32
HTMLPurifier_Generator
Definition: HTMLPurifier.standalone.php:5179
HTMLPurifier_Printer\elementEmpty
elementEmpty($tag, $attr=array())
Definition: Printer.php:90
HTMLPurifier_Printer\start
start($tag, $attr=array())
Definition: Printer.php:51
HTMLPurifier_Token_End
Definition: HTMLPurifier.standalone.php:21386
HTMLPurifier_Token_Text
Definition: HTMLPurifier.standalone.php:21424
HTMLPurifier_Printer\element
element($tag, $contents, $attr=array(), $escape=true)
Definition: Printer.php:78
HTMLPurifier_Printer\getClass
getClass($obj, $sec_prefix='')
Definition: Printer.php:170
HTMLPurifier_Printer\$generator
$generator
Definition: Printer.php:13
HTMLPurifier_Printer\text
text($text)
Definition: Printer.php:101
HTMLPurifier_Printer\row
row($name, $value)
Definition: Printer.php:114
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
HTMLPurifier_Token_Empty
Definition: HTMLPurifier.standalone.php:21366
HTMLPurifier_Printer\$config
$config
Definition: Printer.php:19