Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed HTMLPurifier not using the cache directory #53

Merged
merged 1 commit into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions lib/Caxy/HtmlDiff/AbstractDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ abstract class AbstractDiff
* @deprecated since 0.1.0
*/
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');

/**
* @var array
*
* @deprecated since 0.1.0
*/
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\'');

/**
* @var bool
*
Expand All @@ -35,18 +37,22 @@ abstract class AbstractDiff
* @var string
*/
protected $content;

/**
* @var string
*/
protected $oldText;

/**
* @var string
*/
protected $newText;

/**
* @var array
*/
protected $oldWords = array();

/**
* @var array
*/
Expand All @@ -62,6 +68,11 @@ abstract class AbstractDiff
*/
protected $purifier;

/**
* @var \HTMLPurifier_Config|null
*/
protected $purifierConfig = null;

/**
* AbstractDiff constructor.
*
Expand All @@ -85,10 +96,9 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas
$this->config->setGroupDiffs($groupDiffs);
}

$this->oldText = $this->purifyHtml($oldText);
$this->newText = $this->purifyHtml($newText);
$this->oldText = $oldText;
$this->newText = $newText;
$this->content = '';

}

/**
Expand All @@ -103,23 +113,44 @@ abstract public function build();
*/
public function initPurifier($defaultPurifierSerializerCache = null)
{
$HTMLPurifierConfig = \HTMLPurifier_Config::createDefault();
$HTMLPurifierConfig = null;

if (null !== $this->purifierConfig) {
$HTMLPurifierConfig = $this->purifierConfig;
} else {
$HTMLPurifierConfig = \HTMLPurifier_Config::createDefault();
}

// Cache.SerializerPath defaults to Null and sets
// the location to inside the vendor HTMLPurifier library
// under the DefinitionCache/Serializer folder.
if (!is_null($defaultPurifierSerializerCache)) {
$HTMLPurifierConfig->set('Cache.SerializerPath', $defaultPurifierSerializerCache);
}

$this->purifier = new \HTMLPurifier($HTMLPurifierConfig);
}

/**
* Prepare (purify) the HTML
*
* @return void
*/
protected function prepare()
{
$this->initPurifier($this->config->getPurifierCacheLocation());

$this->oldText = $this->purifyHtml($this->oldText);
$this->newText = $this->purifyHtml($this->newText);
}

/**
* @return DiffCache|null
*/
protected function getDiffCache()
{
if (!$this->hasDiffCache()) {
return;
return null;
}

$hash = spl_object_hash($this->getConfig()->getCacheProvider());
Expand Down Expand Up @@ -155,7 +186,6 @@ public function getConfig()
public function setConfig(HtmlDiffConfig $config)
{
$this->config = $config;
$this->initPurifier($this->config->getPurifierCacheLocation());

return $this;
}
Expand Down Expand Up @@ -322,6 +352,14 @@ public function isGroupDiffs()
return $this->config->isGroupDiffs();
}

/**
* @param \HTMLPurifier_Config $config
*/
public function setHTMLPurifierConfig(\HTMLPurifier_Config $config)
{
$this->purifierConfig = $config;
}

/**
* @param string $tag
*
Expand Down
2 changes: 2 additions & 0 deletions lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function getInsertSpaceInReplace()
*/
public function build()
{
$this->prepare();

if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

Expand Down
2 changes: 2 additions & 0 deletions lib/Caxy/HtmlDiff/ListDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static function create($oldText, $newText, HtmlDiffConfig $config = null)

public function build()
{
$this->prepare();

if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

Expand Down
2 changes: 2 additions & 0 deletions lib/Caxy/HtmlDiff/ListDiffLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public static function create($oldText, $newText, HtmlDiffConfig $config = null)
*/
public function build()
{
$this->prepare();

if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

Expand Down
2 changes: 2 additions & 0 deletions lib/Caxy/HtmlDiff/Table/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public function __construct(
*/
public function build()
{
$this->prepare();

if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

Expand Down
74 changes: 74 additions & 0 deletions tests/Caxy/Tests/HtmlDiff/Functional/HTMLPurifierConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Caxy\Tests\HtmlDiff\Functional;

use Caxy\HtmlDiff\HtmlDiff;
use Caxy\HtmlDiff\HtmlDiffConfig;

class HTMLPurifierConfigTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \HTMLPurifier_Config
*/
protected $config;

public function setUp()
{
$config = \HTMLPurifier_Config::createDefault();

$this->config = $this
->getMockBuilder('\\HTMLPurifier_Config')
->disableOriginalConstructor()
->getMock();

$this->config->expects($this->atLeastOnce())
->method('set')
->with('Cache.SerializerPath', '/tmp');

$this->config->expects($this->any())
->method('getHTMLDefinition')
->will($this->returnValue($config->getHTMLDefinition()));

$this->config->expects($this->any())
->method('get')
->will($this->returnCallback(function ($argument) {
$config = \HTMLPurifier_Config::createDefault();

return $config->get($argument);
}));

$this->config->expects($this->any())
->method('getBatch')
->will($this->returnCallback(function ($argument) {
$config = \HTMLPurifier_Config::createDefault();

return $config->getBatch($argument);
}));
}

public function testHtmlDiffConfigTraditional()
{
$oldText = '<b>text</b>';
$newText = '<b>t3xt</b>';

$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', array());

$diff->getConfig()->setPurifierCacheLocation('/tmp');
$diff->setHTMLPurifierConfig($this->config);

$diff->build();
}

public function testHtmlDiffConfigStatic()
{
$oldText = '<b>text</b>';
$newText = '<b>t3xt</b>';

$config = new HtmlDiffConfig();
$config->setPurifierCacheLocation('/tmp');

$diff = HtmlDiff::create($oldText, $newText, $config);
$diff->setHTMLPurifierConfig($this->config);
$diff->build();
}
}