3 // common defs that we'll support by default
4 require_once 'HTMLPurifier/ChildDef.php';
5 require_once 'HTMLPurifier/ChildDef/Empty.php';
6 require_once 'HTMLPurifier/ChildDef/Required.php';
7 require_once 'HTMLPurifier/ChildDef/Optional.php';
9 class HTMLPurifier_ContentSets
13 * List of content set strings (pipe seperators) indexed by name.
19 * List of content set lookups (element => true) indexed by name.
20 * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
23 var $lookup = array();
26 * Synchronized list of defined content sets (keys of info)
30 * Synchronized list of defined content values (values of info)
32 var $values = array();
35 * Merges in module's content sets, expands identifiers in the content
36 * sets and populates the keys, values and lookup member variables.
37 * @param $modules List of HTMLPurifier_HTMLModule
39 function HTMLPurifier_ContentSets($modules) {
40 if (!is_array($modules)) $modules = array($modules);
41 // populate content_sets based on module hints
42 // sorry, no way of overloading
43 foreach ($modules as $module_i => $module) {
44 foreach ($module->content_sets
as $key => $value) {
45 if (isset($this->info
[$key])) {
46 // add it into the existing content set
47 $this->info
[$key] = $this->info
[$key] . ' | ' . $value;
49 $this->info
[$key] = $value;
53 // perform content_set expansions
54 $this->keys
= array_keys($this->info
);
55 foreach ($this->info
as $i => $set) {
56 // only performed once, so infinite recursion is not
61 // must be recalculated each time due to
62 // changing substitutions
63 array_values($this->info
),
66 $this->values
= array_values($this->info
);
68 // generate lookup tables
69 foreach ($this->info
as $name => $set) {
70 $this->lookup
[$name] = $this->convertToLookup($set);
75 * Accepts a definition; generates and assigns a ChildDef for it
76 * @param $def HTMLPurifier_ElementDef reference
77 * @param $module Module that defined the ElementDef
79 function generateChildDef(&$def, $module) {
80 if (!empty($def->child
)) return; // already done!
81 $content_model = $def->content_model
;
82 if (is_string($content_model)) {
83 $def->content_model
= str_replace(
84 $this->keys
, $this->values
, $content_model);
86 $def->child
= $this->getChildDef($def, $module);
90 * Instantiates a ChildDef based on content_model and content_model_type
91 * member variables in HTMLPurifier_ElementDef
92 * @note This will also defer to modules for custom HTMLPurifier_ChildDef
93 * subclasses that need content set expansion
94 * @param $def HTMLPurifier_ElementDef to have ChildDef extracted
95 * @return HTMLPurifier_ChildDef corresponding to ElementDef
97 function getChildDef($def, $module) {
98 $value = $def->content_model
;
99 if (is_object($value)) {
101 'Literal object child definitions should be stored in '.
102 'ElementDef->child not ElementDef->content_model',
107 switch ($def->content_model_type
) {
109 return new HTMLPurifier_ChildDef_Required($value);
111 return new HTMLPurifier_ChildDef_Optional($value);
113 return new HTMLPurifier_ChildDef_Empty();
115 return new HTMLPurifier_ChildDef_Custom($value);
117 // defer to its module
119 if ($module->defines_child_def
) { // save a func call
120 $return = $module->getChildDef($def);
122 if ($return !== false) return $return;
125 'Could not determine which ChildDef class to instantiate',
132 * Converts a string list of elements separated by pipes into
134 * @param $string List of elements
135 * @return Lookup array of elements
137 function convertToLookup($string) {
138 $array = explode('|', str_replace(' ', '', $string));
140 foreach ($array as $i => $k) {