Skip to content

Commit

Permalink
Html Writer Styles when Using Inline Css (#3680)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
oleibman authored Aug 21, 2023
1 parent 4971801 commit dcf7415
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri
* @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) {
Expand All @@ -1384,14 +1384,10 @@ private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, st
$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
Expand Down Expand Up @@ -1511,7 +1507,7 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp
[$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()) {
Expand Down
37 changes: 37 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PHPUnit\Framework\TestCase;

class Issue3678Test extends TestCase
{
public function testInlineAndNot(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->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('<td class="column0 style1 n">1</td>', $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('<td style="' . $style2 . '">1</td>', $html);
$spreadsheet->disconnectWorksheets();
}
}

0 comments on commit dcf7415

Please sign in to comment.