Skip to content

Commit

Permalink
Added option to disable HTML purifier
Browse files Browse the repository at this point in the history
Added option to disable HTML purifier
  • Loading branch information
SavageTiger authored Jan 25, 2021
2 parents e9b4386 + 2a84b0b commit e7628e3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ $config

// Pass an instance of \Doctrine\Common\Cache\Cache to cache the calculated diffs.
->setCacheProvider(null)

// Disable the HTML purifier (only do this if you known what you're doing)
// This bundle heavily relies on the purified input from ezyang/htmlpurifier
->setPurifierEnabled(true)

// Set the cache directory that HTMLPurifier should use.
->setPurifierCacheLocation(null)
Expand Down Expand Up @@ -192,7 +196,6 @@ php-htmldiff is available under [GNU General Public License, version 2][gnu]. Se
* Maybe add abstraction layer for cache + adapter for doctrine cache
* Make HTML Purifier an optional dependency - possibly use abstraction layer for purifiers so alternatives could be used (or none at all for performance)
* Expose configuration for HTML Purifier (used in table diffing) - currently only cache dir is configurable through HtmlDiffConfig object
* Add option to enable using HTML Purifier to purify all input
* Performance improvements (we have 1 benchmark test, we should probably get more)
* Algorithm improvements - trimming alike text at start and ends, store nested diff results in memory to re-use (like we do w/ caching)
* Benchmark using DOMDocument vs. alternatives vs. string parsing
Expand Down
10 changes: 9 additions & 1 deletion lib/Caxy/HtmlDiff/AbstractDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ abstract class AbstractDiff
protected $diffCaches = array();

/**
* @var \HTMLPurifier
* @var \HTMLPurifier|null
*/
protected $purifier;

Expand Down Expand Up @@ -154,6 +154,10 @@ public function initPurifier($defaultPurifierSerializerCache = null)
*/
protected function prepare()
{
if (false === $this->config->isPurifierEnabled()) {
return;
}

$this->initPurifier($this->config->getPurifierCacheLocation());

$this->oldText = $this->purifyHtml($this->oldText);
Expand Down Expand Up @@ -403,6 +407,10 @@ protected function getClosingTag($tag)
*/
protected function purifyHtml($html)
{
if (null === $this->purifier) {
return $html;
}

return $this->purifier->purify($html);
}

Expand Down
17 changes: 17 additions & 0 deletions lib/Caxy/HtmlDiff/HtmlDiffConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class HtmlDiffConfig
*/
protected $cacheProvider;

/**
* @var bool
*/
protected $purifierEnabled = true;

/**
* @var null|string
*/
Expand Down Expand Up @@ -468,6 +473,18 @@ public function getCacheProvider()
return $this->cacheProvider;
}

public function isPurifierEnabled(): bool
{
return $this->purifierEnabled;
}

public function setPurifierEnabled(bool $purifierEnabled = true): self
{
$this->purifierEnabled = $purifierEnabled;

return $this;
}

/**
* @param null|string
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Caxy/HtmlDiff/Table/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ protected function createDocumentWithHtml($text)
{
$dom = new \DOMDocument();
$dom->loadHTML(mb_convert_encoding(
$this->purifier->purify(mb_convert_encoding($text, $this->config->getEncoding(), mb_detect_encoding($text))),
$this->purifyHtml(mb_convert_encoding($text, $this->config->getEncoding(), mb_detect_encoding($text))),
'HTML-ENTITIES',
$this->config->getEncoding()
));
Expand Down

0 comments on commit e7628e3

Please sign in to comment.