Skip to content

Commit 3961854

Browse files
committed
Improve Coverage
1 parent 21711d1 commit 3961854

File tree

5 files changed

+40
-115
lines changed

5 files changed

+40
-115
lines changed

src/PhpWord/Shared/Converter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ public static function angleToDegree($angle = 1)
340340
Color::YELLOW => 'FFFF00',
341341
];
342342

343+
public static function validStringColor(string $value): bool
344+
{
345+
return (self::STRING_TO_RGB[$value] ?? null) !== null;
346+
}
347+
343348
/**
344349
* Convert colorname as string to RGB.
345350
*

src/PhpWord/Writer/HTML/Element/PreserveText.php

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,13 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\HTML\Element;
2020

21-
use PhpOffice\PhpWord\Style;
22-
use PhpOffice\PhpWord\Style\Font;
23-
use PhpOffice\PhpWord\Style\Paragraph;
2421
use PhpOffice\PhpWord\Writer\HTML;
25-
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
26-
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
2722

2823
/**
2924
* PreserveText element HTML writer. Issue 2188.
3025
*/
31-
class PreserveText extends AbstractElement
26+
class PreserveText extends Text
3227
{
33-
/**
34-
* Opening tags.
35-
*
36-
* @var string
37-
*/
38-
private $openingTags = '';
39-
40-
/**
41-
* Closing tag.
42-
*
43-
* @var string
44-
*/
45-
private $closingTags = '';
46-
4728
/**
4829
* Write text.
4930
*
@@ -106,91 +87,4 @@ protected function writeClosing()
10687

10788
return $content;
10889
}
109-
110-
/**
111-
* Write paragraph style.
112-
*
113-
* @return string
114-
*/
115-
private function getParagraphStyle()
116-
{
117-
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
118-
$element = $this->element;
119-
$style = '';
120-
if (!method_exists($element, 'getParagraphStyle')) {
121-
return $style;
122-
}
123-
124-
$paragraphStyle = $element->getParagraphStyle();
125-
$pStyleIsObject = ($paragraphStyle instanceof Paragraph);
126-
if ($pStyleIsObject) {
127-
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
128-
$styleWriter->setParentWriter($this->parentWriter);
129-
$style = $styleWriter->write();
130-
} elseif (is_string($paragraphStyle)) {
131-
$style = $paragraphStyle;
132-
}
133-
if ($style) {
134-
$attribute = $pStyleIsObject ? 'style' : 'class';
135-
$style = " {$attribute}=\"{$style}\"";
136-
}
137-
138-
return $style;
139-
}
140-
141-
/**
142-
* Get font style.
143-
*/
144-
private function processFontStyle(): void
145-
{
146-
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
147-
$element = $this->element;
148-
149-
$attributeStyle = $attributeLang = '';
150-
$lang = null;
151-
152-
$fontStyle = $element->getFontStyle();
153-
if ($fontStyle instanceof Font) {
154-
// Attribute style
155-
$styleWriter = new FontStyleWriter($fontStyle);
156-
$fontCSS = $styleWriter->write();
157-
if ($fontCSS) {
158-
$attributeStyle = ' style="' . $fontCSS . '"';
159-
} else {
160-
$className = $fontStyle->getStyleName();
161-
if ($className) {
162-
$attributeStyle = ' class="' . $className . '"';
163-
}
164-
}
165-
// Attribute Lang
166-
$lang = $fontStyle->getLang();
167-
} elseif (!empty($fontStyle)) {
168-
// Attribute class
169-
$attributeStyle = ' class="' . $fontStyle . '"';
170-
// Attribute Lang
171-
/** @var Font $cssClassStyle */
172-
$cssClassStyle = Style::getStyle($fontStyle);
173-
if ($cssClassStyle !== null && method_exists($cssClassStyle, 'getLang')) {
174-
$lang = $cssClassStyle->getLang();
175-
}
176-
}
177-
178-
if ($lang) {
179-
$attributeLang = $lang->getLatin();
180-
if (!$attributeLang) {
181-
$attributeLang = $lang->getEastAsia();
182-
}
183-
if (!$attributeLang) {
184-
$attributeLang = $lang->getBidirectional();
185-
}
186-
if ($attributeLang) {
187-
$attributeLang = " lang='$attributeLang'";
188-
}
189-
}
190-
191-
if ($attributeStyle || $attributeLang) {
192-
$this->openingTags = "<span$attributeLang$attributeStyle>";
193-
$this->closingTags = '</span>';
194-
}
195-
}
19690
}

