diff --git a/CHANGELOG.md b/CHANGELOG.md index e7dfefca..2b85901e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Control characters in cell values are automatically escaped [#212](https://github.com/PHPOffice/PhpSpreadsheet/issues/212) + ### BREAKING CHANGE - Standardization of array keys used for style, see the [migration guide](./docs/topics/migration-from-PHPExcel.md). diff --git a/src/PhpSpreadsheet/Shared/StringHelper.php b/src/PhpSpreadsheet/Shared/StringHelper.php index b7e8e568..8eb66423 100644 --- a/src/PhpSpreadsheet/Shared/StringHelper.php +++ b/src/PhpSpreadsheet/Shared/StringHelper.php @@ -300,11 +300,12 @@ class StringHelper return true; } - public static function buildCharacterSets() + private static function buildCharacterSets() { if (empty(self::$controlCharacters)) { self::buildControlCharacters(); } + if (empty(self::$SYLKCharacters)) { self::buildSYLKCharacters(); } @@ -327,6 +328,8 @@ class StringHelper */ public static function controlCharacterOOXML2PHP($value) { + self::buildCharacterSets(); + return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value); } @@ -347,6 +350,8 @@ class StringHelper */ public static function controlCharacterPHP2OOXML($value) { + self::buildCharacterSets(); + return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value); } @@ -713,6 +718,8 @@ class StringHelper */ public static function SYLKtoUTF8($pValue) { + self::buildCharacterSets(); + // If there is no escape character in the string there is nothing to do if (strpos($pValue, '') === false) { return $pValue; diff --git a/tests/PhpSpreadsheetTests/Shared/StringTest.php b/tests/PhpSpreadsheetTests/Shared/StringHelperTest.php similarity index 74% rename from tests/PhpSpreadsheetTests/Shared/StringTest.php rename to tests/PhpSpreadsheetTests/Shared/StringHelperTest.php index 9fbaba33..67b82f7e 100644 --- a/tests/PhpSpreadsheetTests/Shared/StringTest.php +++ b/tests/PhpSpreadsheetTests/Shared/StringHelperTest.php @@ -5,7 +5,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Shared; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PHPUnit_Framework_TestCase; -class StringTest extends PHPUnit_Framework_TestCase +class StringHelperTest extends PHPUnit_Framework_TestCase { public function setUp() { @@ -73,4 +73,28 @@ class StringTest extends PHPUnit_Framework_TestCase $result = StringHelper::getCurrencyCode(); $this->assertEquals($expectedResult, $result); } + + public function testControlCharacterPHP2OOXML() + { + $expectedResult = 'foo_x000B_bar'; + $result = StringHelper::controlCharacterPHP2OOXML('foo' . chr(11) . 'bar'); + + $this->assertEquals($expectedResult, $result); + } + + public function testControlCharacterOOXML2PHP() + { + $expectedResult = 'foo' . chr(11) . 'bar'; + $result = StringHelper::controlCharacterOOXML2PHP('foo_x000B_bar'); + + $this->assertEquals($expectedResult, $result); + } + + public function testSYLKtoUTF8() + { + $expectedResult = 'foo' . chr(11) . 'bar'; + $result = StringHelper::SYLKtoUTF8("foo\x1B ;bar"); + + $this->assertEquals($expectedResult, $result); + } }