From 2a524e2445e27b293c9b3e0f3945f312cedac3a1 Mon Sep 17 00:00:00 2001 From: Kacper <30697294+Chudy20007@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:36:01 +0100 Subject: [PATCH] Background and border color transparency support for textboxes when color is undefined (#2555) --- docs/changes/1.x/1.4.0.md | 3 +- .../Writer/Word2007/Element/TextBox.php | 7 ++ .../Writer/Word2007/Element/TextBoxTest.php | 84 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/PhpWordTests/Writer/Word2007/Element/TextBoxTest.php diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index b5102f25fa..634d1d6f31 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -17,7 +17,8 @@ - Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727) - Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737) - Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) - +- Writer2007: Added support for background and border color transparency in Text Box element [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555) + ### Bug fixes - Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668) diff --git a/src/PhpWord/Writer/Word2007/Element/TextBox.php b/src/PhpWord/Writer/Word2007/Element/TextBox.php index e005480802..79011d8d6f 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBox.php @@ -51,6 +51,13 @@ public function write(): void if ($style->getBgColor()) { $xmlWriter->writeAttribute('fillcolor', $style->getBgColor()); + } else { + $xmlWriter->writeAttribute('filled', 'f'); + } + + if (!$style->getBorderColor()) { + $xmlWriter->writeAttribute('stroked', 'f'); + $xmlWriter->writeAttribute('strokecolor', 'white'); } $styleWriter->write(); diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TextBoxTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TextBoxTest.php new file mode 100644 index 0000000000..6d4df65298 --- /dev/null +++ b/tests/PhpWordTests/Writer/Word2007/Element/TextBoxTest.php @@ -0,0 +1,84 @@ +setBgColor($bgColor); + } + + if ($borderColor !== null) { + $style->setBorderColor($borderColor); + } + + $textBoxElement = new TextBoxElement($style); + $textBox = new TextBox($xmlWriter, $textBoxElement); + + // Act + $textBox->write(); + $output = $xmlWriter->getData(); + + // Assert + self::assertStringContainsString($expectedFillColorAttribute, $output, 'Background color should be applied.'); + self::assertStringContainsString($expectedBorderColorAttribute, $output, 'Border color should be applied correctly.'); + } + + /** + * Data provider for testing different combinations of background and border colors. + */ + public static function textBoxColorProvider(): array + { + return [ + // Case 1: Background color set, border color set + 'With both colors' => [ + '#FF0000', + '#000000', + 'fillcolor="#FF0000"', + 'stroke color="#000000"', + ], + // Case 2: Background color set, no border color + 'With background only' => [ + '#00FF00', + null, + 'fillcolor="#00FF00"', + 'stroked="f" strokecolor="white"', + ], + // Case 3: No background color, border color set + 'With border only' => [ + null, + '#123456', + 'filled="f"', + 'stroke color="#123456"', + ], + // Case 4: Neither background nor border color set + 'Without any colors' => [ + null, + null, + 'filled="f"', + 'stroked="f" strokecolor="white"', + ], + ]; + } +}