Source for file Required.php

Documentation is available at Required.php

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

Documentation generated on Thu, 19 Jun 2008 18:50:16 -0400 by phpDocumentor 1.4.2