Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / ElementDef.php
blobf2c8f77a7eedaa17b97e85d60c4a282ceaee9a30
1 <?php
3 /**
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 */
9 class HTMLPurifier_ElementDef
12 /**
13 * Does the definition work by itself, or is it created solely
14 * for the purpose of merging into another definition?
16 var $standalone = true;
18 /**
19 * Associative array of attribute name to HTMLPurifier_AttrDef
20 * @note Before being processed by HTMLPurifier_AttrCollections
21 * when modules are finalized during
22 * HTMLPurifier_HTMLDefinition->setup(), this array may also
23 * contain an array at index 0 that indicates which attribute
24 * collections to load into the full array. It may also
25 * contain string indentifiers in lieu of HTMLPurifier_AttrDef,
26 * see HTMLPurifier_AttrTypes on how they are expanded during
27 * HTMLPurifier_HTMLDefinition->setup() processing.
28 * @public
30 var $attr = array();
32 /**
33 * Indexed list of tag's HTMLPurifier_AttrTransform to be done before validation
34 * @public
36 var $attr_transform_pre = array();
38 /**
39 * Indexed list of tag's HTMLPurifier_AttrTransform to be done after validation
40 * @public
42 var $attr_transform_post = array();
46 /**
47 * HTMLPurifier_ChildDef of this tag.
48 * @public
50 var $child;
52 /**
53 * Abstract string representation of internal ChildDef rules. See
54 * HTMLPurifier_ContentSets for how this is parsed and then transformed
55 * into an HTMLPurifier_ChildDef.
56 * @warning This is a temporary variable that is not available after
57 * being processed by HTMLDefinition
58 * @public
60 var $content_model;
62 /**
63 * Value of $child->type, used to determine which ChildDef to use,
64 * used in combination with $content_model.
65 * @warning This must be lowercase
66 * @warning This is a temporary variable that is not available after
67 * being processed by HTMLDefinition
68 * @public
70 var $content_model_type;
74 /**
75 * Does the element have a content model (#PCDATA | Inline)*? This
76 * is important for chameleon ins and del processing in
77 * HTMLPurifier_ChildDef_Chameleon. Dynamically set: modules don't
78 * have to worry about this one.
79 * @public
81 var $descendants_are_inline = false;
83 /**
84 * List of the names of required attributes this element has. Dynamically
85 * populated.
86 * @public
88 var $required_attr = array();
90 /**
91 * Lookup table of tags excluded from all descendants of this tag.
92 * @note SGML permits exclusions for all descendants, but this is
93 * not possible with DTDs or XML Schemas. W3C has elected to
94 * use complicated compositions of content_models to simulate
95 * exclusion for children, but we go the simpler, SGML-style
96 * route of flat-out exclusions, which correctly apply to
97 * all descendants and not just children. Note that the XHTML
98 * Modularization Abstract Modules are blithely unaware of such
99 * distinctions.
100 * @public
102 var $excludes = array();
105 * Is this element safe for untrusted users to use?
107 var $safe;
110 * Low-level factory constructor for creating new standalone element defs
111 * @static
113 static function create($safe, $content_model, $content_model_type, $attr) {
114 $def = new HTMLPurifier_ElementDef();
115 $def->safe = (bool) $safe;
116 $def->content_model = $content_model;
117 $def->content_model_type = $content_model_type;
118 $def->attr = $attr;
119 return $def;
123 * Merges the values of another element definition into this one.
124 * Values from the new element def take precedence if a value is
125 * not mergeable.
127 function mergeIn($def) {
129 // later keys takes precedence
130 foreach($def->attr as $k => $v) {
131 if ($k === 0) {
132 // merge in the includes
133 // sorry, no way to override an include
134 foreach ($v as $v2) {
135 $this->attr[0][] = $v2;
137 continue;
139 if ($v === false) {
140 if (isset($this->attr[$k])) unset($this->attr[$k]);
141 continue;
143 $this->attr[$k] = $v;
145 $this->_mergeAssocArray($this->attr_transform_pre, $def->attr_transform_pre);
146 $this->_mergeAssocArray($this->attr_transform_post, $def->attr_transform_post);
147 $this->_mergeAssocArray($this->excludes, $def->excludes);
149 if(!empty($def->content_model)) {
150 $this->content_model .= ' | ' . $def->content_model;
151 $this->child = false;
153 if(!empty($def->content_model_type)) {
154 $this->content_model_type = $def->content_model_type;
155 $this->child = false;
157 if(!is_null($def->child)) $this->child = $def->child;
158 if($def->descendants_are_inline) $this->descendants_are_inline = $def->descendants_are_inline;
159 if(!is_null($def->safe)) $this->safe = $def->safe;
164 * Merges one array into another, removes values which equal false
165 * @param $a1 Array by reference that is merged into
166 * @param $a2 Array that merges into $a1
168 function _mergeAssocArray(&$a1, $a2) {
169 foreach ($a2 as $k => $v) {
170 if ($v === false) {
171 if (isset($a1[$k])) unset($a1[$k]);
172 continue;
174 $a1[$k] = $v;
179 * Retrieves a copy of the element definition
181 function copy() {
182 return unserialize(serialize($this));