Don't add nofollow for matching hosts, generalize this code.
[htmlpurifier.git] / library / HTMLPurifier / ElementDef.php
blob5498d9567040ac17208c16ce78b037e57dd885fc
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 * @warning If you add new properties to this class, you MUST update
9 * the mergeIn() method.
11 class HTMLPurifier_ElementDef
14 /**
15 * Does the definition work by itself, or is it created solely
16 * for the purpose of merging into another definition?
18 public $standalone = true;
20 /**
21 * Associative array of attribute name to HTMLPurifier_AttrDef
22 * @note Before being processed by HTMLPurifier_AttrCollections
23 * when modules are finalized during
24 * HTMLPurifier_HTMLDefinition->setup(), this array may also
25 * contain an array at index 0 that indicates which attribute
26 * collections to load into the full array. It may also
27 * contain string indentifiers in lieu of HTMLPurifier_AttrDef,
28 * see HTMLPurifier_AttrTypes on how they are expanded during
29 * HTMLPurifier_HTMLDefinition->setup() processing.
31 public $attr = array();
33 /**
34 * Indexed list of tag's HTMLPurifier_AttrTransform to be done before validation
36 public $attr_transform_pre = array();
38 /**
39 * Indexed list of tag's HTMLPurifier_AttrTransform to be done after validation
41 public $attr_transform_post = array();
43 /**
44 * HTMLPurifier_ChildDef of this tag.
46 public $child;
48 /**
49 * Abstract string representation of internal ChildDef rules. See
50 * HTMLPurifier_ContentSets for how this is parsed and then transformed
51 * into an HTMLPurifier_ChildDef.
52 * @warning This is a temporary variable that is not available after
53 * being processed by HTMLDefinition
55 public $content_model;
57 /**
58 * Value of $child->type, used to determine which ChildDef to use,
59 * used in combination with $content_model.
60 * @warning This must be lowercase
61 * @warning This is a temporary variable that is not available after
62 * being processed by HTMLDefinition
64 public $content_model_type;
68 /**
69 * Does the element have a content model (#PCDATA | Inline)*? This
70 * is important for chameleon ins and del processing in
71 * HTMLPurifier_ChildDef_Chameleon. Dynamically set: modules don't
72 * have to worry about this one.
74 public $descendants_are_inline = false;
76 /**
77 * List of the names of required attributes this element has. Dynamically
78 * populated by HTMLPurifier_HTMLDefinition::getElement
80 public $required_attr = array();
82 /**
83 * Lookup table of tags excluded from all descendants of this tag.
84 * @note SGML permits exclusions for all descendants, but this is
85 * not possible with DTDs or XML Schemas. W3C has elected to
86 * use complicated compositions of content_models to simulate
87 * exclusion for children, but we go the simpler, SGML-style
88 * route of flat-out exclusions, which correctly apply to
89 * all descendants and not just children. Note that the XHTML
90 * Modularization Abstract Modules are blithely unaware of such
91 * distinctions.
93 public $excludes = array();
95 /**
96 * This tag is explicitly auto-closed by the following tags.
98 public $autoclose = array();
101 * If a foreign element is found in this element, test if it is
102 * allowed by this sub-element; if it is, instead of closing the
103 * current element, place it inside this element.
105 public $wrap;
108 * Whether or not this is a formatting element affected by the
109 * "Active Formatting Elements" algorithm.
111 public $formatting;
114 * Low-level factory constructor for creating new standalone element defs
116 public static function create($content_model, $content_model_type, $attr) {
117 $def = new HTMLPurifier_ElementDef();
118 $def->content_model = $content_model;
119 $def->content_model_type = $content_model_type;
120 $def->attr = $attr;
121 return $def;
125 * Merges the values of another element definition into this one.
126 * Values from the new element def take precedence if a value is
127 * not mergeable.
129 public function mergeIn($def) {
131 // later keys takes precedence
132 foreach($def->attr as $k => $v) {
133 if ($k === 0) {
134 // merge in the includes
135 // sorry, no way to override an include
136 foreach ($v as $v2) {
137 $this->attr[0][] = $v2;
139 continue;
141 if ($v === false) {
142 if (isset($this->attr[$k])) unset($this->attr[$k]);
143 continue;
145 $this->attr[$k] = $v;
147 $this->_mergeAssocArray($this->attr_transform_pre, $def->attr_transform_pre);
148 $this->_mergeAssocArray($this->attr_transform_post, $def->attr_transform_post);
149 $this->_mergeAssocArray($this->excludes, $def->excludes);
151 if(!empty($def->content_model)) {
152 $this->content_model =
153 str_replace("#SUPER", $this->content_model, $def->content_model);
154 $this->child = false;
156 if(!empty($def->content_model_type)) {
157 $this->content_model_type = $def->content_model_type;
158 $this->child = false;
160 if(!is_null($def->child)) $this->child = $def->child;
161 if(!is_null($def->formatting)) $this->formatting = $def->formatting;
162 if($def->descendants_are_inline) $this->descendants_are_inline = $def->descendants_are_inline;
167 * Merges one array into another, removes values which equal false
168 * @param $a1 Array by reference that is merged into
169 * @param $a2 Array that merges into $a1
171 private function _mergeAssocArray(&$a1, $a2) {
172 foreach ($a2 as $k => $v) {
173 if ($v === false) {
174 if (isset($a1[$k])) unset($a1[$k]);
175 continue;
177 $a1[$k] = $v;
183 // vim: et sw=4 sts=4