Skip to content

Commit 11d3493

Browse files
committed
Merge pull request #53 from SavageTiger/master
Fixed HTMLPurifier not using the cache directory
2 parents 8430aad + 013c8f3 commit 11d3493

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ abstract class AbstractDiff
1313
* @deprecated since 0.1.0
1414
*/
1515
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
16+
1617
/**
1718
* @var array
1819
*
1920
* @deprecated since 0.1.0
2021
*/
2122
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\'');
23+
2224
/**
2325
* @var bool
2426
*
@@ -35,18 +37,22 @@ abstract class AbstractDiff
3537
* @var string
3638
*/
3739
protected $content;
40+
3841
/**
3942
* @var string
4043
*/
4144
protected $oldText;
45+
4246
/**
4347
* @var string
4448
*/
4549
protected $newText;
50+
4651
/**
4752
* @var array
4853
*/
4954
protected $oldWords = array();
55+
5056
/**
5157
* @var array
5258
*/
@@ -62,6 +68,11 @@ abstract class AbstractDiff
6268
*/
6369
protected $purifier;
6470

71+
/**
72+
* @var \HTMLPurifier_Config|null
73+
*/
74+
protected $purifierConfig = null;
75+
6576
/**
6677
* AbstractDiff constructor.
6778
*
@@ -85,10 +96,9 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas
8596
$this->config->setGroupDiffs($groupDiffs);
8697
}
8798

88-
$this->oldText = $this->purifyHtml($oldText);
89-
$this->newText = $this->purifyHtml($newText);
99+
$this->oldText = $oldText;
100+
$this->newText = $newText;
90101
$this->content = '';
91-
92102
}
93103

94104
/**
@@ -103,23 +113,44 @@ abstract public function build();
103113
*/
104114
public function initPurifier($defaultPurifierSerializerCache = null)
105115
{
106-
$HTMLPurifierConfig = \HTMLPurifier_Config::createDefault();
116+
$HTMLPurifierConfig = null;
117+
118+
if (null !== $this->purifierConfig) {
119+
$HTMLPurifierConfig = $this->purifierConfig;
120+
} else {
121+
$HTMLPurifierConfig = \HTMLPurifier_Config::createDefault();
122+
}
123+
107124
// Cache.SerializerPath defaults to Null and sets
108125
// the location to inside the vendor HTMLPurifier library
109126
// under the DefinitionCache/Serializer folder.
110127
if (!is_null($defaultPurifierSerializerCache)) {
111128
$HTMLPurifierConfig->set('Cache.SerializerPath', $defaultPurifierSerializerCache);
112129
}
130+
113131
$this->purifier = new \HTMLPurifier($HTMLPurifierConfig);
114132
}
115133

134+
/**
135+
* Prepare (purify) the HTML
136+
*
137+
* @return void
138+
*/
139+
protected function prepare()
140+
{
141+
$this->initPurifier($this->config->getPurifierCacheLocation());
142+
143+
$this->oldText = $this->purifyHtml($this->oldText);
144+
$this->newText = $this->purifyHtml($this->newText);
145+
}
146+
116147
/**
117148
* @return DiffCache|null
118149
*/
119150
protected function getDiffCache()
120151
{
121152
if (!$this->hasDiffCache()) {
122-
return;
153+
return null;
123154
}
124155

125156
$hash = spl_object_hash($this->getConfig()->getCacheProvider());
@@ -155,7 +186,6 @@ public function getConfig()
155186
public function setConfig(HtmlDiffConfig $config)
156187
{
157188
$this->config = $config;
158-
$this->initPurifier($this->config->getPurifierCacheLocation());
159189

160190
return $this;
161191
}
@@ -322,6 +352,14 @@ public function isGroupDiffs()
322352
return $this->config->isGroupDiffs();
323353
}
324354

355+
/**
356+
* @param \HTMLPurifier_Config $config
357+
*/
358+
public function setHTMLPurifierConfig(\HTMLPurifier_Config $config)
359+
{
360+
$this->purifierConfig = $config;
361+
}
362+
325363
/**
326364
* @param string $tag
327365
*

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public function getInsertSpaceInReplace()
9191
*/
9292
public function build()
9393
{
94+
$this->prepare();
95+
9496
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
9597
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);
9698

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static function create($oldText, $newText, HtmlDiffConfig $config = null)
2929

3030
public function build()
3131
{
32+
$this->prepare();
33+
3234
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
3335
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);
3436

lib/Caxy/HtmlDiff/ListDiffLines.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static function create($oldText, $newText, HtmlDiffConfig $config = null)
5757
*/
5858
public function build()
5959
{
60+
$this->prepare();
61+
6062
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
6163
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);
6264

lib/Caxy/HtmlDiff/Table/TableDiff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public function __construct(
8989
*/
9090
public function build()
9191
{
92+
$this->prepare();
93+
9294
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
9395
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);
9496

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Caxy\Tests\HtmlDiff\Functional;
4+
5+
use Caxy\HtmlDiff\HtmlDiff;
6+
use Caxy\HtmlDiff\HtmlDiffConfig;
7+
8+
class HTMLPurifierConfigTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \HTMLPurifier_Config
12+
*/
13+
protected $config;
14+
15+
public function setUp()
16+
{
17+
$config = \HTMLPurifier_Config::createDefault();
18+
19+
$this->config = $this
20+
->getMockBuilder('\\HTMLPurifier_Config')
21+
->disableOriginalConstructor()
22+
->getMock();
23+
24+
$this->config->expects($this->atLeastOnce())
25+
->method('set')
26+
->with('Cache.SerializerPath', '/tmp');
27+
28+
$this->config->expects($this->any())
29+
->method('getHTMLDefinition')
30+
->will($this->returnValue($config->getHTMLDefinition()));
31+
32+
$this->config->expects($this->any())
33+
->method('get')
34+
->will($this->returnCallback(function ($argument) {
35+
$config = \HTMLPurifier_Config::createDefault();
36+
37+
return $config->get($argument);
38+
}));
39+
40+
$this->config->expects($this->any())
41+
->method('getBatch')
42+
->will($this->returnCallback(function ($argument) {
43+
$config = \HTMLPurifier_Config::createDefault();
44+
45+
return $config->getBatch($argument);
46+
}));
47+
}
48+
49+
public function testHtmlDiffConfigTraditional()
50+
{
51+
$oldText = '<b>text</b>';
52+
$newText = '<b>t3xt</b>';
53+
54+
$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', array());
55+
56+
$diff->getConfig()->setPurifierCacheLocation('/tmp');
57+
$diff->setHTMLPurifierConfig($this->config);
58+
59+
$diff->build();
60+
}
61+
62+
public function testHtmlDiffConfigStatic()
63+
{
64+
$oldText = '<b>text</b>';
65+
$newText = '<b>t3xt</b>';
66+
67+
$config = new HtmlDiffConfig();
68+
$config->setPurifierCacheLocation('/tmp');
69+
70+
$diff = HtmlDiff::create($oldText, $newText, $config);
71+
$diff->setHTMLPurifierConfig($this->config);
72+
$diff->build();
73+
}
74+
}

0 commit comments

Comments
 (0)