HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HTMLPurifier_Injector_RemoveSpansWithoutAttributes extends HTMLPurifier_Injector
00007 {
00008     public $name = 'RemoveSpansWithoutAttributes';
00009     public $needed = array('span');
00010 
00011     private $attrValidator;
00012 
00016     private $config;
00017     private $context;
00018 
00019     public function prepare($config, $context) {
00020         $this->attrValidator = new HTMLPurifier_AttrValidator();
00021         $this->config = $config;
00022         $this->context = $context;
00023         return parent::prepare($config, $context);
00024     }
00025 
00026     public function handleElement(&$token) {
00027         if ($token->name !== 'span' || !$token instanceof HTMLPurifier_Token_Start) {
00028             return;
00029         }
00030 
00031         // We need to validate the attributes now since this doesn't normally
00032         // happen until after MakeWellFormed. If all the attributes are removed
00033         // the span needs to be removed too.
00034         $this->attrValidator->validateToken($token, $this->config, $this->context);
00035         $token->armor['ValidateAttributes'] = true;
00036 
00037         if (!empty($token->attr)) {
00038             return;
00039         }
00040 
00041         $nesting = 0;
00042         $spanContentTokens = array();
00043         while ($this->forwardUntilEndToken($i, $current, $nesting)) {}
00044 
00045         if ($current instanceof HTMLPurifier_Token_End && $current->name === 'span') {
00046             // Mark closing span tag for deletion
00047             $current->markForDeletion = true;
00048             // Delete open span tag
00049             $token = false;
00050         }
00051     }
00052 
00053     public function handleEnd(&$token) {
00054         if ($token->markForDeletion) {
00055             $token = false;
00056         }
00057     }
00058 }
00059 
00060 // vim: et sw=4 sts=4