Skip to content

Commit dcf7415

Browse files
authored
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
1 parent 4971801 commit dcf7415

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/PhpSpreadsheet/Writer/Html.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri
13601360
* @param null|Cell|string $cell
13611361
* @param array|string $cssClass
13621362
*/
1363-
private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, string $cellType): string
1363+
private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass): string
13641364
{
13651365
$cellData = ' ';
13661366
if ($cell instanceof Cell) {
@@ -1384,14 +1384,10 @@ private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, st
13841384
$cssClass .= ' style' . $cell->getXfIndex();
13851385
$cssClass .= ' ' . $cell->getDataType();
13861386
} elseif (is_array($cssClass)) {
1387-
if ($cellType == 'th') {
1388-
if (isset($this->cssStyles['th.style' . $cell->getXfIndex()])) {
1389-
$cssClass = array_merge($cssClass, $this->cssStyles['th.style' . $cell->getXfIndex()]);
1390-
}
1391-
} else {
1392-
if (isset($this->cssStyles['td.style' . $cell->getXfIndex()])) {
1393-
$cssClass = array_merge($cssClass, $this->cssStyles['td.style' . $cell->getXfIndex()]);
1394-
}
1387+
$index = $cell->getXfIndex();
1388+
$styleIndex = 'td.style' . $index . ', th.style' . $index;
1389+
if (isset($this->cssStyles[$styleIndex])) {
1390+
$cssClass = array_merge($cssClass, $this->cssStyles[$styleIndex]);
13951391
}
13961392

13971393
// General horizontal alignment: Actual horizontal alignment depends on dataType
@@ -1511,7 +1507,7 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp
15111507
[$cell, $cssClass, $coordinate] = $this->generateRowCellCss($worksheet, $cellAddress, $row, $colNum);
15121508

15131509
// Cell Data
1514-
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass, $cellType);
1510+
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass);
15151511

15161512
// Hyperlink?
15171513
if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
4+
5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
use PhpOffice\PhpSpreadsheet\Style\Fill;
7+
use PhpOffice\PhpSpreadsheet\Writer\Html;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue3678Test extends TestCase
11+
{
12+
public function testInlineAndNot(): void
13+
{
14+
$spreadsheet = new Spreadsheet();
15+
$sheet = $spreadsheet->getActiveSheet();
16+
$sheet->getCell('A1')->setValue(1);
17+
$styleArray = [
18+
'fill' => [
19+
'fillType' => Fill::FILL_SOLID,
20+
'color' => ['rgb' => 'FFFF00'],
21+
],
22+
];
23+
$sheet->getStyle('A1')->applyFromArray($styleArray);
24+
$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";
25+
$style2 = $style1 . '; text-align:right; width:42pt';
26+
$writer = new Html($spreadsheet);
27+
$html = $writer->generateHtmlAll();
28+
self::assertStringContainsString('td.style1, th.style1 { ' . $style1 . ' }', $html);
29+
self::assertStringContainsString('<td class="column0 style1 n">1</td>', $html);
30+
self::assertStringContainsString('table.sheet0 col.col0 { width:42pt }', $html);
31+
self::assertStringContainsString('.n { text-align:right }', $html);
32+
$writer->setUseInlineCss(true);
33+
$html = $writer->generateHtmlAll();
34+
self::assertStringContainsString('<td style="' . $style2 . '">1</td>', $html);
35+
$spreadsheet->disconnectWorksheets();
36+
}
37+
}

0 commit comments

Comments
 (0)