File indexing completed on 2024-05-12 06:02:05

0001 <?php
0002 
0003 /**
0004  * Injector that removes spans with no attributes
0005  */
0006 class HTMLPurifier_Injector_RemoveSpansWithoutAttributes extends HTMLPurifier_Injector
0007 {
0008     /**
0009      * @type string
0010      */
0011     public $name = 'RemoveSpansWithoutAttributes';
0012 
0013     /**
0014      * @type array
0015      */
0016     public $needed = array('span');
0017 
0018     /**
0019      * @type HTMLPurifier_AttrValidator
0020      */
0021     private $attrValidator;
0022 
0023     /**
0024      * Used by AttrValidator.
0025      * @type HTMLPurifier_Config
0026      */
0027     private $config;
0028 
0029     /**
0030      * @type HTMLPurifier_Context
0031      */
0032     private $context;
0033 
0034     public function prepare($config, $context)
0035     {
0036         $this->attrValidator = new HTMLPurifier_AttrValidator();
0037         $this->config = $config;
0038         $this->context = $context;
0039         return parent::prepare($config, $context);
0040     }
0041 
0042     /**
0043      * @param HTMLPurifier_Token $token
0044      */
0045     public function handleElement(&$token)
0046     {
0047         if ($token->name !== 'span' || !$token instanceof HTMLPurifier_Token_Start) {
0048             return;
0049         }
0050 
0051         // We need to validate the attributes now since this doesn't normally
0052         // happen until after MakeWellFormed. If all the attributes are removed
0053         // the span needs to be removed too.
0054         $this->attrValidator->validateToken($token, $this->config, $this->context);
0055         $token->armor['ValidateAttributes'] = true;
0056 
0057         if (!empty($token->attr)) {
0058             return;
0059         }
0060 
0061         $nesting = 0;
0062         while ($this->forwardUntilEndToken($i, $current, $nesting)) {
0063         }
0064 
0065         if ($current instanceof HTMLPurifier_Token_End && $current->name === 'span') {
0066             // Mark closing span tag for deletion
0067             $current->markForDeletion = true;
0068             // Delete open span tag
0069             $token = false;
0070         }
0071     }
0072 
0073     /**
0074      * @param HTMLPurifier_Token $token
0075      */
0076     public function handleEnd(&$token)
0077     {
0078         if ($token->markForDeletion) {
0079             $token = false;
0080         }
0081     }
0082 }
0083 
0084 // vim: et sw=4 sts=4