Skip to content

Commit e7628e3

Browse files
authored
Added option to disable HTML purifier
Added option to disable HTML purifier
2 parents e9b4386 + 2a84b0b commit e7628e3

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ $config
127127

128128
// Pass an instance of \Doctrine\Common\Cache\Cache to cache the calculated diffs.
129129
->setCacheProvider(null)
130+
131+
// Disable the HTML purifier (only do this if you known what you're doing)
132+
// This bundle heavily relies on the purified input from ezyang/htmlpurifier
133+
->setPurifierEnabled(true)
130134

131135
// Set the cache directory that HTMLPurifier should use.
132136
->setPurifierCacheLocation(null)
@@ -192,7 +196,6 @@ php-htmldiff is available under [GNU General Public License, version 2][gnu]. Se
192196
* Maybe add abstraction layer for cache + adapter for doctrine cache
193197
* Make HTML Purifier an optional dependency - possibly use abstraction layer for purifiers so alternatives could be used (or none at all for performance)
194198
* Expose configuration for HTML Purifier (used in table diffing) - currently only cache dir is configurable through HtmlDiffConfig object
195-
* Add option to enable using HTML Purifier to purify all input
196199
* Performance improvements (we have 1 benchmark test, we should probably get more)
197200
* Algorithm improvements - trimming alike text at start and ends, store nested diff results in memory to re-use (like we do w/ caching)
198201
* Benchmark using DOMDocument vs. alternatives vs. string parsing

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ abstract class AbstractDiff
6666
protected $diffCaches = array();
6767

6868
/**
69-
* @var \HTMLPurifier
69+
* @var \HTMLPurifier|null
7070
*/
7171
protected $purifier;
7272

@@ -154,6 +154,10 @@ public function initPurifier($defaultPurifierSerializerCache = null)
154154
*/
155155
protected function prepare()
156156
{
157+
if (false === $this->config->isPurifierEnabled()) {
158+
return;
159+
}
160+
157161
$this->initPurifier($this->config->getPurifierCacheLocation());
158162

159163
$this->oldText = $this->purifyHtml($this->oldText);
@@ -403,6 +407,10 @@ protected function getClosingTag($tag)
403407
*/
404408
protected function purifyHtml($html)
405409
{
410+
if (null === $this->purifier) {
411+
return $html;
412+
}
413+
406414
return $this->purifier->purify($html);
407415
}
408416

lib/Caxy/HtmlDiff/HtmlDiffConfig.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class HtmlDiffConfig
8080
*/
8181
protected $cacheProvider;
8282

83+
/**
84+
* @var bool
85+
*/
86+
protected $purifierEnabled = true;
87+
8388
/**
8489
* @var null|string
8590
*/
@@ -468,6 +473,18 @@ public function getCacheProvider()
468473
return $this->cacheProvider;
469474
}
470475

476+
public function isPurifierEnabled(): bool
477+
{
478+
return $this->purifierEnabled;
479+
}
480+
481+
public function setPurifierEnabled(bool $purifierEnabled = true): self
482+
{
483+
$this->purifierEnabled = $purifierEnabled;
484+
485+
return $this;
486+
}
487+
471488
/**
472489
* @param null|string
473490
*

lib/Caxy/HtmlDiff/Table/TableDiff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ protected function createDocumentWithHtml($text)
628628
{
629629
$dom = new \DOMDocument();
630630
$dom->loadHTML(mb_convert_encoding(
631-
$this->purifier->purify(mb_convert_encoding($text, $this->config->getEncoding(), mb_detect_encoding($text))),
631+
$this->purifyHtml(mb_convert_encoding($text, $this->config->getEncoding(), mb_detect_encoding($text))),
632632
'HTML-ENTITIES',
633633
$this->config->getEncoding()
634634
));

0 commit comments

Comments
 (0)