Skip to content

Commit

Permalink
Background and border color transparency support for textboxes when c…
Browse files Browse the repository at this point in the history
…olor is undefined (PHPOffice#2555)
  • Loading branch information
Chudy20007 authored Feb 12, 2025
1 parent 66f0a2c commit 2a524e2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/PhpWord/Writer/Word2007/Element/TextBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
84 changes: 84 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Element/TextBoxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpWordTests\Writer\Word2007\Element;

use PhpOffice\PhpWord\Element\TextBox as TextBoxElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
use PhpOffice\PhpWord\Writer\Word2007\Element\TextBox;
use PHPUnit\Framework\TestCase;

class TextBoxTest extends TestCase
{
/**
* @dataProvider textBoxColorProvider
*/
public function testTextBoxGeneratesCorrectXml(
?string $bgColor,
?string $borderColor,
string $expectedFillColorAttribute,
string $expectedBorderColorAttribute
): void {
// Arrange
$xmlWriter = new XMLWriter();
$style = new TextBoxStyle();

if ($bgColor !== null) {
$style->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"',
],
];
}
}

0 comments on commit 2a524e2

Please sign in to comment.