Skip to content

Commit cf4a027

Browse files
committed
Background and border color transparency support for textboxes when color is undefined
1 parent 2f4da6e commit cf4a027

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Diff for: docs/changes/2.x/2.0.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Enhancements
66

77
- IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515)
8+
- TextBox: Added support for background and border color transparency [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555)
89

910
### Bug fixes
1011

Diff for: src/PhpWord/Writer/Word2007/Element/TextBox.php

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

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

5562
$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)