File indexing completed on 2024-04-28 05:58:53

0001 <?php
0002 
0003 /**
0004  * Structure that stores an HTML element definition. Used by
0005  * HTMLPurifier_HTMLDefinition and HTMLPurifier_HTMLModule.
0006  * @note This class is inspected by HTMLPurifier_Printer_HTMLDefinition.
0007  *       Please update that class too.
0008  * @warning If you add new properties to this class, you MUST update
0009  *          the mergeIn() method.
0010  */
0011 class HTMLPurifier_ElementDef
0012 {
0013     /**
0014      * Does the definition work by itself, or is it created solely
0015      * for the purpose of merging into another definition?
0016      * @type bool
0017      */
0018     public $standalone = true;
0019 
0020     /**
0021      * Associative array of attribute name to HTMLPurifier_AttrDef.
0022      * @type array
0023      * @note Before being processed by HTMLPurifier_AttrCollections
0024      *       when modules are finalized during
0025      *       HTMLPurifier_HTMLDefinition->setup(), this array may also
0026      *       contain an array at index 0 that indicates which attribute
0027      *       collections to load into the full array. It may also
0028      *       contain string indentifiers in lieu of HTMLPurifier_AttrDef,
0029      *       see HTMLPurifier_AttrTypes on how they are expanded during
0030      *       HTMLPurifier_HTMLDefinition->setup() processing.
0031      */
0032     public $attr = array();
0033 
0034     // XXX: Design note: currently, it's not possible to override
0035     // previously defined AttrTransforms without messing around with
0036     // the final generated config. This is by design; a previous version
0037     // used an associated list of attr_transform, but it was extremely
0038     // easy to accidentally override other attribute transforms by
0039     // forgetting to specify an index (and just using 0.)  While we
0040     // could check this by checking the index number and complaining,
0041     // there is a second problem which is that it is not at all easy to
0042     // tell when something is getting overridden. Combine this with a
0043     // codebase where this isn't really being used, and it's perfect for
0044     // nuking.
0045 
0046     /**
0047      * List of tags HTMLPurifier_AttrTransform to be done before validation.
0048      * @type array
0049      */
0050     public $attr_transform_pre = array();
0051 
0052     /**
0053      * List of tags HTMLPurifier_AttrTransform to be done after validation.
0054      * @type array
0055      */
0056     public $attr_transform_post = array();
0057 
0058     /**
0059      * HTMLPurifier_ChildDef of this tag.
0060      * @type HTMLPurifier_ChildDef
0061      */
0062     public $child;
0063 
0064     /**
0065      * Abstract string representation of internal ChildDef rules.
0066      * @see HTMLPurifier_ContentSets for how this is parsed and then transformed
0067      * into an HTMLPurifier_ChildDef.
0068      * @warning This is a temporary variable that is not available after
0069      *      being processed by HTMLDefinition
0070      * @type string
0071      */
0072     public $content_model;
0073 
0074     /**
0075      * Value of $child->type, used to determine which ChildDef to use,
0076      * used in combination with $content_model.
0077      * @warning This must be lowercase
0078      * @warning This is a temporary variable that is not available after
0079      *      being processed by HTMLDefinition
0080      * @type string
0081      */
0082     public $content_model_type;
0083 
0084     /**
0085      * Does the element have a content model (#PCDATA | Inline)*? This
0086      * is important for chameleon ins and del processing in
0087      * HTMLPurifier_ChildDef_Chameleon. Dynamically set: modules don't
0088      * have to worry about this one.
0089      * @type bool
0090      */
0091     public $descendants_are_inline = false;
0092 
0093     /**
0094      * List of the names of required attributes this element has.
0095      * Dynamically populated by HTMLPurifier_HTMLDefinition::getElement()
0096      * @type array
0097      */
0098     public $required_attr = array();
0099 
0100     /**
0101      * Lookup table of tags excluded from all descendants of this tag.
0102      * @type array
0103      * @note SGML permits exclusions for all descendants, but this is
0104      *       not possible with DTDs or XML Schemas. W3C has elected to
0105      *       use complicated compositions of content_models to simulate
0106      *       exclusion for children, but we go the simpler, SGML-style
0107      *       route of flat-out exclusions, which correctly apply to
0108      *       all descendants and not just children. Note that the XHTML
0109      *       Modularization Abstract Modules are blithely unaware of such
0110      *       distinctions.
0111      */
0112     public $excludes = array();
0113 
0114     /**
0115      * This tag is explicitly auto-closed by the following tags.
0116      * @type array
0117      */
0118     public $autoclose = array();
0119 
0120     /**
0121      * If a foreign element is found in this element, test if it is
0122      * allowed by this sub-element; if it is, instead of closing the
0123      * current element, place it inside this element.
0124      * @type string
0125      */
0126     public $wrap;
0127 
0128     /**
0129      * Whether or not this is a formatting element affected by the
0130      * "Active Formatting Elements" algorithm.
0131      * @type bool
0132      */
0133     public $formatting;
0134 
0135     /**
0136      * Low-level factory constructor for creating new standalone element defs
0137      */
0138     public static function create($content_model, $content_model_type, $attr)
0139     {
0140         $def = new HTMLPurifier_ElementDef();
0141         $def->content_model = $content_model;
0142         $def->content_model_type = $content_model_type;
0143         $def->attr = $attr;
0144         return $def;
0145     }
0146 
0147     /**
0148      * Merges the values of another element definition into this one.
0149      * Values from the new element def take precedence if a value is
0150      * not mergeable.
0151      * @param HTMLPurifier_ElementDef $def
0152      */
0153     public function mergeIn($def)
0154     {
0155         // later keys takes precedence
0156         foreach ($def->attr as $k => $v) {
0157             if ($k === 0) {
0158                 // merge in the includes
0159                 // sorry, no way to override an include
0160                 foreach ($v as $v2) {
0161                     $this->attr[0][] = $v2;
0162                 }
0163                 continue;
0164             }
0165             if ($v === false) {
0166                 if (isset($this->attr[$k])) {
0167                     unset($this->attr[$k]);
0168                 }
0169                 continue;
0170             }
0171             $this->attr[$k] = $v;
0172         }
0173         $this->_mergeAssocArray($this->excludes, $def->excludes);
0174         $this->attr_transform_pre = array_merge($this->attr_transform_pre, $def->attr_transform_pre);
0175         $this->attr_transform_post = array_merge($this->attr_transform_post, $def->attr_transform_post);
0176 
0177         if (!empty($def->content_model)) {
0178             $this->content_model =
0179                 str_replace("#SUPER", $this->content_model, $def->content_model);
0180             $this->child = false;
0181         }
0182         if (!empty($def->content_model_type)) {
0183             $this->content_model_type = $def->content_model_type;
0184             $this->child = false;
0185         }
0186         if (!is_null($def->child)) {
0187             $this->child = $def->child;
0188         }
0189         if (!is_null($def->formatting)) {
0190             $this->formatting = $def->formatting;
0191         }
0192         if ($def->descendants_are_inline) {
0193             $this->descendants_are_inline = $def->descendants_are_inline;
0194         }
0195     }
0196 
0197     /**
0198      * Merges one array into another, removes values which equal false
0199      * @param $a1 Array by reference that is merged into
0200      * @param $a2 Array that merges into $a1
0201      */
0202     private function _mergeAssocArray(&$a1, $a2)
0203     {
0204         foreach ($a2 as $k => $v) {
0205             if ($v === false) {
0206                 if (isset($a1[$k])) {
0207                     unset($a1[$k]);
0208                 }
0209                 continue;
0210             }
0211             $a1[$k] = $v;
0212         }
0213     }
0214 }
0215 
0216 // vim: et sw=4 sts=4