4 * Defines common attribute collections that modules reference
7 class HTMLPurifier_AttrCollections
11 * Associative array of attribute collections, indexed by name.
14 public $info = array();
17 * Performs all expansions on internal data for use by other inclusions
18 * It also collects all attribute collection extensions from
20 * @param HTMLPurifier_AttrTypes $attr_types HTMLPurifier_AttrTypes instance
21 * @param HTMLPurifier_HTMLModule[] $modules Hash array of HTMLPurifier_HTMLModule members
23 public function __construct($attr_types, $modules)
25 // load extensions from the modules
26 foreach ($modules as $module) {
27 foreach ($module->attr_collections
as $coll_i => $coll) {
28 if (!isset($this->info
[$coll_i])) {
29 $this->info
[$coll_i] = array();
31 foreach ($coll as $attr_i => $attr) {
32 if ($attr_i === 0 && isset($this->info
[$coll_i][$attr_i])) {
34 $this->info
[$coll_i][$attr_i] = array_merge(
35 $this->info
[$coll_i][$attr_i],
40 $this->info
[$coll_i][$attr_i] = $attr;
44 // perform internal expansions and inclusions
45 foreach ($this->info
as $name => $attr) {
46 // merge attribute collections that include others
47 $this->performInclusions($this->info
[$name]);
48 // replace string identifiers with actual attribute objects
49 $this->expandIdentifiers($this->info
[$name], $attr_types);
54 * Takes a reference to an attribute associative array and performs
55 * all inclusions specified by the zero index.
56 * @param array &$attr Reference to attribute array
58 public function performInclusions(&$attr)
60 if (!isset($attr[0])) {
64 $seen = array(); // recursion guard
65 // loop through all the inclusions
66 for ($i = 0; isset($merge[$i]); $i++
) {
67 if (isset($seen[$merge[$i]])) {
70 $seen[$merge[$i]] = true;
71 // foreach attribute of the inclusion, copy it over
72 if (!isset($this->info
[$merge[$i]])) {
75 foreach ($this->info
[$merge[$i]] as $key => $value) {
76 if (isset($attr[$key])) {
78 } // also catches more inclusions
81 if (isset($this->info
[$merge[$i]][0])) {
83 $merge = array_merge($merge, $this->info
[$merge[$i]][0]);
90 * Expands all string identifiers in an attribute array by replacing
91 * them with the appropriate values inside HTMLPurifier_AttrTypes
92 * @param array &$attr Reference to attribute array
93 * @param HTMLPurifier_AttrTypes $attr_types HTMLPurifier_AttrTypes instance
95 public function expandIdentifiers(&$attr, $attr_types)
97 // because foreach will process new elements we add, make sure we
101 foreach ($attr as $def_i => $def) {
107 if (isset($processed[$def_i])) {
111 // determine whether or not attribute is required
112 if ($required = (strpos($def_i, '*') !== false)) {
113 // rename the definition
114 unset($attr[$def_i]);
115 $def_i = trim($def_i, '*');
116 $attr[$def_i] = $def;
119 $processed[$def_i] = true;
121 // if we've already got a literal object, move on
122 if (is_object($def)) {
123 // preserve previous required
124 $attr[$def_i]->required
= ($required ||
$attr[$def_i]->required
);
128 if ($def === false) {
129 unset($attr[$def_i]);
133 if ($t = $attr_types->get($def)) {
135 $attr[$def_i]->required
= $required;
137 unset($attr[$def_i]);
143 // vim: et sw=4 sts=4