Ods Reader Allow Omission of Some Page Settings Tags

Fix #4099. Ods Reader was expecting there to always be `header-style` and `footer-style` tags when `page-layout` tag is present, but these need not exist. It seemed like there might be other exposures along this line in `readPageSettingStyles`; rather than waiting for a problem report to show up for each, the code is updated to use `->item(0)` in place of `[0]` when appropriate, and make use of the nullsafe `?->` operator introduced with Php8.
This commit is contained in:
oleibman
2024-07-18 13:30:58 -07:00
parent 1b68270f80
commit 2952cf5526
3 changed files with 43 additions and 15 deletions
+15 -15
View File
@@ -55,22 +55,22 @@ class PageSettings
foreach ($styles as $styleSet) {
$styleName = $styleSet->getAttributeNS($this->stylesNs, 'name');
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')[0];
$styleOrientation = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-orientation');
$styleScale = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'scale-to');
$stylePrintOrder = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-page-order');
$centered = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'table-centering');
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')->item(0);
$styleOrientation = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-orientation');
$styleScale = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'scale-to');
$stylePrintOrder = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-page-order');
$centered = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'table-centering');
$marginLeft = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-left');
$marginRight = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-right');
$marginTop = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-top');
$marginBottom = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')[0];
$headerProperties = $header->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginHeader = isset($headerProperties) ? $headerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')[0];
$footerProperties = $footer->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginFooter = isset($footerProperties) ? $footerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
$marginLeft = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-left');
$marginRight = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-right');
$marginTop = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-top');
$marginBottom = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')->item(0);
$headerProperties = $header?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
$marginHeader = $headerProperties?->getAttributeNS($this->stylesFo, 'min-height');
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')->item(0);
$footerProperties = $footer?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
$marginFooter = $footerProperties?->getAttributeNS($this->stylesFo, 'min-height');
$this->pageLayoutStyles[$styleName] = (object) [
'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT,
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Reader\Ods as OdsReader;
use PHPUnit\Framework\TestCase;
class Issue4099Test extends TestCase
{
private string $file = 'tests/data/Reader/Ods/issue.4099.ods';
public function testNoHeaderFooterStyle(): void
{
// header-style and footer-style are missing in styles.xml
$zipFile = 'zip://' . $this->file . '#styles.xml';
$contents = (string) file_get_contents($zipFile);
self::assertStringContainsString('page-layout ', $contents);
self::assertStringNotContainsString('header-style', $contents);
self::assertStringNotContainsString('footer-style', $contents);
$reader = new OdsReader();
$spreadsheet = $reader->load($this->file);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('FirstCell', $sheet->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}
Binary file not shown.