Debian Codegraph @

vendor/ezyang/htmlpurifier/library/HTMLPurifier/HTMLModule/Tidy.php

raw
1 <?php
2
3 /**
4 * Abstract class for a set of proprietary modules that clean up (tidy)
5 * poorly written HTML.
6 * @todo Figure out how to protect some of these methods/properties
7 */
8 class HTMLPurifier_HTMLModule_Tidy extends HTMLPurifier_HTMLModule
9 {
10 /**
11 * List of supported levels.
12 * Index zero is a special case "no fixes" level.
13 * @type array
14 */
15 public $levels = array(0 => 'none', 'light', 'medium', 'heavy');
16
17 /**
18 * Default level to place all fixes in.
19 * Disabled by default.
20 * @type string
21 */
22 public $defaultLevel = null;
23
24 /**
25 * Lists of fixes used by getFixesForLevel().
26 * Format is:
27 * HTMLModule_Tidy->fixesForLevel[$level] = array('fix-1', 'fix-2');
28 * @type array
29 */
30 public $fixesForLevel = array(
31 'light' => array(),
32 'medium' => array(),
33 'heavy' => array()
34 );
35
36 /**
37 * Lazy load constructs the module by determining the necessary
38 * fixes to create and then delegating to the populate() function.
39 * @param HTMLPurifier_Config $config
40 * @todo Wildcard matching and error reporting when an added or
41 * subtracted fix has no effect.
42 */
43 public function setup($config)
44 {
45 // create fixes, initialize fixesForLevel
46 $fixes = $this->makeFixes();
47 $this->makeFixesForLevel($fixes);
48
49 // figure out which fixes to use
50 $level = $config->get('HTML.TidyLevel');
51 $fixes_lookup = $this->getFixesForLevel($level);
52
53 // get custom fix declarations: these need namespace processing
54 $add_fixes = $config->get('HTML.TidyAdd');
55 $remove_fixes = $config->get('HTML.TidyRemove');
56
57 foreach ($fixes as $name => $fix) {
58 // needs to be refactored a little to implement globbing
59 if (isset($remove_fixes[$name]) ||
60 (!isset($add_fixes[$name]) && !isset($fixes_lookup[$name]))) {
61 unset($fixes[$name]);
62 }
63 }
64
65 // populate this module with necessary fixes
66 $this->populate($fixes);
67 }
68
69 /**
70 * Retrieves all fixes per a level, returning fixes for that specific
71 * level as well as all levels below it.
72 * @param string $level level identifier, see $levels for valid values
73 * @return array Lookup up table of fixes
74 */
75 public function getFixesForLevel($level)
76 {
77 if ($level == $this->levels[0]) {
78 return array();
79 }
80 $activated_levels = array();
81 for ($i = 1, $c = count($this->levels); $i < $c; $i++) {
82 $activated_levels[] = $this->levels[$i];
83 if ($this->levels[$i] == $level) {
84 break;
85 }
86 }
87 if ($i == $c) {
88 trigger_error(
89 'Tidy level ' . htmlspecialchars($level) . ' not recognized',
90 E_USER_WARNING
91 );
92 return array();
93 }
94 $ret = array();
95 foreach ($activated_levels as $level) {
96 foreach ($this->fixesForLevel[$level] as $fix) {
97 $ret[$fix] = true;
98 }
99 }
100 return $ret;
101 }
102
103 /**
104 * Dynamically populates the $fixesForLevel member variable using
105 * the fixes array. It may be custom overloaded, used in conjunction
106 * with $defaultLevel, or not used at all.
107 * @param array $fixes
108 */
109 public function makeFixesForLevel($fixes)
110 {
111 if (!isset($this->defaultLevel)) {
112 return;
113 }
114 if (!isset($this->fixesForLevel[$this->defaultLevel])) {
115 throw new Exception(
116 'Default level ' . $this->defaultLevel . ' does not exist'
117 );
118 return;
119 }
120 $this->fixesForLevel[$this->defaultLevel] = array_keys($fixes);
121 }
122
123 /**
124 * Populates the module with transforms and other special-case code
125 * based on a list of fixes passed to it
126 * @param array $fixes Lookup table of fixes to activate
127 */
128 public function populate($fixes)
129 {
130 foreach ($fixes as $name => $fix) {
131 // determine what the fix is for
132 list($type, $params) = $this->getFixType($name);
133 switch ($type) {
134 case 'attr_transform_pre':
135 case 'attr_transform_post':
136 $attr = $params['attr'];
137 if (isset($params['element'])) {
138 $element = $params['element'];
139 if (empty($this->info[$element])) {
140 $e = $this->addBlankElement($element);
141 } else {
142 $e = $this->info[$element];
143 }
144 } else {
145 $type = "info_$type";
146 $e = $this;
147 }
148 $e->{$type}[$attr] = $fix;
149 break;
150 case 'tag_transform':
151 $this->info_tag_transform[$params['element']] = $fix;
152 break;
153 case 'child':
154 case 'content_model_type':
155 $element = $params['element'];
156 if (empty($this->info[$element])) {
157 $e = $this->addBlankElement($element);
158 } else {
159 $e = $this->info[$element];
160 }
161 $e->$type = $fix;
162 break;
163 default:
164 throw new Exception("Fix type $type not supported");
165 }
166 }
167 }
168
169 /**
170 * Parses a fix name and determines what kind of fix it is, as well
171 * as other information defined by the fix
172 * @param $name String name of fix
173 * @return array(string $fix_type, array $fix_parameters)
174 * @note $fix_parameters is type dependant, see populate() for usage
175 * of these parameters
176 */
177 public function getFixType($name)
178 {
179 // parse it
180 $property = $attr = null;
181 if (strpos($name, '#') !== false) {
182 list($name, $property) = explode('#', $name);
183 }
184 if (strpos($name, '@') !== false) {
185 list($name, $attr) = explode('@', $name);
186 }
187
188 // figure out the parameters
189 $params = array();
190 if ($name !== '') {
191 $params['element'] = $name;
192 }
193 if (!is_null($attr)) {
194 $params['attr'] = $attr;
195 }
196
197 // special case: attribute transform
198 if (!is_null($attr)) {
199 if (is_null($property)) {
200 $property = 'pre';
201 }
202 $type = 'attr_transform_' . $property;
203 return array($type, $params);
204 }
205
206 // special case: tag transform
207 if (is_null($property)) {
208 return array('tag_transform', $params);
209 }
210
211 return array($property, $params);
212
213 }
214
215 /**
216 * Defines all fixes the module will perform in a compact
217 * associative array of fix name to fix implementation.
218 * @return array
219 */
220 public function makeFixes()
221 {
222 return array();
223 }
224 }
225
226 // vim: et sw=4 sts=4