]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php
Merge pull request #482 from pixelroot/master
[friendica.git] / library / HTMLPurifier / Injector / RemoveSpansWithoutAttributes.php
1 <?php
2
3 /**
4  * Injector that removes spans with no attributes
5  */
6 class HTMLPurifier_Injector_RemoveSpansWithoutAttributes extends HTMLPurifier_Injector
7 {
8     public $name = 'RemoveSpansWithoutAttributes';
9     public $needed = array('span');
10
11     private $attrValidator;
12
13     /**
14      * Used by AttrValidator
15      */
16     private $config;
17     private $context;
18
19     public function prepare($config, $context) {
20         $this->attrValidator = new HTMLPurifier_AttrValidator();
21         $this->config = $config;
22         $this->context = $context;
23         return parent::prepare($config, $context);
24     }
25
26     public function handleElement(&$token) {
27         if ($token->name !== 'span' || !$token instanceof HTMLPurifier_Token_Start) {
28             return;
29         }
30
31         // We need to validate the attributes now since this doesn't normally
32         // happen until after MakeWellFormed. If all the attributes are removed
33         // the span needs to be removed too.
34         $this->attrValidator->validateToken($token, $this->config, $this->context);
35         $token->armor['ValidateAttributes'] = true;
36
37         if (!empty($token->attr)) {
38             return;
39         }
40
41         $nesting = 0;
42         $spanContentTokens = array();
43         while ($this->forwardUntilEndToken($i, $current, $nesting)) {}
44
45         if ($current instanceof HTMLPurifier_Token_End && $current->name === 'span') {
46             // Mark closing span tag for deletion
47             $current->markForDeletion = true;
48             // Delete open span tag
49             $token = false;
50         }
51     }
52
53     public function handleEnd(&$token) {
54         if ($token->markForDeletion) {
55             $token = false;
56         }
57     }
58 }
59
60 // vim: et sw=4 sts=4