mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-10 07:38:41 +00:00
700d5bcbfc
Injector rewind: Injectors can now use the method rewind() in order to move the input index backwards, so that they can reprocess tokens (other injectors are not affected by a rewind). This functionality was necessary to implement nested node removals in %AutoFormat.RemoveEmpty. End to start ref: To facilitate rewinding, HTMLPurifier_Token_End now maintains a reference called $start to the starting token for their node. %AutoFormat.RemoveEmpty removes empty nodes. Lots of people have requested it, so here is a partially effective implementation. Because it is implemented as an Injector, it's not possible for it to handle newly introduced empty nodes by later validators, specifically auto-closing and child validation. The Injector is only meant to be used on HTML-ish languages. Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
41 lines
1.5 KiB
PHP
41 lines
1.5 KiB
PHP
<?php
|
|
|
|
class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
|
|
{
|
|
|
|
private $context, $config;
|
|
|
|
public function prepare($config, $context) {
|
|
parent::prepare($config, $context);
|
|
$this->config = $config;
|
|
$this->context = $context;
|
|
$this->attrValidator = new HTMLPurifier_AttrValidator();
|
|
}
|
|
|
|
public function handleElement(&$token) {
|
|
if (!$token instanceof HTMLPurifier_Token_Start) return;
|
|
$next = false;
|
|
for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
|
|
$next = $this->inputTokens[$i];
|
|
if ($next instanceof HTMLPurifier_Token_Text && $next->is_whitespace) continue;
|
|
break;
|
|
}
|
|
if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
|
|
if ($token->name == 'colgroup') return;
|
|
$this->attrValidator->validateToken($token, $this->config, $this->context);
|
|
$token->armor['ValidateAttributes'] = true;
|
|
if (isset($token->attr['id']) || isset($token->attr['name'])) return;
|
|
$token = $i - $this->inputIndex + 1;
|
|
for ($b = $this->inputIndex - 1; $b > 0; $b--) {
|
|
$prev = $this->inputTokens[$b];
|
|
if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) continue;
|
|
break;
|
|
}
|
|
// This is safe because we removed the token that triggered this.
|
|
$this->rewind($b - 1);
|
|
return;
|
|
}
|
|
}
|
|
|
|
}
|