$name
$name
Advisory name of injector, this is for friendly error messages.
Injector that removes spans with no attributes
rewindOffset(bool|int $offset) : mixed
Rewind to a spot to re-perform processing. This is useful if you deleted a node, and now need to see if this change affected any earlier nodes. Rewinding does not affect other injectors, and can result in infinite loops if not used carefully.
bool|int | $offset |
prepare(mixed $config, mixed $context) : bool|string
Prepares the injector by giving it the config and context objects: this allows references to important variables to be made within the injector. This function also checks if the HTML environment will work with the Injector (see checkNeeded()).
mixed | $config | |
mixed | $context |
Boolean false if success, string of missing needed element/attribute if failure
checkNeeded(\HTMLPurifier_Config $config) : bool|string
This function checks if the HTML environment will work with the Injector: if p tags are not allowed, the Auto-Paragraphing injector should not be enabled.
\HTMLPurifier_Config | $config |
Boolean false if success, string of missing needed element/attribute if failure
forward(int $i, \HTMLPurifier_Token $current) : bool
Iterator function, which starts with the next token and continues until you reach the end of the input tokens.
int | $i | Current integer index variable for inputTokens |
\HTMLPurifier_Token | $current | Current token variable. Do NOT use $token, as that variable is also a reference |
warning |
Please prevent previous references from interfering with this functions by setting $i = null beforehand! |
---|
forwardUntilEndToken(int $i, \HTMLPurifier_Token $current, int $nesting) : bool
Similar to _forward, but accepts a third parameter $nesting (which should be initialized at 0) and stops when we hit the end tag for the node $this->inputIndex starts in.
int | $i | Current integer index variable for inputTokens |
\HTMLPurifier_Token | $current | Current token variable. Do NOT use $token, as that variable is also a reference |
int | $nesting |
None found |
backward(int $i, \HTMLPurifier_Token $current) : bool
Iterator function, starts with the previous token and continues until you reach the beginning of input tokens.
int | $i | Current integer index variable for inputTokens |
\HTMLPurifier_Token | $current | Current token variable. Do NOT use $token, as that variable is also a reference |
warning |
Please prevent previous references from interfering with this functions by setting $i = null beforehand! |
---|
<?php
/**
* Injector that removes spans with no attributes
*/
class HTMLPurifier_Injector_RemoveSpansWithoutAttributes extends HTMLPurifier_Injector
{
/**
* @type string
*/
public $name = 'RemoveSpansWithoutAttributes';
/**
* @type array
*/
public $needed = array('span');
/**
* @type HTMLPurifier_AttrValidator
*/
private $attrValidator;
/**
* Used by AttrValidator.
* @type HTMLPurifier_Config
*/
private $config;
/**
* @type HTMLPurifier_Context
*/
private $context;
public function prepare($config, $context)
{
$this->attrValidator = new HTMLPurifier_AttrValidator();
$this->config = $config;
$this->context = $context;
return parent::prepare($config, $context);
}
/**
* @param HTMLPurifier_Token $token
*/
public function handleElement(&$token)
{
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;
while ($this->forwardUntilEndToken($i, $current, $nesting)) {
}
if ($current instanceof HTMLPurifier_Token_End && $current->name === 'span') {
// Mark closing span tag for deletion
$current->markForDeletion = true;
// Delete open span tag
$token = false;
}
}
/**
* @param HTMLPurifier_Token $token
*/
public function handleEnd(&$token)
{
if ($token->markForDeletion) {
$token = false;
}
}
}
// vim: et sw=4 sts=4