HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/Injector.php
Go to the documentation of this file.
00001 <?php
00002 
00016 abstract class HTMLPurifier_Injector
00017 {
00018 
00022     public $name;
00023 
00027     protected $htmlDefinition;
00028 
00033     protected $currentNesting;
00034 
00039     protected $inputTokens;
00040 
00046     protected $inputIndex;
00047 
00053     public $needed = array();
00054 
00058     protected $rewind = false;
00059 
00068     public function rewind($index) {
00069         $this->rewind = $index;
00070     }
00071 
00075     public function getRewind() {
00076         $r = $this->rewind;
00077         $this->rewind = false;
00078         return $r;
00079     }
00080 
00090     public function prepare($config, $context) {
00091         $this->htmlDefinition = $config->getHTMLDefinition();
00092         // Even though this might fail, some unit tests ignore this and
00093         // still test checkNeeded, so be careful. Maybe get rid of that
00094         // dependency.
00095         $result = $this->checkNeeded($config);
00096         if ($result !== false) return $result;
00097         $this->currentNesting =& $context->get('CurrentNesting');
00098         $this->inputTokens    =& $context->get('InputTokens');
00099         $this->inputIndex     =& $context->get('InputIndex');
00100         return false;
00101     }
00102 
00111     public function checkNeeded($config) {
00112         $def = $config->getHTMLDefinition();
00113         foreach ($this->needed as $element => $attributes) {
00114             if (is_int($element)) $element = $attributes;
00115             if (!isset($def->info[$element])) return $element;
00116             if (!is_array($attributes)) continue;
00117             foreach ($attributes as $name) {
00118                 if (!isset($def->info[$element]->attr[$name])) return "$element.$name";
00119             }
00120         }
00121         return false;
00122     }
00123 
00129     public function allowsElement($name) {
00130         if (!empty($this->currentNesting)) {
00131             $parent_token = array_pop($this->currentNesting);
00132             $this->currentNesting[] = $parent_token;
00133             $parent = $this->htmlDefinition->info[$parent_token->name];
00134         } else {
00135             $parent = $this->htmlDefinition->info_parent_def;
00136         }
00137         if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
00138             return false;
00139         }
00140         // check for exclusion
00141         for ($i = count($this->currentNesting) - 2; $i >= 0; $i--) {
00142             $node = $this->currentNesting[$i];
00143             $def  = $this->htmlDefinition->info[$node->name];
00144             if (isset($def->excludes[$name])) return false;
00145         }
00146         return true;
00147     }
00148 
00157     protected function forward(&$i, &$current) {
00158         if ($i === null) $i = $this->inputIndex + 1;
00159         else $i++;
00160         if (!isset($this->inputTokens[$i])) return false;
00161         $current = $this->inputTokens[$i];
00162         return true;
00163     }
00164 
00170     protected function forwardUntilEndToken(&$i, &$current, &$nesting) {
00171         $result = $this->forward($i, $current);
00172         if (!$result) return false;
00173         if ($nesting === null) $nesting = 0;
00174         if     ($current instanceof HTMLPurifier_Token_Start) $nesting++;
00175         elseif ($current instanceof HTMLPurifier_Token_End) {
00176             if ($nesting <= 0) return false;
00177             $nesting--;
00178         }
00179         return true;
00180     }
00181 
00190     protected function backward(&$i, &$current) {
00191         if ($i === null) $i = $this->inputIndex - 1;
00192         else $i--;
00193         if ($i < 0) return false;
00194         $current = $this->inputTokens[$i];
00195         return true;
00196     }
00197 
00207     protected function current(&$i, &$current) {
00208         if ($i === null) $i = $this->inputIndex;
00209         $current = $this->inputTokens[$i];
00210     }
00211 
00215     public function handleText(&$token) {}
00216 
00220     public function handleElement(&$token) {}
00221 
00225     public function handleEnd(&$token) {
00226         $this->notifyEnd($token);
00227     }
00228 
00234     public function notifyEnd($token) {}
00235 
00236 
00237 }
00238 
00239 // vim: et sw=4 sts=4