HTMLPurifier 4.4.0
|
00001 <?php 00002 00006 class HTMLPurifier_ContentSets 00007 { 00008 00012 public $info = array(); 00013 00018 public $lookup = array(); 00019 00023 protected $keys = array(); 00027 protected $values = array(); 00028 00034 public function __construct($modules) { 00035 if (!is_array($modules)) $modules = array($modules); 00036 // populate content_sets based on module hints 00037 // sorry, no way of overloading 00038 foreach ($modules as $module_i => $module) { 00039 foreach ($module->content_sets as $key => $value) { 00040 $temp = $this->convertToLookup($value); 00041 if (isset($this->lookup[$key])) { 00042 // add it into the existing content set 00043 $this->lookup[$key] = array_merge($this->lookup[$key], $temp); 00044 } else { 00045 $this->lookup[$key] = $temp; 00046 } 00047 } 00048 } 00049 $old_lookup = false; 00050 while ($old_lookup !== $this->lookup) { 00051 $old_lookup = $this->lookup; 00052 foreach ($this->lookup as $i => $set) { 00053 $add = array(); 00054 foreach ($set as $element => $x) { 00055 if (isset($this->lookup[$element])) { 00056 $add += $this->lookup[$element]; 00057 unset($this->lookup[$i][$element]); 00058 } 00059 } 00060 $this->lookup[$i] += $add; 00061 } 00062 } 00063 00064 foreach ($this->lookup as $key => $lookup) { 00065 $this->info[$key] = implode(' | ', array_keys($lookup)); 00066 } 00067 $this->keys = array_keys($this->info); 00068 $this->values = array_values($this->info); 00069 } 00070 00076 public function generateChildDef(&$def, $module) { 00077 if (!empty($def->child)) return; // already done! 00078 $content_model = $def->content_model; 00079 if (is_string($content_model)) { 00080 // Assume that $this->keys is alphanumeric 00081 $def->content_model = preg_replace_callback( 00082 '/\b(' . implode('|', $this->keys) . ')\b/', 00083 array($this, 'generateChildDefCallback'), 00084 $content_model 00085 ); 00086 //$def->content_model = str_replace( 00087 // $this->keys, $this->values, $content_model); 00088 } 00089 $def->child = $this->getChildDef($def, $module); 00090 } 00091 00092 public function generateChildDefCallback($matches) { 00093 return $this->info[$matches[0]]; 00094 } 00095 00104 public function getChildDef($def, $module) { 00105 $value = $def->content_model; 00106 if (is_object($value)) { 00107 trigger_error( 00108 'Literal object child definitions should be stored in '. 00109 'ElementDef->child not ElementDef->content_model', 00110 E_USER_NOTICE 00111 ); 00112 return $value; 00113 } 00114 switch ($def->content_model_type) { 00115 case 'required': 00116 return new HTMLPurifier_ChildDef_Required($value); 00117 case 'optional': 00118 return new HTMLPurifier_ChildDef_Optional($value); 00119 case 'empty': 00120 return new HTMLPurifier_ChildDef_Empty(); 00121 case 'custom': 00122 return new HTMLPurifier_ChildDef_Custom($value); 00123 } 00124 // defer to its module 00125 $return = false; 00126 if ($module->defines_child_def) { // save a func call 00127 $return = $module->getChildDef($def); 00128 } 00129 if ($return !== false) return $return; 00130 // error-out 00131 trigger_error( 00132 'Could not determine which ChildDef class to instantiate', 00133 E_USER_ERROR 00134 ); 00135 return false; 00136 } 00137 00144 protected function convertToLookup($string) { 00145 $array = explode('|', str_replace(' ', '', $string)); 00146 $ret = array(); 00147 foreach ($array as $i => $k) { 00148 $ret[$k] = true; 00149 } 00150 return $ret; 00151 } 00152 00153 } 00154 00155 // vim: et sw=4 sts=4