[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/htmlpurifier/HTMLPurifier/ -> Printer.php (source)

   1  <?php
   2  
   3  require_once  'HTMLPurifier/Generator.php';
   4  require_once 'HTMLPurifier/Token.php';
   5  require_once  'HTMLPurifier/Encoder.php';
   6  
   7  // OUT OF DATE, NEEDS UPDATING!
   8  
   9  class HTMLPurifier_Printer
  10  {
  11      
  12      /**
  13       * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
  14       */
  15      var $generator;
  16      
  17      /**
  18       * Instance of HTMLPurifier_Config, for easy access
  19       */
  20      var $config;
  21      
  22      /**
  23       * Initialize $generator.
  24       */
  25      function HTMLPurifier_Printer() {
  26          $this->generator = new HTMLPurifier_Generator();
  27      }
  28      
  29      /**
  30       * Give generator necessary configuration if possible
  31       */
  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);
  37      }
  38      
  39      /**
  40       * Main function that renders object or aspect of that object
  41       * @note Parameters vary depending on printer
  42       */
  43      // function render() {}
  44      
  45      /**
  46       * Returns a start tag
  47       * @param $tag Tag name
  48       * @param $attr Attribute array
  49       */
  50      function start($tag, $attr = array()) {
  51          return $this->generator->generateFromToken(
  52                      new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
  53                 );
  54      }
  55      
  56      /**
  57       * Returns an end teg
  58       * @param $tag Tag name
  59       */
  60      function end($tag) {
  61          return $this->generator->generateFromToken(
  62                      new HTMLPurifier_Token_End($tag)
  63                 );
  64      }
  65      
  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
  72       */
  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);
  77      }
  78      
  79      function elementEmpty($tag, $attr = array()) {
  80          return $this->generator->generateFromToken(
  81              new HTMLPurifier_Token_Empty($tag, $attr)
  82          );
  83      }
  84      
  85      function text($text) {
  86          return $this->generator->generateFromToken(
  87              new HTMLPurifier_Token_Text($text)
  88          );
  89      }
  90      
  91      /**
  92       * Prints a simple key/value row in a table.
  93       * @param $name Key
  94       * @param $value Value
  95       */
  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')
 103          ;
 104      }
 105      
 106      /**
 107       * Escapes a string for HTML output.
 108       * @param $string String to escape
 109       */
 110      function escape($string) {
 111          $string = HTMLPurifier_Encoder::cleanUTF8($string);
 112          $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
 113          return $string;
 114      }
 115      
 116      /**
 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
 120       */
 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 ';
 130          }
 131          return $ret;
 132      }
 133      
 134      /**
 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
 138       */
 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;
 152                  }
 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);
 159                  }
 160                  $class .= implode(', ', $values);
 161                  break;
 162              case 'multiple':
 163                  $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
 164                  $class .= $obj->max;
 165                  break;
 166          }
 167          $class .= ')';
 168          return $class;
 169      }
 170      
 171  }
 172  


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7