StyleMerger Use Copy Of baseStyle

Using baseStyle directly can lead to the by-reference problems associated with Cell and Style.
This commit is contained in:
oleibman
2025-07-17 07:45:55 -07:00
parent ffeaa42583
commit 2735c40179
3 changed files with 19 additions and 4 deletions
@@ -14,7 +14,10 @@ class StyleMerger
public function __construct(Style $baseStyle)
{
$this->baseStyle = $baseStyle;
// Setting to $baseStyle sometimes causes problems later on.
$array = $baseStyle->exportArray();
$this->baseStyle = new Style();
$this->baseStyle->applyFromArray($array);
}
public function getStyle(): Style
@@ -19,17 +19,29 @@ class Issue4539Test extends TestCase
$writer->setConditionalFormatting(true);
$writer->setUseInlineCss(true);
$html = $writer->generateHtmlAll();
$expected = '<td class="gridlines" style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; color:#000000;background-color:#5A8AC6;">5</td>';
$expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; color:#000000;background-color:#5A8AC6;">5</td>';
self::assertStringContainsString($expected, $html, 'inline conditional style');
$expected = '<td class="gridlines" style="vertical-align:bottom; font-weight:bold; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:left; width:102pt">Column Heading</td>';
$expected = '<td style="vertical-align:bottom; font-weight:bold; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:left; width:102pt">Column Heading</td>';
self::assertStringContainsString($expected, $html, 'inline no conditional style');
$expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; border-top:1px solid #92D050 !important;color:#000000;">1</td>';
self::assertStringContainsString($expected, $html, 'inline border-top');
$expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; border-top:1px solid #FF0000 !important;font-weight:bold;color:#000000;">2</td>';
self::assertStringContainsString($expected, $html, 'inline border-top and bold');
$expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt">3</td>';
self::assertStringContainsString($expected, $html, 'inline nomatch');
$writer->setUseInlineCss(false);
$html = $writer->generateHtmlAll();
$expected = '<td class="column0 style2 n" style="color:#000000;background-color:#5A8AC6;">5</td>';
$expected = '<td class="column0 style0 n" style="color:#000000;background-color:#5A8AC6;">5</td>';
self::assertStringContainsString($expected, $html, 'notinline conditional style');
$expected = '<td class="column0 style1 s">Column Heading</td>';
self::assertStringContainsString($expected, $html, 'notinline no conditional style');
$expected = '<td class="column0 style0 n" style="border-top:1px solid #92D050 !important;color:#000000;">1</td>';
self::assertStringContainsString($expected, $html, 'notinline border-top');
$expected = '<td class="column0 style0 n" style="border-top:1px solid #FF0000 !important;font-weight:bold;color:#000000;">2</td>';
self::assertStringContainsString($expected, $html, 'notinline border-top bold');
$expected = '<td class="column0 style0 n">3</td>';
self::assertStringContainsString($expected, $html, 'notinline nomatch');
$spreadsheet->disconnectWorksheets();
}
Binary file not shown.