4 * Abstract class for a set of proprietary modules that clean up (tidy)
6 * @todo Figure out how to protect some of these methods/properties
8 class HTMLPurifier_HTMLModule_Tidy extends HTMLPurifier_HTMLModule
11 * List of supported levels.
12 * Index zero is a special case "no fixes" level.
15 public $levels = array(0 => 'none', 'light', 'medium', 'heavy');
18 * Default level to place all fixes in.
19 * Disabled by default.
22 public $defaultLevel = null;
25 * Lists of fixes used by getFixesForLevel().
27 * HTMLModule_Tidy->fixesForLevel[$level] = array('fix-1', 'fix-2');
30 public $fixesForLevel = array(
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.
43 public function setup($config)
45 // create fixes, initialize fixesForLevel
46 $fixes = $this->makeFixes();
47 $this->makeFixesForLevel($fixes);
49 // figure out which fixes to use
50 $level = $config->get('HTML.TidyLevel');
51 $fixes_lookup = $this->getFixesForLevel($level);
53 // get custom fix declarations: these need namespace processing
54 $add_fixes = $config->get('HTML.TidyAdd');
55 $remove_fixes = $config->get('HTML.TidyRemove');
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]))) {
65 // populate this module with necessary fixes
66 $this->populate($fixes);
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
75 public function getFixesForLevel($level)
77 if ($level == $this->levels[0]) {
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) {
89 'Tidy level ' . htmlspecialchars($level) . ' not recognized',
95 foreach ($activated_levels as $level) {
96 foreach ($this->fixesForLevel[$level] as $fix) {
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
109 public function makeFixesForLevel($fixes)
111 if (!isset($this->defaultLevel)) {
114 if (!isset($this->fixesForLevel[$this->defaultLevel])) {
116 'Default level ' . $this->defaultLevel . ' does not exist',
121 $this->fixesForLevel[$this->defaultLevel] = array_keys($fixes);
125 * Populates the module with transforms and other special-case code
126 * based on a list of fixes passed to it
127 * @param array $fixes Lookup table of fixes to activate
129 public function populate($fixes)
131 foreach ($fixes as $name => $fix) {
132 // determine what the fix is for
133 list($type, $params) = $this->getFixType($name);
135 case 'attr_transform_pre':
136 case 'attr_transform_post':
137 $attr = $params['attr'];
138 if (isset($params['element'])) {
139 $element = $params['element'];
140 if (empty($this->info[$element])) {
141 $e = $this->addBlankElement($element);
143 $e = $this->info[$element];
146 $type = "info_$type";
149 // PHP does some weird parsing when I do
150 // $e->$type[$attr], so I have to assign a ref.
154 case 'tag_transform':
155 $this->info_tag_transform[$params['element']] = $fix;
158 case 'content_model_type':
159 $element = $params['element'];
160 if (empty($this->info[$element])) {
161 $e = $this->addBlankElement($element);
163 $e = $this->info[$element];
168 trigger_error("Fix type $type not supported", E_USER_ERROR);
175 * Parses a fix name and determines what kind of fix it is, as well
176 * as other information defined by the fix
177 * @param $name String name of fix
178 * @return array(string $fix_type, array $fix_parameters)
179 * @note $fix_parameters is type dependant, see populate() for usage
180 * of these parameters
182 public function getFixType($name)
185 $property = $attr = null;
186 if (strpos($name, '#') !== false) {
187 list($name, $property) = explode('#', $name);
189 if (strpos($name, '@') !== false) {
190 list($name, $attr) = explode('@', $name);
193 // figure out the parameters
196 $params['element'] = $name;
198 if (!is_null($attr)) {
199 $params['attr'] = $attr;
202 // special case: attribute transform
203 if (!is_null($attr)) {
204 if (is_null($property)) {
207 $type = 'attr_transform_' . $property;
208 return array($type, $params);
211 // special case: tag transform
212 if (is_null($property)) {
213 return array('tag_transform', $params);
216 return array($property, $params);
221 * Defines all fixes the module will perform in a compact
222 * associative array of fix name to fix implementation.
225 public function makeFixes()
230 // vim: et sw=4 sts=4