Skip to content

Commit

Permalink
Some encoding updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jschroed91 committed Feb 26, 2016
1 parent 7e900f2 commit 6f9616d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
11 changes: 9 additions & 2 deletions demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ function addDebugOutput($value, $key = 'general')
$diff->setMatchThreshold($data['matchThreshold']);
}
$diff->setUseTableDiffing($useTableDiffing);
$diff->build();
$diffOutput = $diff->build();
$diffOutput = mb_convert_encoding($diffOutput, 'UTF-8');

echo json_encode(array('diff' => $diff->getDifference(), 'debug' => $debugOutput));
$jsonOutput = json_encode(array('diff' => $diffOutput, 'debug' => $debugOutput));

if (false === $jsonOutput) {
throw new \Exception('Failed to encode JSON: '.json_last_error_msg());
}

echo $jsonOutput;
} else {
header('Content-Type: text/html');
echo file_get_contents('demo.html');
Expand Down
2 changes: 2 additions & 0 deletions lib/Caxy/HtmlDiff/AbstractDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ abstract class AbstractDiff
*/
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null)
{
mb_substitute_character(0x20);

if ($specialCaseTags === null) {
$specialCaseTags = static::$defaultSpecialCaseTags;
}
Expand Down
27 changes: 21 additions & 6 deletions lib/Caxy/HtmlDiff/Table/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,25 @@ protected function diffCells($oldCell, $newCell, $usingExtraRow = false)

protected function buildTableDoms()
{
$this->oldTable = $this->parseTableStructure(mb_convert_encoding($this->oldText, 'HTML-ENTITIES', 'UTF-8'));
$this->newTable = $this->parseTableStructure(mb_convert_encoding($this->newText, 'HTML-ENTITIES', 'UTF-8'));
$this->oldTable = $this->parseTableStructure($this->oldText);
$this->newTable = $this->parseTableStructure($this->newText);
}

/**
* @param string $text
*
* @return \DOMDocument
*/
protected function createDocumentWithHtml($text)
{
$dom = new \DOMDocument();
$dom->loadHTML(mb_convert_encoding(
$this->purifier->purify(mb_convert_encoding($text, $this->encoding, mb_detect_encoding($text))),
'HTML-ENTITIES',
$this->encoding
));

return $dom;
}

/**
Expand All @@ -601,8 +618,7 @@ protected function buildTableDoms()
*/
protected function parseTableStructure($text)
{
$dom = new \DOMDocument();
$dom->loadHTML($text);
$dom = $this->createDocumentWithHtml($text);

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

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

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

0 comments on commit 6f9616d

Please sign in to comment.