HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/Injector/RemoveEmpty.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
00004 {
00005 
00006     private $context, $config, $attrValidator, $removeNbsp, $removeNbspExceptions;
00007 
00008     public function prepare($config, $context) {
00009         parent::prepare($config, $context);
00010         $this->config = $config;
00011         $this->context = $context;
00012         $this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');
00013         $this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');
00014         $this->attrValidator = new HTMLPurifier_AttrValidator();
00015     }
00016 
00017     public function handleElement(&$token) {
00018         if (!$token instanceof HTMLPurifier_Token_Start) return;
00019         $next = false;
00020         for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
00021             $next = $this->inputTokens[$i];
00022             if ($next instanceof HTMLPurifier_Token_Text) {
00023                 if ($next->is_whitespace) continue;
00024                 if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
00025                     $plain = str_replace("\xC2\xA0", "", $next->data);
00026                     $isWsOrNbsp = $plain === '' || ctype_space($plain);
00027                     if ($isWsOrNbsp) continue;
00028                 }
00029             }
00030             break;
00031         }
00032         if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
00033             if ($token->name == 'colgroup') return;
00034             $this->attrValidator->validateToken($token, $this->config, $this->context);
00035             $token->armor['ValidateAttributes'] = true;
00036             if (isset($token->attr['id']) || isset($token->attr['name'])) return;
00037             $token = $i - $this->inputIndex + 1;
00038             for ($b = $this->inputIndex - 1; $b > 0; $b--) {
00039                 $prev = $this->inputTokens[$b];
00040                 if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) continue;
00041                 break;
00042             }
00043             // This is safe because we removed the token that triggered this.
00044             $this->rewind($b - 1);
00045             return;
00046         }
00047     }
00048 
00049 }
00050 
00051 // vim: et sw=4 sts=4