4 * Structure that stores an HTML element definition. Used by
5 * HTMLPurifier_HTMLDefinition and HTMLPurifier_HTMLModule.
6 * @note This class is inspected by HTMLPurifier_Printer_HTMLDefinition.
7 * Please update that class too.
8 * @warning If you add new properties to this class, you MUST update
9 * the mergeIn() method.
11 class HTMLPurifier_ElementDef
14 * Does the definition work by itself, or is it created solely
15 * for the purpose of merging into another definition?
18 public $standalone = true;
21 * Associative array of attribute name to HTMLPurifier_AttrDef.
23 * @note Before being processed by HTMLPurifier_AttrCollections
24 * when modules are finalized during
25 * HTMLPurifier_HTMLDefinition->setup(), this array may also
26 * contain an array at index 0 that indicates which attribute
27 * collections to load into the full array. It may also
28 * contain string indentifiers in lieu of HTMLPurifier_AttrDef,
29 * see HTMLPurifier_AttrTypes on how they are expanded during
30 * HTMLPurifier_HTMLDefinition->setup() processing.
32 public $attr = array();
34 // XXX: Design note: currently, it's not possible to override
35 // previously defined AttrTransforms without messing around with
36 // the final generated config. This is by design; a previous version
37 // used an associated list of attr_transform, but it was extremely
38 // easy to accidentally override other attribute transforms by
39 // forgetting to specify an index (and just using 0.) While we
40 // could check this by checking the index number and complaining,
41 // there is a second problem which is that it is not at all easy to
42 // tell when something is getting overridden. Combine this with a
43 // codebase where this isn't really being used, and it's perfect for
47 * List of tags HTMLPurifier_AttrTransform to be done before validation.
50 public $attr_transform_pre = array();
53 * List of tags HTMLPurifier_AttrTransform to be done after validation.
56 public $attr_transform_post = array();
59 * HTMLPurifier_ChildDef of this tag.
60 * @type HTMLPurifier_ChildDef
65 * Abstract string representation of internal ChildDef rules.
66 * @see HTMLPurifier_ContentSets for how this is parsed and then transformed
67 * into an HTMLPurifier_ChildDef.
68 * @warning This is a temporary variable that is not available after
69 * being processed by HTMLDefinition
72 public $content_model;
75 * Value of $child->type, used to determine which ChildDef to use,
76 * used in combination with $content_model.
77 * @warning This must be lowercase
78 * @warning This is a temporary variable that is not available after
79 * being processed by HTMLDefinition
82 public $content_model_type;
85 * Does the element have a content model (#PCDATA | Inline)*? This
86 * is important for chameleon ins and del processing in
87 * HTMLPurifier_ChildDef_Chameleon. Dynamically set: modules don't
88 * have to worry about this one.
91 public $descendants_are_inline = false;
94 * List of the names of required attributes this element has.
95 * Dynamically populated by HTMLPurifier_HTMLDefinition::getElement()
98 public $required_attr = array();
101 * Lookup table of tags excluded from all descendants of this tag.
103 * @note SGML permits exclusions for all descendants, but this is
104 * not possible with DTDs or XML Schemas. W3C has elected to
105 * use complicated compositions of content_models to simulate
106 * exclusion for children, but we go the simpler, SGML-style
107 * route of flat-out exclusions, which correctly apply to
108 * all descendants and not just children. Note that the XHTML
109 * Modularization Abstract Modules are blithely unaware of such
112 public $excludes = array();
115 * This tag is explicitly auto-closed by the following tags.
118 public $autoclose = array();
121 * If a foreign element is found in this element, test if it is
122 * allowed by this sub-element; if it is, instead of closing the
123 * current element, place it inside this element.
129 * Whether or not this is a formatting element affected by the
130 * "Active Formatting Elements" algorithm.
136 * Low-level factory constructor for creating new standalone element defs
138 public static function create($content_model, $content_model_type, $attr)
140 $def = new HTMLPurifier_ElementDef();
141 $def->content_model
= $content_model;
142 $def->content_model_type
= $content_model_type;
148 * Merges the values of another element definition into this one.
149 * Values from the new element def take precedence if a value is
151 * @param HTMLPurifier_ElementDef $def
153 public function mergeIn($def)
155 // later keys takes precedence
156 foreach ($def->attr
as $k => $v) {
158 // merge in the includes
159 // sorry, no way to override an include
160 foreach ($v as $v2) {
161 $this->attr
[0][] = $v2;
166 if (isset($this->attr
[$k])) {
167 unset($this->attr
[$k]);
171 $this->attr
[$k] = $v;
173 $this->_mergeAssocArray($this->excludes
, $def->excludes
);
174 $this->attr_transform_pre
= array_merge($this->attr_transform_pre
, $def->attr_transform_pre
);
175 $this->attr_transform_post
= array_merge($this->attr_transform_post
, $def->attr_transform_post
);
177 if (!empty($def->content_model
)) {
178 $this->content_model
=
179 str_replace("#SUPER", $this->content_model
, $def->content_model
);
180 $this->child
= false;
182 if (!empty($def->content_model_type
)) {
183 $this->content_model_type
= $def->content_model_type
;
184 $this->child
= false;
186 if (!is_null($def->child
)) {
187 $this->child
= $def->child
;
189 if (!is_null($def->formatting
)) {
190 $this->formatting
= $def->formatting
;
192 if ($def->descendants_are_inline
) {
193 $this->descendants_are_inline
= $def->descendants_are_inline
;
198 * Merges one array into another, removes values which equal false
199 * @param $a1 Array by reference that is merged into
200 * @param $a2 Array that merges into $a1
202 private function _mergeAssocArray(&$a1, $a2)
204 foreach ($a2 as $k => $v) {
206 if (isset($a1[$k])) {
216 // vim: et sw=4 sts=4