Source for file HTMLModule.php

Documentation is available at HTMLModule.php

  1. <?php
  2.  
  3. /**
  4.  * Represents an XHTML 1.1 module, with information on elements, tags
  5.  * and attributes.
  6.  * @note Even though this is technically XHTML 1.1, it is also used for
  7.  *        regular HTML parsing. We are using modulization as a convenient
  8.  *        way to represent the internals of HTMLDefinition, and our
  9.  *        implementation is by no means conforming and does not directly
  10.  *        use the normative DTDs or XML schemas.
  11.  * @note The public variables in a module should almost directly
  12.  *        correspond to the variables in HTMLPurifier_HTMLDefinition.
  13.  *        However, the prefix info carries no special meaning in these
  14.  *        objects (include it anyway if that's the correspondence though).
  15.  * @todo Consider making some member functions protected
  16.  */
  17.  
  18. {
  19.     
  20.     // -- Overloadable ----------------------------------------------------
  21.     
  22.     /**
  23.      * Short unique string identifier of the module
  24.      */
  25.     public $name;
  26.     
  27.     /**
  28.      * Informally, a list of elements this module changes. Not used in
  29.      * any significant way.
  30.      */
  31.     public $elements = array();
  32.     
  33.     /**
  34.      * Associative array of element names to element definitions.
  35.      * Some definitions may be incomplete, to be merged in later
  36.      * with the full definition.
  37.      */
  38.     public $info = array();
  39.     
  40.     /**
  41.      * Associative array of content set names to content set additions.
  42.      * This is commonly used to, say, add an A element to the Inline
  43.      * content set. This corresponds to an internal variable $content_sets
  44.      * and NOT info_content_sets member variable of HTMLDefinition.
  45.      */
  46.     public $content_sets = array();
  47.     
  48.     /**
  49.      * Associative array of attribute collection names to attribute
  50.      * collection additions. More rarely used for adding attributes to
  51.      * the global collections. Example is the StyleAttribute module adding
  52.      * the style attribute to the Core. Corresponds to HTMLDefinition's
  53.      * attr_collections->info, since the object's data is only info,
  54.      * with extra behavior associated with it.
  55.      */
  56.     public $attr_collections = array();
  57.     
  58.     /**
  59.      * Associative array of deprecated tag name to HTMLPurifier_TagTransform
  60.      */
  61.     public $info_tag_transform = array();
  62.     
  63.     /**
  64.      * List of HTMLPurifier_AttrTransform to be performed before validation.
  65.      */
  66.     public $info_attr_transform_pre = array();
  67.     
  68.     /**
  69.      * List of HTMLPurifier_AttrTransform to be performed after validation.
  70.      */
  71.     public $info_attr_transform_post = array();
  72.     
  73.     /**
  74.      * List of HTMLPurifier_Injector to be performed during well-formedness fixing.
  75.      * An injector will only be invoked if all of it's pre-requisites are met;
  76.      * if an injector fails setup, there will be no error; it will simply be
  77.      * silently disabled.
  78.      */
  79.     public $info_injector = array();
  80.     
  81.     /**
  82.      * Boolean flag that indicates whether or not getChildDef is implemented.
  83.      * For optimization reasons: may save a call to a function. Be sure
  84.      * to set it if you do implement getChildDef(), otherwise it will have
  85.      * no effect!
  86.      */
  87.     public $defines_child_def = false;
  88.     
  89.     /**
  90.      * Boolean flag whether or not this module is safe. If it is not safe, all
  91.      * of its members are unsafe. Modules are safe by default (this might be
  92.      * slightly dangerous, but it doesn't make much sense to force HTML Purifier,
  93.      * which is based off of safe HTML, to explicitly say, "This is safe," even
  94.      * though there are modules which are "unsafe")
  95.      * 
  96.      * @note Previously, safety could be applied at an element level granularity.
  97.      *        We've removed this ability, so in order to add "unsafe" elements
  98.      *        or attributes, a dedicated module with this property set to false
  99.      *        must be used.
  100.      */
  101.     public $safe = true;
  102.     
  103.     /**
  104.      * Retrieves a proper HTMLPurifier_ChildDef subclass based on
  105.      * content_model and content_model_type member variables of
  106.      * the HTMLPurifier_ElementDef class. There is a similar function
  107.      * in HTMLPurifier_HTMLDefinition.
  108.      * @param $def HTMLPurifier_ElementDef instance
  109.      * @return HTMLPurifier_ChildDef subclass
  110.      */
  111.     public function getChildDef($def{return false;}
  112.     
  113.     // -- Convenience -----------------------------------------------------
  114.     
  115.     /**
  116.      * Convenience function that sets up a new element
  117.      * @param $element Name of element to add
  118.      * @param $type What content set should element be registered to?
  119.      *               Set as false to skip this step.
  120.      * @param $contents Allowed children in form of:
  121.      *               "$content_model_type: $content_model"
  122.      * @param $attr_includes What attribute collections to register to
  123.      *               element?
  124.      * @param $attr What unique attributes does the element define?
  125.      * @note See ElementDef for in-depth descriptions of these parameters.
  126.      * @return Created element definition object, so you
  127.      *          can set advanced parameters
  128.      */
  129.     public function addElement($element$type$contents$attr_includes array()$attr array()) {
  130.         $this->elements[$element;
  131.         // parse content_model
  132.         list($content_model_type$content_model$this->parseContents($contents);
  133.         // merge in attribute inclusions
  134.         $this->mergeInAttrIncludes($attr$attr_includes);
  135.         // add element to content sets
  136.         if ($type$this->addElementToContentSet($element$type);
  137.         // create element
  138.         $this->info[$elementHTMLPurifier_ElementDef::create(
  139.             $content_model$content_model_type$attr
  140.         );
  141.         // literal object $contents means direct child manipulation
  142.         if (!is_string($contents)) $this->info[$element]->child $contents;
  143.         return $this->info[$element];
  144.     }
  145.     
  146.     /**
  147.      * Convenience function that creates a totally blank, non-standalone
  148.      * element.
  149.      * @param $element Name of element to create
  150.      * @return Created element
  151.      */
  152.     public function addBlankElement($element{
  153.         if (!isset($this->info[$element])) {
  154.             $this->elements[$element;
  155.             $this->info[$elementnew HTMLPurifier_ElementDef();
  156.             $this->info[$element]->standalone false;
  157.         else {
  158.             trigger_error("Definition for $element already exists in module, cannot redefine");
  159.         }
  160.         return $this->info[$element];
  161.     }
  162.     
  163.     /**
  164.      * Convenience function that registers an element to a content set
  165.      * @param Element to register
  166.      * @param Name content set (warning: case sensitive, usually upper-case
  167.      *         first letter)
  168.      */
  169.     public function addElementToContentSet($element$type{
  170.         if (!isset($this->content_sets[$type])) $this->content_sets[$type'';
  171.         else $this->content_sets[$type.= ' | ';
  172.         $this->content_sets[$type.= $element;
  173.     }
  174.     
  175.     /**
  176.      * Convenience function that transforms single-string contents
  177.      * into separate content model and content model type
  178.      * @param $contents Allowed children in form of:
  179.      *                   "$content_model_type: $content_model"
  180.      * @note If contents is an object, an array of two nulls will be
  181.      *        returned, and the callee needs to take the original $contents
  182.      *        and use it directly.
  183.      */
  184.     public function parseContents($contents{
  185.         if (!is_string($contents)) return array(nullnull)// defer
  186.         switch ($contents{
  187.             // check for shorthand content model forms
  188.             case 'Empty':
  189.                 return array('empty''');
  190.             case 'Inline':
  191.                 return array('optional''Inline | #PCDATA');
  192.             case 'Flow':
  193.                 return array('optional''Flow | #PCDATA');
  194.         }
  195.         list($content_model_type$content_modelexplode(':'$contents);
  196.         $content_model_type strtolower(trim($content_model_type));
  197.         $content_model trim($content_model);
  198.         return array($content_model_type$content_model);
  199.     }
  200.     
  201.     /**
  202.      * Convenience function that merges a list of attribute includes into
  203.      * an attribute array.
  204.      * @param $attr Reference to attr array to modify
  205.      * @param $attr_includes Array of includes / string include to merge in
  206.      */
  207.     public function mergeInAttrIncludes(&$attr$attr_includes{
  208.         if (!is_array($attr_includes)) {
  209.             if (empty($attr_includes)) $attr_includes array();
  210.             else $attr_includes array($attr_includes);
  211.         }
  212.         $attr[0$attr_includes;
  213.     }
  214.     
  215.     /**
  216.      * Convenience function that generates a lookup table with boolean
  217.      * true as value.
  218.      * @param $list List of values to turn into a lookup
  219.      * @note You can also pass an arbitrary number of arguments in
  220.      *        place of the regular argument
  221.      * @return Lookup array equivalent of list
  222.      */
  223.     public function makeLookup($list{
  224.         if (is_string($list)) $list func_get_args();
  225.         $ret array();
  226.         foreach ($list as $value{
  227.             if (is_null($value)) continue;
  228.             $ret[$valuetrue;
  229.         }
  230.         return $ret;
  231.     }
  232.     
  233.     /**
  234.      * Lazy load construction of the module after determining whether
  235.      * or not it's needed, and also when a finalized configuration object
  236.      * is available.
  237.      * @param $config Instance of HTMLPurifier_Config
  238.      */
  239.     public function setup($config{}
  240.     
  241. }

Documentation generated on Thu, 19 Jun 2008 18:49:30 -0400 by phpDocumentor 1.4.2