Escape control characters in cell values

Control characters in cell values are automatically escaped without
the need to excplicitly call `StringHelper::buildCharacterSets()` beforehand.

Fixes #212
This commit is contained in:
Adrien Crivelli 2017-09-09 19:29:08 +09:00
parent ec7312cb64
commit 1cf119dd0b
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
3 changed files with 35 additions and 2 deletions

View File

@ -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).

View File

@ -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;

View File

@ -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);
}
}