Skip to content

Commit da21a96

Browse files
authored
Merge branch 'master' into wordunimplemented2
2 parents 60dbd8c + 2a524e2 commit da21a96

File tree

5 files changed

+192
-2
lines changed

5 files changed

+192
-2
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 3 additions & 1 deletion
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)
@@ -27,6 +28,7 @@
2728
- Reader Word2007: Support Header elements within Title elements by [@SpraxDev](https://github.com/SpraxDev) fixing [#2616](https://github.com/PHPOffice/PHPWord/issues/2616), [#2426](https://github.com/PHPOffice/PHPWord/issues/2426) in [#2674](https://github.com/PHPOffice/PHPWord/pull/2674)
2829
- Reader HTML: Support for inherit value for property line-height by [@Progi1984](https://github.com/Progi1984) fixing [#2683](https://github.com/PHPOffice/PHPWord/issues/2683) in [#2733](https://github.com/PHPOffice/PHPWord/pull/2733)
2930
- Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738)
31+
- Template Processor: Fix 0 considered as empty string by [@cavasinf](https://github.com/cavasinf), [@SnipsMine](https://github.com/SnipsMine) and [@Progi1984](https://github.com/Progi1984) fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572), [#2703](https://github.com/PHPOffice/PHPWord/issues/2703) in [#2748](https://github.com/PHPOffice/PHPWord/pull/2748)
3032

3133
### Miscellaneous
3234

src/PhpWord/TemplateProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro)
269269
*/
270270
protected static function ensureUtf8Encoded($subject)
271271
{
272-
return $subject ? Text::toUTF8($subject) : '';
272+
return (null !== $subject) ? Text::toUTF8($subject) : '';
273273
}
274274

275275
/**

src/PhpWord/Writer/Word2007/Element/TextBox.php

Lines changed: 7 additions & 0 deletions
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();

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,4 +1641,101 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
16411641
$templateProcessor->setUpdateFields(false);
16421642
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
16431643
}
1644+
1645+
public function testEnsureUtf8Encoded(): void
1646+
{
1647+
$mainPart = '<w:tbl>
1648+
<w:tr>
1649+
<w:tc>
1650+
<w:tcPr>
1651+
<w:vMerge w:val="restart"/>
1652+
</w:tcPr>
1653+
<w:p>
1654+
<w:r>
1655+
<w:t t=1>${stringZero}</w:t>
1656+
</w:r>
1657+
</w:p>
1658+
</w:tc>
1659+
<w:tc>
1660+
<w:p>
1661+
<w:r>
1662+
<w:t t=2>${intZero}</w:t>
1663+
</w:r>
1664+
</w:p>
1665+
</w:tc>
1666+
<w:tc>
1667+
<w:p>
1668+
<w:r>
1669+
<w:t t=3>${stringTest}</w:t>
1670+
</w:r>
1671+
</w:p>
1672+
</w:tc>
1673+
<w:tc>
1674+
<w:p>
1675+
<w:r>
1676+
<w:t t=4>${null}</w:t>
1677+
</w:r>
1678+
</w:p>
1679+
</w:tc>
1680+
<w:tc>
1681+
<w:p>
1682+
<w:r>
1683+
<w:t t=5>${floatZero}</w:t>
1684+
</w:r>
1685+
</w:p>
1686+
</w:tc>
1687+
<w:tc>
1688+
<w:p>
1689+
<w:r>
1690+
<w:t t=6>${intTen}</w:t>
1691+
</w:r>
1692+
</w:p>
1693+
</w:tc>
1694+
<w:tc>
1695+
<w:p>
1696+
<w:r>
1697+
<w:t t=7>${boolFalse}</w:t>
1698+
</w:r>
1699+
</w:p>
1700+
</w:tc>
1701+
<w:tc>
1702+
<w:p>
1703+
<w:r>
1704+
<w:t t=8>${boolTrue}</w:t>
1705+
</w:r>
1706+
</w:p>
1707+
</w:tc>
1708+
</w:tr>
1709+
</w:tbl>';
1710+
$templateProcessor = new TestableTemplateProcesor($mainPart);
1711+
1712+
self::assertEquals(
1713+
['stringZero', 'intZero', 'stringTest', 'null', 'floatZero', 'intTen', 'boolFalse', 'boolTrue'],
1714+
$templateProcessor->getVariables()
1715+
);
1716+
1717+
$templateProcessor->setValue('stringZero', '0');
1718+
self::assertStringContainsString('<w:t t=1>0</w:t>', $templateProcessor->getMainPart());
1719+
1720+
$templateProcessor->setValue('intZero', 0);
1721+
self::assertStringContainsString('<w:t t=2>0</w:t>', $templateProcessor->getMainPart());
1722+
1723+
$templateProcessor->setValue('stringTest', 'test');
1724+
self::assertStringContainsString('<w:t t=3>test</w:t>', $templateProcessor->getMainPart());
1725+
1726+
$templateProcessor->setValue('null', null);
1727+
self::assertStringContainsString('<w:t t=4></w:t>', $templateProcessor->getMainPart());
1728+
1729+
$templateProcessor->setValue('floatZero', 0.00);
1730+
self::assertStringContainsString('<w:t t=5>0</w:t>', $templateProcessor->getMainPart());
1731+
1732+
$templateProcessor->setValue('intTen', 10);
1733+
self::assertStringContainsString('<w:t t=6>10</w:t>', $templateProcessor->getMainPart());
1734+
1735+
$templateProcessor->setValue('boolFalse', false);
1736+
self::assertStringContainsString('<w:t t=7></w:t>', $templateProcessor->getMainPart());
1737+
1738+
$templateProcessor->setValue('boolTrue', true);
1739+
self::assertStringContainsString('<w:t t=8>1</w:t>', $templateProcessor->getMainPart());
1740+
}
16441741
}
Lines changed: 84 additions & 0 deletions
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+
public static 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)