2008-06-27 20:09:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
|
|
|
|
{
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type HTMLPurifier_Context
|
|
|
|
*/
|
|
|
|
private $context;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type HTMLPurifier_Config
|
|
|
|
*/
|
|
|
|
private $config;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type HTMLPurifier_AttrValidator
|
|
|
|
*/
|
|
|
|
private $attrValidator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type bool
|
|
|
|
*/
|
|
|
|
private $removeNbsp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type bool
|
|
|
|
*/
|
|
|
|
private $removeNbspExceptions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type array
|
|
|
|
* TODO: make me configurable
|
|
|
|
*/
|
2012-10-27 09:32:48 +00:00
|
|
|
private $_exclude = array('colgroup' => 1, 'th' => 1, 'td' => 1, 'iframe' => 1);
|
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function prepare($config, $context)
|
|
|
|
{
|
2008-06-27 20:09:14 +00:00
|
|
|
parent::prepare($config, $context);
|
|
|
|
$this->config = $config;
|
|
|
|
$this->context = $context;
|
2009-04-09 04:53:19 +00:00
|
|
|
$this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');
|
|
|
|
$this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');
|
2008-06-27 20:09:14 +00:00
|
|
|
$this->attrValidator = new HTMLPurifier_AttrValidator();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_Token $token
|
|
|
|
*/
|
|
|
|
public function handleElement(&$token)
|
|
|
|
{
|
|
|
|
if (!$token instanceof HTMLPurifier_Token_Start) {
|
|
|
|
return;
|
|
|
|
}
|
2008-06-27 20:09:14 +00:00
|
|
|
$next = false;
|
2013-10-13 19:53:51 +00:00
|
|
|
$deleted = 1; // the current tag
|
|
|
|
for ($i = count($this->inputZipper->back) - 1; $i >= 0; $i--, $deleted++) {
|
|
|
|
$next = $this->inputZipper->back[$i];
|
2009-04-09 04:53:19 +00:00
|
|
|
if ($next instanceof HTMLPurifier_Token_Text) {
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($next->is_whitespace) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-09 04:53:19 +00:00
|
|
|
if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
|
|
|
|
$plain = str_replace("\xC2\xA0", "", $next->data);
|
|
|
|
$isWsOrNbsp = $plain === '' || ctype_space($plain);
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($isWsOrNbsp) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-09 04:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-27 20:09:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
|
2013-07-16 11:56:14 +00:00
|
|
|
if (isset($this->_exclude[$token->name])) {
|
|
|
|
return;
|
|
|
|
}
|
2008-06-27 20:09:14 +00:00
|
|
|
$this->attrValidator->validateToken($token, $this->config, $this->context);
|
|
|
|
$token->armor['ValidateAttributes'] = true;
|
2013-07-16 11:56:14 +00:00
|
|
|
if (isset($token->attr['id']) || isset($token->attr['name'])) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-13 19:53:51 +00:00
|
|
|
$token = $deleted + 1;
|
|
|
|
for ($b = 0, $c = count($this->inputZipper->front); $b < $c; $b++) {
|
|
|
|
$prev = $this->inputZipper->front[$b];
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-06-27 20:09:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// This is safe because we removed the token that triggered this.
|
2013-10-13 19:53:51 +00:00
|
|
|
$this->rewindOffset($b+$deleted);
|
2008-06-27 20:09:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|