From 9f3c29e6bef607b502ed2b2f1270d62a324b7b6d Mon Sep 17 00:00:00 2001 From: madflow Date: Mon, 17 Oct 2016 16:29:42 +0200 Subject: [PATCH] added hhvm specific hack, #329 --- src/Spout/Common/Escaper/ODS.php | 13 +++++++++---- tests/Spout/Writer/ODS/WriterTest.php | 9 ++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Spout/Common/Escaper/ODS.php b/src/Spout/Common/Escaper/ODS.php index c26c73b..9462ab3 100644 --- a/src/Spout/Common/Escaper/ODS.php +++ b/src/Spout/Common/Escaper/ODS.php @@ -4,9 +4,6 @@ namespace Box\Spout\Common\Escaper; use Box\Spout\Common\Singleton; -// HVM hack https://github.com/box/spout/issues/329 -defined('ENT_DISALLOWED') || define('ENT_DISALLOWED', 128); - /** * Class ODS * Provides functions to escape and unescape data for ODS files @@ -25,7 +22,15 @@ class ODS implements EscaperInterface */ public function escape($string) { - return htmlspecialchars($string, ENT_QUOTES | ENT_DISALLOWED); + if (defined('ENT_DISALLOWED')) { + return htmlspecialchars($string, ENT_QUOTES | ENT_DISALLOWED); + } else { + // We are on hhvm or any other engine that does not support ENT_DISALLOWED + // https://github.com/box/spout/issues/329 + $escapedString = htmlspecialchars($string, ENT_QUOTES); + $replacedString = preg_replace('/[\x00-\x08\x0B-\x0C\x0E-\x1F]/', '�', $escapedString); + return $replacedString; + } } /** diff --git a/tests/Spout/Writer/ODS/WriterTest.php b/tests/Spout/Writer/ODS/WriterTest.php index c9c7df7..7ae6f30 100644 --- a/tests/Spout/Writer/ODS/WriterTest.php +++ b/tests/Spout/Writer/ODS/WriterTest.php @@ -468,10 +468,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase $reader->open($resourcePath); $canBeRead = false; + $rowsRead = []; try { foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) { foreach ($sheet->getRowIterator() as $rowIndex => $row) { - + $rowsRead[] = $row; } } $canBeRead = true; @@ -479,6 +480,12 @@ class WriterTest extends \PHPUnit_Framework_TestCase } catch(\Exception $e) {} $this->assertTrue($canBeRead, 'The file with illegal chars can be read'); + $dataRowsExpected = [ + ['I am a text'], + ['I am a vertical tab:�'], + ['I am a form feed:�'], + ]; + $this->assertEquals($dataRowsExpected, $rowsRead, 'Correct rows with unicode replacement are read'); } /**