Skip to content

Commit 6f9616d

Browse files
committed
Some encoding updates
1 parent 7e900f2 commit 6f9616d

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

demo/index.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ function addDebugOutput($value, $key = 'general')
4040
$diff->setMatchThreshold($data['matchThreshold']);
4141
}
4242
$diff->setUseTableDiffing($useTableDiffing);
43-
$diff->build();
43+
$diffOutput = $diff->build();
44+
$diffOutput = mb_convert_encoding($diffOutput, 'UTF-8');
4445

45-
echo json_encode(array('diff' => $diff->getDifference(), 'debug' => $debugOutput));
46+
$jsonOutput = json_encode(array('diff' => $diffOutput, 'debug' => $debugOutput));
47+
48+
if (false === $jsonOutput) {
49+
throw new \Exception('Failed to encode JSON: '.json_last_error_msg());
50+
}
51+
52+
echo $jsonOutput;
4653
} else {
4754
header('Content-Type: text/html');
4855
echo file_get_contents('demo.html');

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ abstract class AbstractDiff
8181
*/
8282
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null)
8383
{
84+
mb_substitute_character(0x20);
85+
8486
if ($specialCaseTags === null) {
8587
$specialCaseTags = static::$defaultSpecialCaseTags;
8688
}

lib/Caxy/HtmlDiff/Table/TableDiff.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,25 @@ protected function diffCells($oldCell, $newCell, $usingExtraRow = false)
590590

591591
protected function buildTableDoms()
592592
{
593-
$this->oldTable = $this->parseTableStructure(mb_convert_encoding($this->oldText, 'HTML-ENTITIES', 'UTF-8'));
594-
$this->newTable = $this->parseTableStructure(mb_convert_encoding($this->newText, 'HTML-ENTITIES', 'UTF-8'));
593+
$this->oldTable = $this->parseTableStructure($this->oldText);
594+
$this->newTable = $this->parseTableStructure($this->newText);
595+
}
596+
597+
/**
598+
* @param string $text
599+
*
600+
* @return \DOMDocument
601+
*/
602+
protected function createDocumentWithHtml($text)
603+
{
604+
$dom = new \DOMDocument();
605+
$dom->loadHTML(mb_convert_encoding(
606+
$this->purifier->purify(mb_convert_encoding($text, $this->encoding, mb_detect_encoding($text))),
607+
'HTML-ENTITIES',
608+
$this->encoding
609+
));
610+
611+
return $dom;
595612
}
596613

597614
/**
@@ -601,8 +618,7 @@ protected function buildTableDoms()
601618
*/
602619
protected function parseTableStructure($text)
603620
{
604-
$dom = new \DOMDocument();
605-
$dom->loadHTML($text);
621+
$dom = $this->createDocumentWithHtml($text);
606622

607623
$tableNode = $dom->getElementsByTagName('table')->item(0);
608624

@@ -692,8 +708,7 @@ protected function setInnerHtml($node, $html)
692708
$html = '<span class="empty"></span>';
693709
}
694710

695-
$doc = new \DOMDocument();
696-
$doc->loadHTML(mb_convert_encoding($this->purifier->purify($html), 'HTML-ENTITIES', 'UTF-8'));
711+
$doc = $this->createDocumentWithHtml($html);
697712
$fragment = $node->ownerDocument->createDocumentFragment();
698713
$root = $doc->getElementsByTagName('body')->item(0);
699714
foreach ($root->childNodes as $child) {

0 commit comments

Comments
 (0)