src/PhpWord/Writer/HTML/Element/Text.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\HTML\Element;
2020

21+
use PhpOffice\PhpWord\Element\PreserveText as PreserveTextElement;
22+
use PhpOffice\PhpWord\Element\Text as TextElement;
2123
use PhpOffice\PhpWord\Element\TrackChange;
2224
use PhpOffice\PhpWord\Style;
2325
use PhpOffice\PhpWord\Style\Font;
@@ -52,14 +54,14 @@ class Text extends AbstractElement
5254
*
5355
* @var string
5456
*/
55-
private $openingTags = '';
57+
protected $openingTags = '';
5658

5759
/**
5860
* Closing tag.
5961
*
6062
* @var string
6163
*/
62-
private $closingTags = '';
64+
protected $closingTags = '';
6365

6466
/**
6567
* Write text.
@@ -70,7 +72,7 @@ public function write()
7072
{
7173
$this->processFontStyle();
7274

73-
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
75+
/** @var TextElement */
7476
$element = $this->element;
7577

7678
$text = $this->parentWriter->escapeHTML($element->getText() ?? '');
@@ -213,9 +215,9 @@ private function writeTrackChangeClosing()
213215
*
214216
* @return string
215217
*/
216-
private function getParagraphStyle()
218+
protected function getParagraphStyle()
217219
{
218-
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
220+
/** @var PreserveTextElement|TextElement */
219221
$element = $this->element;
220222
$style = '';
221223
if (!method_exists($element, 'getParagraphStyle')) {
@@ -242,9 +244,9 @@ private function getParagraphStyle()
242244
/**
243245
* Get font style.
244246
*/
245-
private function processFontStyle(): void
247+
protected function processFontStyle(): void
246248
{
247-
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
249+
/** @var PreserveTextElement|TextElement */
248250
$element = $this->element;
249251

250252
$attributeStyle = $attributeLang = '';

src/PhpWord/Writer/HTML/Style/Font.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\HTML\Style;
2020

21+
use PhpOffice\PhpWord\Shared\Converter;
2122
use PhpOffice\PhpWord\Style\Font as FontStyle;
2223

2324
/**
@@ -69,7 +70,9 @@ public function write()
6970

7071
$css['font-family'] = $this->getValueIf(!empty($font), $font);
7172
$css['font-size'] = $this->getValueIf($size !== null, "{$size}pt");
72-
$css['color'] = $this->getValueIf($color !== null, "#{$color}");
73+
if ($color !== null) {
74+
$css['color'] = Converter::validStringColor($color) ? $color : "#$color";
75+
}
7376
$css['background'] = $this->getValueIf($fgColor != '', $fgColor);
7477
$css['font-weight'] = $this->getValueIf($style->isBold(), 'bold');
7578
$css['font-style'] = $this->getValueIf($style->isItalic(), 'italic');

tests/PhpWordTests/Writer/HTML/Element/PreserveTextTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,25 @@ public function testPreserveText(): void
3939
$expected = "<p>$text2</p>";
4040
self::assertStringContainsString($expected, $content);
4141
}
42+
43+
public function testPreserveTextStyle(): void
44+
{
45+
$phpWord = new PhpWord();
46+
$section = $phpWord->addSection();
47+
$text1 = 'This text is missing in the HTML output.';
48+
$fontStyle = ['color' => 'red'];
49+
$section->addPreserveText($text1, $fontStyle);
50+
$paragraphStyle = ['align' => 'center'];
51+
$text2 = 'Likewise page {PAGE} of {NUMPAGES}';
52+
$section->addPreserveText($text2, null, $paragraphStyle);
53+
$section->addPreserveText('');
54+
$writer = new HTML($phpWord);
55+
$content = $writer->getContent();
56+
$expected = "<p><span style=\"color: red;\">$text1</span></p>";
57+
self::assertStringContainsString($expected, $content);
58+
$expected = "<p style=\"text-align: center;\">$text2</p>";
59+
self::assertStringContainsString($expected, $content);
60+
$expected = '<p>&nbsp;</p>';
61+
self::assertStringContainsString($expected, $content);
62+
}
4263
}

0 commit comments

Comments
 (0)