From dcf74157626c9127c3e2d18d12e48c2764242423 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Mon, 21 Aug 2023 07:58:25 -0700 Subject: [PATCH] Html Writer Styles when Using Inline Css (#3680) * Html Writer Styles when Using Inline Css Fix #3678. Problem introduced by PR #3016. Combining `td` and `th` styles into a single declaration greatly reduces file size when `useInlineCss` is false, which is the default. However, generating code with the non-default option was not changed to use the combined declaration, so styling was lost. This PR rectifies that error. * Apostrophe Rather Than Quote --- src/PhpSpreadsheet/Writer/Html.php | 16 +++----- .../Writer/Html/Issue3678Test.php | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 53cd86bea..5c907149e 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -1360,7 +1360,7 @@ class Html extends BaseWriter * @param null|Cell|string $cell * @param array|string $cssClass */ - private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, string $cellType): string + private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass): string { $cellData = ' '; if ($cell instanceof Cell) { @@ -1384,14 +1384,10 @@ class Html extends BaseWriter $cssClass .= ' style' . $cell->getXfIndex(); $cssClass .= ' ' . $cell->getDataType(); } elseif (is_array($cssClass)) { - if ($cellType == 'th') { - if (isset($this->cssStyles['th.style' . $cell->getXfIndex()])) { - $cssClass = array_merge($cssClass, $this->cssStyles['th.style' . $cell->getXfIndex()]); - } - } else { - if (isset($this->cssStyles['td.style' . $cell->getXfIndex()])) { - $cssClass = array_merge($cssClass, $this->cssStyles['td.style' . $cell->getXfIndex()]); - } + $index = $cell->getXfIndex(); + $styleIndex = 'td.style' . $index . ', th.style' . $index; + if (isset($this->cssStyles[$styleIndex])) { + $cssClass = array_merge($cssClass, $this->cssStyles[$styleIndex]); } // General horizontal alignment: Actual horizontal alignment depends on dataType @@ -1511,7 +1507,7 @@ class Html extends BaseWriter [$cell, $cssClass, $coordinate] = $this->generateRowCellCss($worksheet, $cellAddress, $row, $colNum); // Cell Data - $cellData = $this->generateRowCellData($worksheet, $cell, $cssClass, $cellType); + $cellData = $this->generateRowCellData($worksheet, $cell, $cssClass); // Hyperlink? if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) { diff --git a/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php b/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php new file mode 100644 index 000000000..5c0a7cd02 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php @@ -0,0 +1,37 @@ +getActiveSheet(); + $sheet->getCell('A1')->setValue(1); + $styleArray = [ + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'color' => ['rgb' => 'FFFF00'], + ], + ]; + $sheet->getStyle('A1')->applyFromArray($styleArray); + $style1 = "vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:#FFFF00"; + $style2 = $style1 . '; text-align:right; width:42pt'; + $writer = new Html($spreadsheet); + $html = $writer->generateHtmlAll(); + self::assertStringContainsString('td.style1, th.style1 { ' . $style1 . ' }', $html); + self::assertStringContainsString('1', $html); + self::assertStringContainsString('table.sheet0 col.col0 { width:42pt }', $html); + self::assertStringContainsString('.n { text-align:right }', $html); + $writer->setUseInlineCss(true); + $html = $writer->generateHtmlAll(); + self::assertStringContainsString('1', $html); + $spreadsheet->disconnectWorksheets(); + } +}