Source for file ContentSets.php

Documentation is available at ContentSets.php

  1. <?php
  2.  
  3. /**
  4.  * @todo Unit test
  5.  */
  6. {
  7.     
  8.     /**
  9.      * List of content set strings (pipe seperators) indexed by name.
  10.      */
  11.     public $info = array();
  12.     
  13.     /**
  14.      * List of content set lookups (element => true) indexed by name.
  15.      * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
  16.      */
  17.     public $lookup = array();
  18.     
  19.     /**
  20.      * Synchronized list of defined content sets (keys of info)
  21.      */
  22.     protected $keys = array();
  23.     /**
  24.      * Synchronized list of defined content values (values of info)
  25.      */
  26.     protected $values = array();
  27.     
  28.     /**
  29.      * Merges in module's content sets, expands identifiers in the content
  30.      * sets and populates the keys, values and lookup member variables.
  31.      * @param $modules List of HTMLPurifier_HTMLModule
  32.      */
  33.     public function __construct($modules{
  34.         if (!is_array($modules)) $modules array($modules);
  35.         // populate content_sets based on module hints
  36.         // sorry, no way of overloading
  37.         foreach ($modules as $module_i => $module{
  38.             foreach ($module->content_sets as $key => $value{
  39.                 if (isset($this->info[$key])) {
  40.                     // add it into the existing content set
  41.                     $this->info[$key$this->info[$key' | ' $value;
  42.                 else {
  43.                     $this->info[$key$value;
  44.                 }
  45.             }
  46.         }
  47.         // perform content_set expansions
  48.         $this->keys = array_keys($this->info);
  49.         foreach ($this->info as $i => $set{
  50.             // only performed once, so infinite recursion is not
  51.             // a problem
  52.             $this->info[$i=
  53.                 str_replace(
  54.                     $this->keys,
  55.                     // must be recalculated each time due to
  56.                     // changing substitutions
  57.                     array_values($this->info),
  58.                 $set);
  59.         }
  60.         $this->values = array_values($this->info);
  61.         
  62.         // generate lookup tables
  63.         foreach ($this->info as $name => $set{
  64.             $this->lookup[$name$this->convertToLookup($set);
  65.         }
  66.     }
  67.     
  68.     /**
  69.      * Accepts a definition; generates and assigns a ChildDef for it
  70.      * @param $def HTMLPurifier_ElementDef reference
  71.      * @param $module Module that defined the ElementDef
  72.      */
  73.     public function generateChildDef(&$def$module{
  74.         if (!empty($def->child)) return// already done!
  75.         $content_model $def->content_model;
  76.         if (is_string($content_model)) {
  77.             $def->content_model str_replace(
  78.                 $this->keys$this->values$content_model);
  79.         }
  80.         $def->child $this->getChildDef($def$module);
  81.     }
  82.     
  83.     /**
  84.      * Instantiates a ChildDef based on content_model and content_model_type
  85.      * member variables in HTMLPurifier_ElementDef
  86.      * @note This will also defer to modules for custom HTMLPurifier_ChildDef
  87.      *        subclasses that need content set expansion
  88.      * @param $def HTMLPurifier_ElementDef to have ChildDef extracted
  89.      * @return HTMLPurifier_ChildDef corresponding to ElementDef
  90.      */
  91.     public function getChildDef($def$module{
  92.         $value $def->content_model;
  93.         if (is_object($value)) {
  94.             trigger_error(
  95.                 'Literal object child definitions should be stored in '.
  96.                 'ElementDef->child not ElementDef->content_model',
  97.                 E_USER_NOTICE
  98.             );
  99.             return $value;
  100.         }
  101.         switch ($def->content_model_type{
  102.             case 'required':
  103.                 return new HTMLPurifier_ChildDef_Required($value);
  104.             case 'optional':
  105.                 return new HTMLPurifier_ChildDef_Optional($value);
  106.             case 'empty':
  107.                 return new HTMLPurifier_ChildDef_Empty();
  108.             case 'custom':
  109.                 return new HTMLPurifier_ChildDef_Custom($value);
  110.         }
  111.         // defer to its module
  112.         $return false;
  113.         if ($module->defines_child_def// save a func call
  114.             $return $module->getChildDef($def);
  115.         }
  116.         if ($return !== falsereturn $return;
  117.         // error-out
  118.         trigger_error(
  119.             'Could not determine which ChildDef class to instantiate',
  120.             E_USER_ERROR
  121.         );
  122.         return false;
  123.     }
  124.     
  125.     /**
  126.      * Converts a string list of elements separated by pipes into
  127.      * a lookup array.
  128.      * @param $string List of elements
  129.      * @return Lookup array of elements
  130.      */
  131.     protected function convertToLookup($string{
  132.         $array explode('|'str_replace(' '''$string));
  133.         $ret array();
  134.         foreach ($array as $i => $k{
  135.             $ret[$ktrue;
  136.         }
  137.         return $ret;
  138.     }
  139.     
  140. }

Documentation generated on Thu, 19 Jun 2008 18:48:57 -0400 by phpDocumentor 1.4.2