Skip to content

Commit

Permalink
Merge branch 'master' into wordunimplemented2
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman authored Feb 12, 2025
2 parents 60dbd8c + 2a524e2 commit da21a96
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 2 deletions.
4 changes: 3 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 All @@ -27,6 +28,7 @@
- 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)
- 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)
- 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)
- 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)

### Miscellaneous

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro)
*/
protected static function ensureUtf8Encoded($subject)
{
return $subject ? Text::toUTF8($subject) : '';
return (null !== $subject) ? Text::toUTF8($subject) : '';
}

/**
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
97 changes: 97 additions & 0 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1641,4 +1641,101 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
$templateProcessor->setUpdateFields(false);
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
}

public function testEnsureUtf8Encoded(): void
{
$mainPart = '<w:tbl>
<w:tr>
<w:tc>
<w:tcPr>
<w:vMerge w:val="restart"/>
</w:tcPr>
<w:p>
<w:r>
<w:t t=1>${stringZero}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=2>${intZero}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=3>${stringTest}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=4>${null}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=5>${floatZero}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=6>${intTen}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=7>${boolFalse}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t t=8>${boolTrue}</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>';
$templateProcessor = new TestableTemplateProcesor($mainPart);

self::assertEquals(
['stringZero', 'intZero', 'stringTest', 'null', 'floatZero', 'intTen', 'boolFalse', 'boolTrue'],
$templateProcessor->getVariables()
);

$templateProcessor->setValue('stringZero', '0');
self::assertStringContainsString('<w:t t=1>0</w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('intZero', 0);
self::assertStringContainsString('<w:t t=2>0</w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('stringTest', 'test');
self::assertStringContainsString('<w:t t=3>test</w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('null', null);
self::assertStringContainsString('<w:t t=4></w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('floatZero', 0.00);
self::assertStringContainsString('<w:t t=5>0</w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('intTen', 10);
self::assertStringContainsString('<w:t t=6>10</w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('boolFalse', false);
self::assertStringContainsString('<w:t t=7></w:t>', $templateProcessor->getMainPart());

$templateProcessor->setValue('boolTrue', true);
self::assertStringContainsString('<w:t t=8>1</w:t>', $templateProcessor->getMainPart());
}
}
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 da21a96

Please sign in to comment.