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