Add full documentation. Implement deferred ChildDef to HTMLModule. Add missing attrib...
[htmlpurifier.git] / library / HTMLPurifier / AttrCollection.php
blob6d92ca1f33e0e800a9453ba22c5a9b10dff4e974
1 <?php
3 require_once 'HTMLPurifier/AttrTypes.php';
4 require_once 'HTMLPurifier/AttrDef/Lang.php';
6 /**
7 * Defines common attribute collections that modules reference
8 */
10 class HTMLPurifier_AttrCollection
13 /**
14 * Associative array of attribute collections, indexed by name
15 * @note Technically, the composition of these is more complicated,
16 * but we bypass it using our own excludes property
18 var $info = array(
19 'Core' => array(
20 0 => array('Style'),
21 // 'xml:space' => false,
22 'class' => 'NMTOKENS',
23 'id' => 'ID',
24 'title' => 'CDATA',
26 'I18N' => array(
27 'xml:lang' => false, // see constructor
28 'lang' => false, // see constructor
30 'Common' => array(
31 0 => array('Core', 'I18N')
35 /**
36 * Sets up direct objects not registered to HTMLPurifier_AttrTypes
38 function HTMLPurifier_AttrCollection() {
39 // setup direct objects
40 $this->info['I18N']['xml:lang'] =
41 $this->info['I18N']['lang'] = new HTMLPurifier_AttrDef_Lang();
44 /**
45 * Performs all expansions on internal data for use by other inclusions
46 * It also collects all attribute collection extensions from
47 * modules
48 * @param $attr_types HTMLPurifier_AttrTypes instance
49 * @param $modules Hash array of HTMLPurifier_HTMLModule members
51 function setup($attr_types, $modules) {
52 $info =& $this->info;
53 // load extensions from the modules
54 foreach ($modules as $module) {
55 foreach ($module->attr_collection as $coll_i => $coll) {
56 foreach ($coll as $attr_i => $attr) {
57 if ($attr_i === 0 && isset($info[$coll_i][$attr_i])) {
58 // merge in includes
59 $info[$coll_i][$attr_i] = array_merge(
60 $info[$coll_i][$attr_i], $attr);
61 continue;
63 $info[$coll_i][$attr_i] = $attr;
67 // perform internal expansions and inclusions
68 foreach ($info as $name => $attr) {
69 // merge attribute collections that include others
70 $this->performInclusions($info[$name]);
71 // replace string identifiers with actual attribute objects
72 $this->expandIdentifiers($info[$name], $attr_types);
76 /**
77 * Takes a reference to an attribute associative array and performs
78 * all inclusions specified by the zero index.
79 * @param &$attr Reference to attribute array
81 function performInclusions(&$attr) {
82 if (!isset($attr[0])) return;
83 $merge = $attr[0];
84 // loop through all the inclusions
85 for ($i = 0; isset($merge[$i]); $i++) {
86 // foreach attribute of the inclusion, copy it over
87 foreach ($this->info[$merge[$i]] as $key => $value) {
88 if (isset($attr[$key])) continue; // also catches more inclusions
89 $attr[$key] = $value;
91 if (isset($info[$merge[$i]][0])) {
92 // recursion
93 $merge = array_merge($merge, isset($info[$merge[$i]][0]));
96 unset($attr[0]);
99 /**
100 * Expands all string identifiers in an attribute array by replacing
101 * them with the appropriate values inside HTMLPurifier_AttrTypes
102 * @param &$attr Reference to attribute array
103 * @param $attr_types HTMLPurifier_AttrTypes instance
105 function expandIdentifiers(&$attr, $attr_types) {
106 foreach ($attr as $def_i => $def) {
107 if ($def_i === 0) continue;
108 if (!is_string($def)) continue;
109 if (isset($attr_types->info[$def])) {
110 $attr[$def_i] = $attr_types->info[$def];
111 } else {
112 unset($attr[$def_i]);