friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/Injector/RemoveSpansWithoutAttribute...

85 lines
2.0 KiB
PHP
Raw Normal View History

2010-09-08 23:14:17 -04:00
<?php
/**
* Injector that removes spans with no attributes
*/
class HTMLPurifier_Injector_RemoveSpansWithoutAttributes extends HTMLPurifier_Injector
{
2016-02-09 05:06:17 -05:00
/**
* @type string
*/
2010-09-08 23:14:17 -04:00
public $name = 'RemoveSpansWithoutAttributes';
2016-02-09 05:06:17 -05:00
/**
* @type array
*/
2010-09-08 23:14:17 -04:00
public $needed = array('span');
2016-02-09 05:06:17 -05:00
/**
* @type HTMLPurifier_AttrValidator
*/
2010-09-08 23:14:17 -04:00
private $attrValidator;
/**
2016-02-09 05:06:17 -05:00
* Used by AttrValidator.
* @type HTMLPurifier_Config
2010-09-08 23:14:17 -04:00
*/
private $config;
2016-02-09 05:06:17 -05:00
/**
* @type HTMLPurifier_Context
*/
2010-09-08 23:14:17 -04:00
private $context;
2016-02-09 05:06:17 -05:00
public function prepare($config, $context)
{
2010-09-08 23:14:17 -04:00
$this->attrValidator = new HTMLPurifier_AttrValidator();
$this->config = $config;
$this->context = $context;
return parent::prepare($config, $context);
}
2016-02-09 05:06:17 -05:00
/**
* @param HTMLPurifier_Token $token
*/
public function handleElement(&$token)
{
2010-09-08 23:14:17 -04:00
if ($token->name !== 'span' || !$token instanceof HTMLPurifier_Token_Start) {
return;
}
// We need to validate the attributes now since this doesn't normally
// happen until after MakeWellFormed. If all the attributes are removed
// the span needs to be removed too.
$this->attrValidator->validateToken($token, $this->config, $this->context);
$token->armor['ValidateAttributes'] = true;
if (!empty($token->attr)) {
return;
}
$nesting = 0;
2016-02-09 05:06:17 -05:00
while ($this->forwardUntilEndToken($i, $current, $nesting)) {
}
2010-09-08 23:14:17 -04:00
if ($current instanceof HTMLPurifier_Token_End && $current->name === 'span') {
// Mark closing span tag for deletion
$current->markForDeletion = true;
// Delete open span tag
$token = false;
}
}
2016-02-09 05:06:17 -05:00
/**
* @param HTMLPurifier_Token $token
*/
public function handleEnd(&$token)
{
2010-09-08 23:14:17 -04:00
if ($token->markForDeletion) {
$token = false;
}
}
}
// vim: et sw=4 sts=4