HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/HTMLModule.php
Go to the documentation of this file.
00001 <?php
00002 
00018 class HTMLPurifier_HTMLModule
00019 {
00020 
00021     // -- Overloadable ----------------------------------------------------
00022 
00026     public $name;
00027 
00032     public $elements = array();
00033 
00039     public $info = array();
00040 
00047     public $content_sets = array();
00048 
00057     public $attr_collections = array();
00058 
00062     public $info_tag_transform = array();
00063 
00067     public $info_attr_transform_pre = array();
00068 
00072     public $info_attr_transform_post = array();
00073 
00080     public $info_injector = array();
00081 
00088     public $defines_child_def = false;
00089 
00102     public $safe = true;
00103 
00112     public function getChildDef($def) {return false;}
00113 
00114     // -- Convenience -----------------------------------------------------
00115 
00130     public function addElement($element, $type, $contents, $attr_includes = array(), $attr = array()) {
00131         $this->elements[] = $element;
00132         // parse content_model
00133         list($content_model_type, $content_model) = $this->parseContents($contents);
00134         // merge in attribute inclusions
00135         $this->mergeInAttrIncludes($attr, $attr_includes);
00136         // add element to content sets
00137         if ($type) $this->addElementToContentSet($element, $type);
00138         // create element
00139         $this->info[$element] = HTMLPurifier_ElementDef::create(
00140             $content_model, $content_model_type, $attr
00141         );
00142         // literal object $contents means direct child manipulation
00143         if (!is_string($contents)) $this->info[$element]->child = $contents;
00144         return $this->info[$element];
00145     }
00146 
00153     public function addBlankElement($element) {
00154         if (!isset($this->info[$element])) {
00155             $this->elements[] = $element;
00156             $this->info[$element] = new HTMLPurifier_ElementDef();
00157             $this->info[$element]->standalone = false;
00158         } else {
00159             trigger_error("Definition for $element already exists in module, cannot redefine");
00160         }
00161         return $this->info[$element];
00162     }
00163 
00170     public function addElementToContentSet($element, $type) {
00171         if (!isset($this->content_sets[$type])) $this->content_sets[$type] = '';
00172         else $this->content_sets[$type] .= ' | ';
00173         $this->content_sets[$type] .= $element;
00174     }
00175 
00185     public function parseContents($contents) {
00186         if (!is_string($contents)) return array(null, null); // defer
00187         switch ($contents) {
00188             // check for shorthand content model forms
00189             case 'Empty':
00190                 return array('empty', '');
00191             case 'Inline':
00192                 return array('optional', 'Inline | #PCDATA');
00193             case 'Flow':
00194                 return array('optional', 'Flow | #PCDATA');
00195         }
00196         list($content_model_type, $content_model) = explode(':', $contents);
00197         $content_model_type = strtolower(trim($content_model_type));
00198         $content_model = trim($content_model);
00199         return array($content_model_type, $content_model);
00200     }
00201 
00208     public function mergeInAttrIncludes(&$attr, $attr_includes) {
00209         if (!is_array($attr_includes)) {
00210             if (empty($attr_includes)) $attr_includes = array();
00211             else $attr_includes = array($attr_includes);
00212         }
00213         $attr[0] = $attr_includes;
00214     }
00215 
00224     public function makeLookup($list) {
00225         if (is_string($list)) $list = func_get_args();
00226         $ret = array();
00227         foreach ($list as $value) {
00228             if (is_null($value)) continue;
00229             $ret[$value] = true;
00230         }
00231         return $ret;
00232     }
00233 
00240     public function setup($config) {}
00241 
00242 }
00243 
00244 // vim: et sw=4 sts=4