4 * Represents an XHTML 1.1 module, with information on elements, tags
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
18 class HTMLPurifier_HTMLModule
21 // -- Overloadable ----------------------------------------------------
24 * Short unique string identifier of the module
29 * Informally, a list of elements this module changes. Not used in
30 * any significant way.
32 public $elements = array();
35 * Associative array of element names to element definitions.
36 * Some definitions may be incomplete, to be merged in later
37 * with the full definition.
39 public $info = array();
42 * Associative array of content set names to content set additions.
43 * This is commonly used to, say, add an A element to the Inline
44 * content set. This corresponds to an internal variable $content_sets
45 * and NOT info_content_sets member variable of HTMLDefinition.
47 public $content_sets = array();
50 * Associative array of attribute collection names to attribute
51 * collection additions. More rarely used for adding attributes to
52 * the global collections. Example is the StyleAttribute module adding
53 * the style attribute to the Core. Corresponds to HTMLDefinition's
54 * attr_collections->info, since the object's data is only info,
55 * with extra behavior associated with it.
57 public $attr_collections = array();
60 * Associative array of deprecated tag name to HTMLPurifier_TagTransform
62 public $info_tag_transform = array();
65 * List of HTMLPurifier_AttrTransform to be performed before validation.
67 public $info_attr_transform_pre = array();
70 * List of HTMLPurifier_AttrTransform to be performed after validation.
72 public $info_attr_transform_post = array();
75 * List of HTMLPurifier_Injector to be performed during well-formedness fixing.
76 * An injector will only be invoked if all of it's pre-requisites are met;
77 * if an injector fails setup, there will be no error; it will simply be
80 public $info_injector = array();
83 * Boolean flag that indicates whether or not getChildDef is implemented.
84 * For optimization reasons: may save a call to a function. Be sure
85 * to set it if you do implement getChildDef(), otherwise it will have
88 public $defines_child_def = false;
91 * Boolean flag whether or not this module is safe. If it is not safe, all
92 * of its members are unsafe. Modules are safe by default (this might be
93 * slightly dangerous, but it doesn't make much sense to force HTML Purifier,
94 * which is based off of safe HTML, to explicitly say, "This is safe," even
95 * though there are modules which are "unsafe")
97 * @note Previously, safety could be applied at an element level granularity.
98 * We've removed this ability, so in order to add "unsafe" elements
99 * or attributes, a dedicated module with this property set to false
105 * Retrieves a proper HTMLPurifier_ChildDef subclass based on
106 * content_model and content_model_type member variables of
107 * the HTMLPurifier_ElementDef class. There is a similar function
108 * in HTMLPurifier_HTMLDefinition.
109 * @param $def HTMLPurifier_ElementDef instance
110 * @return HTMLPurifier_ChildDef subclass
112 public function getChildDef($def) {return false;}
114 // -- Convenience -----------------------------------------------------
117 * Convenience function that sets up a new element
118 * @param $element Name of element to add
119 * @param $type What content set should element be registered to?
120 * Set as false to skip this step.
121 * @param $contents Allowed children in form of:
122 * "$content_model_type: $content_model"
123 * @param $attr_includes What attribute collections to register to
125 * @param $attr What unique attributes does the element define?
126 * @note See ElementDef for in-depth descriptions of these parameters.
127 * @return Created element definition object, so you
128 * can set advanced parameters
130 public function addElement($element, $type, $contents, $attr_includes = array(), $attr = array()) {
131 $this->elements[] = $element;
132 // parse content_model
133 list($content_model_type, $content_model) = $this->parseContents($contents);
134 // merge in attribute inclusions
135 $this->mergeInAttrIncludes($attr, $attr_includes);
136 // add element to content sets
137 if ($type) $this->addElementToContentSet($element, $type);
139 $this->info[$element] = HTMLPurifier_ElementDef::create(
140 $content_model, $content_model_type, $attr
142 // literal object $contents means direct child manipulation
143 if (!is_string($contents)) $this->info[$element]->child = $contents;
144 return $this->info[$element];
148 * Convenience function that creates a totally blank, non-standalone
150 * @param $element Name of element to create
151 * @return Created element
153 public function addBlankElement($element) {
154 if (!isset($this->info[$element])) {
155 $this->elements[] = $element;
156 $this->info[$element] = new HTMLPurifier_ElementDef();
157 $this->info[$element]->standalone = false;
159 trigger_error("Definition for $element already exists in module, cannot redefine");
161 return $this->info[$element];
165 * Convenience function that registers an element to a content set
166 * @param Element to register
167 * @param Name content set (warning: case sensitive, usually upper-case
170 public function addElementToContentSet($element, $type) {
171 if (!isset($this->content_sets[$type])) $this->content_sets[$type] = '';
172 else $this->content_sets[$type] .= ' | ';
173 $this->content_sets[$type] .= $element;
177 * Convenience function that transforms single-string contents
178 * into separate content model and content model type
179 * @param $contents Allowed children in form of:
180 * "$content_model_type: $content_model"
181 * @note If contents is an object, an array of two nulls will be
182 * returned, and the callee needs to take the original $contents
183 * and use it directly.
185 public function parseContents($contents) {
186 if (!is_string($contents)) return array(null, null); // defer
188 // check for shorthand content model forms
190 return array('empty', '');
192 return array('optional', 'Inline | #PCDATA');
194 return array('optional', 'Flow | #PCDATA');
196 list($content_model_type, $content_model) = explode(':', $contents);
197 $content_model_type = strtolower(trim($content_model_type));
198 $content_model = trim($content_model);
199 return array($content_model_type, $content_model);
203 * Convenience function that merges a list of attribute includes into
204 * an attribute array.
205 * @param $attr Reference to attr array to modify
206 * @param $attr_includes Array of includes / string include to merge in
208 public function mergeInAttrIncludes(&$attr, $attr_includes) {
209 if (!is_array($attr_includes)) {
210 if (empty($attr_includes)) $attr_includes = array();
211 else $attr_includes = array($attr_includes);
213 $attr[0] = $attr_includes;
217 * Convenience function that generates a lookup table with boolean
219 * @param $list List of values to turn into a lookup
220 * @note You can also pass an arbitrary number of arguments in
221 * place of the regular argument
222 * @return Lookup array equivalent of list
224 public function makeLookup($list) {
225 if (is_string($list)) $list = func_get_args();
227 foreach ($list as $value) {
228 if (is_null($value)) continue;
235 * Lazy load construction of the module after determining whether
236 * or not it's needed, and also when a finalized configuration object
238 * @param $config Instance of HTMLPurifier_Config
240 public function setup($config) {}
244 // vim: et sw=4 sts=4