Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / ContentSets.php
blobde5c532e1837f9d92f2297804c8ca59a3be601a4
1 <?php
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
12 /**
13 * List of content set strings (pipe seperators) indexed by name.
14 * @public
16 var $info = array();
18 /**
19 * List of content set lookups (element => true) indexed by name.
20 * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
21 * @public
23 var $lookup = array();
25 /**
26 * Synchronized list of defined content sets (keys of info)
28 var $keys = array();
29 /**
30 * Synchronized list of defined content values (values of info)
32 var $values = array();
34 /**
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;
48 } else {
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
57 // a problem
58 $this->info[$i] =
59 str_replace(
60 $this->keys,
61 // must be recalculated each time due to
62 // changing substitutions
63 array_values($this->info),
64 $set);
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);
74 /**
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);
89 /**
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)) {
100 trigger_error(
101 'Literal object child definitions should be stored in '.
102 'ElementDef->child not ElementDef->content_model',
103 E_USER_NOTICE
105 return $value;
107 switch ($def->content_model_type) {
108 case 'required':
109 return new HTMLPurifier_ChildDef_Required($value);
110 case 'optional':
111 return new HTMLPurifier_ChildDef_Optional($value);
112 case 'empty':
113 return new HTMLPurifier_ChildDef_Empty();
114 case 'custom':
115 return new HTMLPurifier_ChildDef_Custom($value);
117 // defer to its module
118 $return = false;
119 if ($module->defines_child_def) { // save a func call
120 $return = $module->getChildDef($def);
122 if ($return !== false) return $return;
123 // error-out
124 trigger_error(
125 'Could not determine which ChildDef class to instantiate',
126 E_USER_ERROR
128 return false;
132 * Converts a string list of elements separated by pipes into
133 * a lookup array.
134 * @param $string List of elements
135 * @return Lookup array of elements
137 function convertToLookup($string) {
138 $array = explode('|', str_replace(' ', '', $string));
139 $ret = array();
140 foreach ($array as $i => $k) {
141 $ret[$k] = true;
143 return $ret;