Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Xlsx Reader Vml File with Unclosed br Tags #3127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ public static function falseToArray($value): array
return is_array($value) ? $value : [];
}

private function loadZip(string $filename, string $ns = ''): SimpleXMLElement
private function loadZip(string $filename, string $ns = '', bool $replaceUnclosedBr = false): SimpleXMLElement
{
$contents = $this->getFromZipArchive($this->zip, $filename);
if ($replaceUnclosedBr) {
$contents = str_replace('<br>', '<br/>', $contents);
}
$rels = simplexml_load_string(
$this->securityScanner->scan($contents),
'SimpleXMLElement',
Expand Down Expand Up @@ -1030,7 +1033,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet

try {
// no namespace okay - processed with Xpath
$vmlCommentsFile = $this->loadZip($relPath, '');
$vmlCommentsFile = $this->loadZip($relPath, '', true);
$vmlCommentsFile->registerXPathNamespace('v', Namespaces::URN_VML);
} catch (Throwable $ex) {
//Ignore unparsable vmlDrawings. Later they will be moved from $unparsedVmlDrawings to $unparsedLoadedData
Expand Down
24 changes: 24 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/ColorTabTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ public function testIssue2316(): void
$filename = 'tests/data/Reader/XLSX/colortabs.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$unparsed = $spreadsheet->getUnparsedLoadedData();
self::assertArrayHasKey('sheets', $unparsed);
self::assertArrayHasKey('Worksheet', $unparsed['sheets']);
self::assertArrayNotHasKey('vmlDrawings', $unparsed['sheets']['Worksheet']);

// theme color
self::assertSame('FF548135', $spreadsheet->getSheet(0)->getTabColor()->getArgb());
// rgb color
self::assertSame('FFFFC000', $spreadsheet->getSheet(1)->getTabColor()->getArgb());
$spreadsheet->disconnectWorksheets();
}

public function testIssue3125(): void
{
// Very artificial - no real sample file to go with issue.
$filename = 'tests/data/Reader/XLSX/colortabs.badbr.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$unparsed = $spreadsheet->getUnparsedLoadedData();
self::assertArrayHasKey('sheets', $unparsed);
self::assertArrayHasKey('Worksheet', $unparsed['sheets']);
// Before fix, vml in sample file was unparseable as xml (unclosed br tag),
// so condition below would fail.
self::assertArrayNotHasKey('vmlDrawings', $unparsed['sheets']['Worksheet']);

// theme color
self::assertSame('FF548135', $spreadsheet->getSheet(0)->getTabColor()->getArgb());
Expand Down
Binary file added tests/data/Reader/XLSX/colortabs.badbr.xlsx
Binary file not shown.