Removed double un-escaping when reading CSV

Removed "unescape enclosure functionality", since the unescaping is already handled by fgetcsv,
and performing the unescaping again would actually result int the text from the cell being read wrong.

As an example try parsing the folowing CSV:

```
"<img alt="""" src=""http://example.com/image.jpg"" />"
```

With the additional unescaping it would have ended up as:

```
<img alt=" src="http://example.com/image.jpg" />
```

instead of the correct:
```
<img alt="" src="http://example.com/image.jpg" />
```

Fixes https://github.com/PHPOffice/PHPExcel/pull/1171
This commit is contained in:
Kurounin 2017-04-03 11:52:35 +09:00 committed by Adrien Crivelli
parent b7b10612ab
commit b01671213a
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
2 changed files with 27 additions and 10 deletions

View File

@ -172,8 +172,6 @@ class Csv extends BaseReader implements IReader
$this->skipBOM();
$this->checkSeparator();
$escapeEnclosures = ['\\' . $this->enclosure, $this->enclosure . $this->enclosure];
$worksheetInfo = [];
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
$worksheetInfo[0]['lastColumnLetter'] = 'A';
@ -246,11 +244,6 @@ class Csv extends BaseReader implements IReader
}
$sheet = $spreadsheet->setActiveSheetIndex($this->sheetIndex);
$escapeEnclosures = [
'\\' . $this->enclosure,
$this->enclosure . $this->enclosure,
];
// Set our starting row based on whether we're in contiguous mode or not
$currentRow = 1;
if ($this->contiguous) {
@ -262,9 +255,6 @@ class Csv extends BaseReader implements IReader
$columnLetter = 'A';
foreach ($rowData as $rowDatum) {
if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) {
// Unescape enclosures
$rowDatum = str_replace($escapeEnclosures, $this->enclosure, $rowDatum);
// Convert encoding if necessary
if ($this->inputEncoding !== 'UTF-8') {
$rowDatum = \PhpOffice\PhpSpreadsheet\Shared\StringHelper::convertEncoding($rowDatum, 'UTF-8', $this->inputEncoding);

View File

@ -0,0 +1,27 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class CsvTest extends \PHPUnit_Framework_TestCase
{
public function testEnclosure()
{
$value = '<img alt="" src="http://example.com/image.jpg" />';
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet');
// Write temp file with value
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->getCell('A1')->setValue($value);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->save($filename);
// Read written file
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$reloadedSpreadsheet = $reader->load($filename);
$actual = $reloadedSpreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue();
$this->assertSame($value, $actual, 'should be able to write and read strings with multiples quotes');
}
}