diff --git a/lib/Caxy/HtmlDiff/AbstractDiff.php b/lib/Caxy/HtmlDiff/AbstractDiff.php index 6deb3c3..f15b271 100644 --- a/lib/Caxy/HtmlDiff/AbstractDiff.php +++ b/lib/Caxy/HtmlDiff/AbstractDiff.php @@ -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 * @@ -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 */ @@ -62,6 +68,11 @@ abstract class AbstractDiff */ protected $purifier; + /** + * @var \HTMLPurifier_Config|null + */ + protected $purifierConfig = null; + /** * AbstractDiff constructor. * @@ -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 = ''; - } /** @@ -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()); @@ -155,7 +186,6 @@ public function getConfig() public function setConfig(HtmlDiffConfig $config) { $this->config = $config; - $this->initPurifier($this->config->getPurifierCacheLocation()); return $this; } @@ -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 * diff --git a/lib/Caxy/HtmlDiff/HtmlDiff.php b/lib/Caxy/HtmlDiff/HtmlDiff.php index d884035..8d6e323 100644 --- a/lib/Caxy/HtmlDiff/HtmlDiff.php +++ b/lib/Caxy/HtmlDiff/HtmlDiff.php @@ -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); diff --git a/lib/Caxy/HtmlDiff/ListDiff.php b/lib/Caxy/HtmlDiff/ListDiff.php index ef7bb76..a0b6363 100644 --- a/lib/Caxy/HtmlDiff/ListDiff.php +++ b/lib/Caxy/HtmlDiff/ListDiff.php @@ -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); diff --git a/lib/Caxy/HtmlDiff/ListDiffLines.php b/lib/Caxy/HtmlDiff/ListDiffLines.php index 38c07c8..0fdc7e9 100644 --- a/lib/Caxy/HtmlDiff/ListDiffLines.php +++ b/lib/Caxy/HtmlDiff/ListDiffLines.php @@ -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); diff --git a/lib/Caxy/HtmlDiff/Table/TableDiff.php b/lib/Caxy/HtmlDiff/Table/TableDiff.php index 1838062..cfd1435 100644 --- a/lib/Caxy/HtmlDiff/Table/TableDiff.php +++ b/lib/Caxy/HtmlDiff/Table/TableDiff.php @@ -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); diff --git a/tests/Caxy/Tests/HtmlDiff/Functional/HTMLPurifierConfigTest.php b/tests/Caxy/Tests/HtmlDiff/Functional/HTMLPurifierConfigTest.php new file mode 100644 index 0000000..e184ae0 --- /dev/null +++ b/tests/Caxy/Tests/HtmlDiff/Functional/HTMLPurifierConfigTest.php @@ -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(); + } +} \ No newline at end of file