ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
Injector.php
Go to the documentation of this file.
1 <?php
2 
16 abstract class HTMLPurifier_Injector
17 {
18 
22  public $name;
23 
27  protected $htmlDefinition;
28 
33  protected $currentNesting;
34 
39  protected $inputTokens;
40 
46  protected $inputIndex;
47 
53  public $needed = array();
54 
58  protected $rewind = false;
59 
68  public function rewind($index) {
69  $this->rewind = $index;
70  }
71 
75  public function getRewind() {
76  $r = $this->rewind;
77  $this->rewind = false;
78  return $r;
79  }
80 
90  public function prepare($config, $context) {
91  $this->htmlDefinition = $config->getHTMLDefinition();
92  // Even though this might fail, some unit tests ignore this and
93  // still test checkNeeded, so be careful. Maybe get rid of that
94  // dependency.
95  $result = $this->checkNeeded($config);
96  if ($result !== false) return $result;
97  $this->currentNesting =& $context->get('CurrentNesting');
98  $this->inputTokens =& $context->get('InputTokens');
99  $this->inputIndex =& $context->get('InputIndex');
100  return false;
101  }
102 
111  public function checkNeeded($config) {
112  $def = $config->getHTMLDefinition();
113  foreach ($this->needed as $element => $attributes) {
114  if (is_int($element)) $element = $attributes;
115  if (!isset($def->info[$element])) return $element;
116  if (!is_array($attributes)) continue;
117  foreach ($attributes as $name) {
118  if (!isset($def->info[$element]->attr[$name])) return "$element.$name";
119  }
120  }
121  return false;
122  }
123 
129  public function allowsElement($name) {
130  if (!empty($this->currentNesting)) {
131  $parent_token = array_pop($this->currentNesting);
132  $this->currentNesting[] = $parent_token;
133  $parent = $this->htmlDefinition->info[$parent_token->name];
134  } else {
135  $parent = $this->htmlDefinition->info_parent_def;
136  }
137  if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
138  return false;
139  }
140  // check for exclusion
141  for ($i = count($this->currentNesting) - 2; $i >= 0; $i--) {
142  $node = $this->currentNesting[$i];
143  $def = $this->htmlDefinition->info[$node->name];
144  if (isset($def->excludes[$name])) return false;
145  }
146  return true;
147  }
148 
157  protected function forward(&$i, &$current) {
158  if ($i === null) $i = $this->inputIndex + 1;
159  else $i++;
160  if (!isset($this->inputTokens[$i])) return false;
161  $current = $this->inputTokens[$i];
162  return true;
163  }
164 
170  protected function forwardUntilEndToken(&$i, &$current, &$nesting) {
171  $result = $this->forward($i, $current);
172  if (!$result) return false;
173  if ($nesting === null) $nesting = 0;
174  if ($current instanceof HTMLPurifier_Token_Start) $nesting++;
175  elseif ($current instanceof HTMLPurifier_Token_End) {
176  if ($nesting <= 0) return false;
177  $nesting--;
178  }
179  return true;
180  }
181 
190  protected function backward(&$i, &$current) {
191  if ($i === null) $i = $this->inputIndex - 1;
192  else $i--;
193  if ($i < 0) return false;
194  $current = $this->inputTokens[$i];
195  return true;
196  }
197 
207  protected function current(&$i, &$current) {
208  if ($i === null) $i = $this->inputIndex;
209  $current = $this->inputTokens[$i];
210  }
211 
215  public function handleText(&$token) {}
216 
220  public function handleElement(&$token) {}
221 
225  public function handleEnd(&$token) {
226  $this->notifyEnd($token);
227  }
228 
234  public function notifyEnd($token) {}
235 
236 
237 }
238 
239 // vim: et sw=4 sts=4