ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Tidy.php
Go to the documentation of this file.
1<?php
2
9{
15 public $levels = array(0 => 'none', 'light', 'medium', 'heavy');
16
22 public $defaultLevel = null;
23
30 public $fixesForLevel = array(
31 'light' => array(),
32 'medium' => array(),
33 'heavy' => array()
34 );
35
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
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
109 public function makeFixesForLevel($fixes)
110 {
111 if (!isset($this->defaultLevel)) {
112 return;
113 }
114 if (!isset($this->fixesForLevel[$this->defaultLevel])) {
115 trigger_error(
116 'Default level ' . $this->defaultLevel . ' does not exist',
117 E_USER_ERROR
118 );
119 return;
120 }
121 $this->fixesForLevel[$this->defaultLevel] = array_keys($fixes);
122 }
123
129 public function populate($fixes)
130 {
131 foreach ($fixes as $name => $fix) {
132 // determine what the fix is for
133 list($type, $params) = $this->getFixType($name);
134 switch ($type) {
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);
142 } else {
143 $e = $this->info[$element];
144 }
145 } else {
146 $type = "info_$type";
147 $e = $this;
148 }
149 // PHP does some weird parsing when I do
150 // $e->$type[$attr], so I have to assign a ref.
151 $f =& $e->$type;
152 $f[$attr] = $fix;
153 break;
154 case 'tag_transform':
155 $this->info_tag_transform[$params['element']] = $fix;
156 break;
157 case 'child':
158 case 'content_model_type':
159 $element = $params['element'];
160 if (empty($this->info[$element])) {
161 $e = $this->addBlankElement($element);
162 } else {
163 $e = $this->info[$element];
164 }
165 $e->$type = $fix;
166 break;
167 default:
168 trigger_error("Fix type $type not supported", E_USER_ERROR);
169 break;
170 }
171 }
172 }
173
182 public function getFixType($name)
183 {
184 // parse it
185 $property = $attr = null;
186 if (strpos($name, '#') !== false) {
187 list($name, $property) = explode('#', $name);
188 }
189 if (strpos($name, '@') !== false) {
190 list($name, $attr) = explode('@', $name);
191 }
192
193 // figure out the parameters
194 $params = array();
195 if ($name !== '') {
196 $params['element'] = $name;
197 }
198 if (!is_null($attr)) {
199 $params['attr'] = $attr;
200 }
201
202 // special case: attribute transform
203 if (!is_null($attr)) {
204 if (is_null($property)) {
205 $property = 'pre';
206 }
207 $type = 'attr_transform_' . $property;
208 return array($type, $params);
209 }
210
211 // special case: tag transform
212 if (is_null($property)) {
213 return array('tag_transform', $params);
214 }
215
216 return array($property, $params);
217
218 }
219
225 public function makeFixes()
226 {
227 }
228}
229
230// vim: et sw=4 sts=4
Abstract class for a set of proprietary modules that clean up (tidy) poorly written HTML.
Definition: Tidy.php:9
$fixesForLevel
Lists of fixes used by getFixesForLevel().
Definition: Tidy.php:30
getFixType($name)
Parses a fix name and determines what kind of fix it is, as well as other information defined by the ...
Definition: Tidy.php:182
setup($config)
Lazy load constructs the module by determining the necessary fixes to create and then delegating to t...
Definition: Tidy.php:43
$defaultLevel
Default level to place all fixes in.
Definition: Tidy.php:22
getFixesForLevel($level)
Retrieves all fixes per a level, returning fixes for that specific level as well as all levels below ...
Definition: Tidy.php:75
populate($fixes)
Populates the module with transforms and other special-case code based on a list of fixes passed to i...
Definition: Tidy.php:129
$levels
List of supported levels.
Definition: Tidy.php:15
makeFixesForLevel($fixes)
Dynamically populates the $fixesForLevel member variable using the fixes array.
Definition: Tidy.php:109
makeFixes()
Defines all fixes the module will perform in a compact associative array of fix name to fix implement...
Definition: Tidy.php:225
Represents an XHTML 1.1 module, with information on elements, tags and attributes.
Definition: HTMLModule.php:19
$name
Short unique string identifier of the module.
Definition: HTMLModule.php:27
addBlankElement($element)
Convenience function that creates a totally blank, non-standalone element.
Definition: HTMLModule.php:174
$params
Definition: example_049.php:96