[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/htmlpurifier/library/HTMLPurifier/ChildDef/ -> Required.php (source)

   1  <?php
   2  
   3  /**
   4   * Definition that allows a set of elements, but disallows empty children.
   5   */
   6  class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
   7  {
   8      /**
   9       * Lookup table of allowed elements.
  10       * @public
  11       */
  12      public $elements = array();
  13      /**
  14       * Whether or not the last passed node was all whitespace.
  15       */
  16      protected $whitespace = false;
  17      /**
  18       * @param $elements List of allowed element names (lowercase).
  19       */
  20      public function __construct($elements) {
  21          if (is_string($elements)) {
  22              $elements = str_replace(' ', '', $elements);
  23              $elements = explode('|', $elements);
  24          }
  25          $keys = array_keys($elements);
  26          if ($keys == array_keys($keys)) {
  27              $elements = array_flip($elements);
  28              foreach ($elements as $i => $x) {
  29                  $elements[$i] = true;
  30                  if (empty($i)) unset($elements[$i]); // remove blank
  31              }
  32          }
  33          $this->elements = $elements;
  34      }
  35      public $allow_empty = false;
  36      public $type = 'required';
  37      public function validateChildren($tokens_of_children, $config, $context) {
  38          // Flag for subclasses
  39          $this->whitespace = false;
  40  
  41          // if there are no tokens, delete parent node
  42          if (empty($tokens_of_children)) return false;
  43  
  44          // the new set of children
  45          $result = array();
  46  
  47          // current depth into the nest
  48          $nesting = 0;
  49  
  50          // whether or not we're deleting a node
  51          $is_deleting = false;
  52  
  53          // whether or not parsed character data is allowed
  54          // this controls whether or not we silently drop a tag
  55          // or generate escaped HTML from it
  56          $pcdata_allowed = isset($this->elements['#PCDATA']);
  57  
  58          // a little sanity check to make sure it's not ALL whitespace
  59          $all_whitespace = true;
  60  
  61          // some configuration
  62          $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
  63  
  64          // generator
  65          $gen = new HTMLPurifier_Generator($config, $context);
  66  
  67          foreach ($tokens_of_children as $token) {
  68              if (!empty($token->is_whitespace)) {
  69                  $result[] = $token;
  70                  continue;
  71              }
  72              $all_whitespace = false; // phew, we're not talking about whitespace
  73  
  74              $is_child = ($nesting == 0);
  75  
  76              if ($token instanceof HTMLPurifier_Token_Start) {
  77                  $nesting++;
  78              } elseif ($token instanceof HTMLPurifier_Token_End) {
  79                  $nesting--;
  80              }
  81  
  82              if ($is_child) {
  83                  $is_deleting = false;
  84                  if (!isset($this->elements[$token->name])) {
  85                      $is_deleting = true;
  86                      if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
  87                          $result[] = $token;
  88                      } elseif ($pcdata_allowed && $escape_invalid_children) {
  89                          $result[] = new HTMLPurifier_Token_Text(
  90                              $gen->generateFromToken($token)
  91                          );
  92                      }
  93                      continue;
  94                  }
  95              }
  96              if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
  97                  $result[] = $token;
  98              } elseif ($pcdata_allowed && $escape_invalid_children) {
  99                  $result[] =
 100                      new HTMLPurifier_Token_Text(
 101                          $gen->generateFromToken($token)
 102                      );
 103              } else {
 104                  // drop silently
 105              }
 106          }
 107          if (empty($result)) return false;
 108          if ($all_whitespace) {
 109              $this->whitespace = true;
 110              return false;
 111          }
 112          if ($tokens_of_children == $result) return true;
 113          return $result;
 114      }
 115  }
 116  
 117  // vim: et sw=4 sts=4


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1