]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/htmlpurifier/HTMLPurifier/AttrCollections.php
Merge pull request #481 from wallabag/dev
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / AttrCollections.php
CommitLineData
d4949327
NL
1<?php\r
2\r
3/**\r
4 * Defines common attribute collections that modules reference\r
5 */\r
6\r
7class HTMLPurifier_AttrCollections\r
8{\r
9\r
10 /**\r
11 * Associative array of attribute collections, indexed by name.\r
12 * @type array\r
13 */\r
14 public $info = array();\r
15\r
16 /**\r
17 * Performs all expansions on internal data for use by other inclusions\r
18 * It also collects all attribute collection extensions from\r
19 * modules\r
20 * @param HTMLPurifier_AttrTypes $attr_types HTMLPurifier_AttrTypes instance\r
21 * @param HTMLPurifier_HTMLModule[] $modules Hash array of HTMLPurifier_HTMLModule members\r
22 */\r
23 public function __construct($attr_types, $modules)\r
24 {\r
25 // load extensions from the modules\r
26 foreach ($modules as $module) {\r
27 foreach ($module->attr_collections as $coll_i => $coll) {\r
28 if (!isset($this->info[$coll_i])) {\r
29 $this->info[$coll_i] = array();\r
30 }\r
31 foreach ($coll as $attr_i => $attr) {\r
32 if ($attr_i === 0 && isset($this->info[$coll_i][$attr_i])) {\r
33 // merge in includes\r
34 $this->info[$coll_i][$attr_i] = array_merge(\r
35 $this->info[$coll_i][$attr_i],\r
36 $attr\r
37 );\r
38 continue;\r
39 }\r
40 $this->info[$coll_i][$attr_i] = $attr;\r
41 }\r
42 }\r
43 }\r
44 // perform internal expansions and inclusions\r
45 foreach ($this->info as $name => $attr) {\r
46 // merge attribute collections that include others\r
47 $this->performInclusions($this->info[$name]);\r
48 // replace string identifiers with actual attribute objects\r
49 $this->expandIdentifiers($this->info[$name], $attr_types);\r
50 }\r
51 }\r
52\r
53 /**\r
54 * Takes a reference to an attribute associative array and performs\r
55 * all inclusions specified by the zero index.\r
56 * @param array &$attr Reference to attribute array\r
57 */\r
58 public function performInclusions(&$attr)\r
59 {\r
60 if (!isset($attr[0])) {\r
61 return;\r
62 }\r
63 $merge = $attr[0];\r
64 $seen = array(); // recursion guard\r
65 // loop through all the inclusions\r
66 for ($i = 0; isset($merge[$i]); $i++) {\r
67 if (isset($seen[$merge[$i]])) {\r
68 continue;\r
69 }\r
70 $seen[$merge[$i]] = true;\r
71 // foreach attribute of the inclusion, copy it over\r
72 if (!isset($this->info[$merge[$i]])) {\r
73 continue;\r
74 }\r
75 foreach ($this->info[$merge[$i]] as $key => $value) {\r
76 if (isset($attr[$key])) {\r
77 continue;\r
78 } // also catches more inclusions\r
79 $attr[$key] = $value;\r
80 }\r
81 if (isset($this->info[$merge[$i]][0])) {\r
82 // recursion\r
83 $merge = array_merge($merge, $this->info[$merge[$i]][0]);\r
84 }\r
85 }\r
86 unset($attr[0]);\r
87 }\r
88\r
89 /**\r
90 * Expands all string identifiers in an attribute array by replacing\r
91 * them with the appropriate values inside HTMLPurifier_AttrTypes\r
92 * @param array &$attr Reference to attribute array\r
93 * @param HTMLPurifier_AttrTypes $attr_types HTMLPurifier_AttrTypes instance\r
94 */\r
95 public function expandIdentifiers(&$attr, $attr_types)\r
96 {\r
97 // because foreach will process new elements we add, make sure we\r
98 // skip duplicates\r
99 $processed = array();\r
100\r
101 foreach ($attr as $def_i => $def) {\r
102 // skip inclusions\r
103 if ($def_i === 0) {\r
104 continue;\r
105 }\r
106\r
107 if (isset($processed[$def_i])) {\r
108 continue;\r
109 }\r
110\r
111 // determine whether or not attribute is required\r
112 if ($required = (strpos($def_i, '*') !== false)) {\r
113 // rename the definition\r
114 unset($attr[$def_i]);\r
115 $def_i = trim($def_i, '*');\r
116 $attr[$def_i] = $def;\r
117 }\r
118\r
119 $processed[$def_i] = true;\r
120\r
121 // if we've already got a literal object, move on\r
122 if (is_object($def)) {\r
123 // preserve previous required\r
124 $attr[$def_i]->required = ($required || $attr[$def_i]->required);\r
125 continue;\r
126 }\r
127\r
128 if ($def === false) {\r
129 unset($attr[$def_i]);\r
130 continue;\r
131 }\r
132\r
133 if ($t = $attr_types->get($def)) {\r
134 $attr[$def_i] = $t;\r
135 $attr[$def_i]->required = $required;\r
136 } else {\r
137 unset($attr[$def_i]);\r
138 }\r
139 }\r
140 }\r
141}\r
142\r
143// vim: et sw=4 sts=4\r