Skip to content

Commit 3c61539

Browse files
committed
Background and border color transparency support for textboxes when color is undefined
1 parent 66f0a2c commit 3c61539

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

Diff for: docs/changes/1.x/1.4.0.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
- 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)
1818
- 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)
1919
- 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)
20-
20+
- 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)
21+
2122
### Bug fixes
2223

2324
- 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 for: src/PhpWord/Writer/Word2007/Element/TextBox.php

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public function write(): void
5151

5252
if ($style->getBgColor()) {
5353
$xmlWriter->writeAttribute('fillcolor', $style->getBgColor());
54+
} else {
55+
$xmlWriter->writeAttribute('filled', 'f');
56+
}
57+
58+
if (!$style->getBorderColor()) {
59+
$xmlWriter->writeAttribute('stroked', 'f');
60+
$xmlWriter->writeAttribute('strokecolor', 'white');
5461
}
5562

5663
$styleWriter->write();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpWordTests\Writer\Word2007\Element;
6+
7+
use PhpOffice\PhpWord\Element\TextBox as TextBoxElement;
8+
use PhpOffice\PhpWord\Shared\XMLWriter;
9+
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
10+
use PhpOffice\PhpWord\Writer\Word2007\Element\TextBox;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class TextBoxTest extends TestCase
14+
{
15+
/**
16+
* @dataProvider textBoxColorProvider
17+
*/
18+
public function testTextBoxGeneratesCorrectXml(
19+
?string $bgColor,
20+
?string $borderColor,
21+
string $expectedFillColorAttribute,
22+
string $expectedBorderColorAttribute
23+
): void {
24+
// Arrange
25+
$xmlWriter = new XMLWriter();
26+
$style = new TextBoxStyle();
27+
28+
if ($bgColor !== null) {
29+
$style->setBgColor($bgColor);
30+
}
31+
32+
if ($borderColor !== null) {
33+
$style->setBorderColor($borderColor);
34+
}
35+
36+
$textBoxElement = new TextBoxElement($style);
37+
$textBox = new TextBox($xmlWriter, $textBoxElement);
38+
39+
// Act
40+
$textBox->write();
41+
$output = $xmlWriter->getData();
42+
43+
// Assert
44+
self::assertStringContainsString($expectedFillColorAttribute, $output, 'Background color should be applied.');
45+
self::assertStringContainsString($expectedBorderColorAttribute, $output, 'Border color should be applied correctly.');
46+
}
47+
48+
/**
49+
* Data provider for testing different combinations of background and border colors.
50+
*/
51+
protected function textBoxColorProvider(): array
52+
{
53+
return [
54+
// Case 1: Background color set, border color set
55+
'With both colors' => [
56+
'#FF0000',
57+
'#000000',
58+
'fillcolor="#FF0000"',
59+
'stroke color="#000000"',
60+
],
61+
// Case 2: Background color set, no border color
62+
'With background only' => [
63+
'#00FF00',
64+
null,
65+
'fillcolor="#00FF00"',
66+
'stroked="f" strokecolor="white"',
67+
],
68+
// Case 3: No background color, border color set
69+
'With border only' => [
70+
null,
71+
'#123456',
72+
'filled="f"',
73+
'stroke color="#123456"',
74+
],
75+
// Case 4: Neither background nor border color set
76+
'Without any colors' => [
77+
null,
78+
null,
79+
'filled="f"',
80+
'stroked="f" strokecolor="white"',
81+
],
82+
];
83+
}
84+
}

0 commit comments

Comments
 (0)