Skip to content

Commit 1904d0e

Browse files
CopilotProgi1984
andcommitted
Implement w:sym tag support in Word2007 reader
Co-authored-by: Progi1984 <1533248+Progi1984@users.noreply.github.com>
1 parent a52e12d commit 1904d0e

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,12 @@ protected function readRunChild(XMLReader $xmlReader, DOMElement $node, Abstract
551551
$parent->addTextBreak();
552552
} elseif ($node->nodeName == 'w:tab') {
553553
$parent->addText("\t");
554+
} elseif ($node->nodeName == 'w:sym') {
555+
// Symbol
556+
$font = $xmlReader->getAttribute('w:font', $node);
557+
$char = $xmlReader->getAttribute('w:char', $node);
558+
$textContent = "[{$font},{$char}]";
559+
$parent->addText($textContent, $fontStyle, $paragraphStyle);
554560
} elseif ($node->nodeName == 'mc:AlternateContent') {
555561
if ($node->hasChildNodes()) {
556562
// Get fallback instead of mc:Choice to make sure it is compatible

tests/PhpWordTests/Reader/Word2007/PartTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,66 @@ public function testReadHeadingWithOverriddenStyle(): void
233233
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
234234
self::assertEquals(' but with parts not in bold', $text->getText());
235235
}
236+
237+
public function testReadSymbol(): void
238+
{
239+
$documentXml = '<w:p>
240+
<w:r>
241+
<w:t>Text before symbol </w:t>
242+
</w:r>
243+
<w:r>
244+
<w:sym w:font="Wingdings 2" w:char="00A3"/>
245+
</w:r>
246+
<w:r>
247+
<w:t> text after symbol</w:t>
248+
</w:r>
249+
</w:p>
250+
<w:p>
251+
<w:r>
252+
<w:t>Another symbol: </w:t>
253+
</w:r>
254+
<w:r>
255+
<w:sym w:font="Wingdings 2" w:char="0052"/>
256+
</w:r>
257+
</w:p>';
258+
259+
$phpWord = $this->getDocumentFromString(['document' => $documentXml]);
260+
261+
$elements = $phpWord->getSection(0)->getElements();
262+
263+
// Test first paragraph with symbol
264+
self::assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
265+
/** @var \PhpOffice\PhpWord\Element\TextRun $textRun1 */
266+
$textRun1 = $elements[0];
267+
268+
/** @var \PhpOffice\PhpWord\Element\Text $text1 */
269+
$text1 = $textRun1->getElement(0);
270+
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text1);
271+
self::assertEquals('Text before symbol ', $text1->getText());
272+
273+
/** @var \PhpOffice\PhpWord\Element\Text $symbol1 */
274+
$symbol1 = $textRun1->getElement(1);
275+
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $symbol1);
276+
self::assertEquals('[Wingdings 2,00A3]', $symbol1->getText());
277+
278+
/** @var \PhpOffice\PhpWord\Element\Text $text2 */
279+
$text2 = $textRun1->getElement(2);
280+
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text2);
281+
self::assertEquals(' text after symbol', $text2->getText());
282+
283+
// Test second paragraph with different symbol
284+
self::assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[1]);
285+
/** @var \PhpOffice\PhpWord\Element\TextRun $textRun2 */
286+
$textRun2 = $elements[1];
287+
288+
/** @var \PhpOffice\PhpWord\Element\Text $text3 */
289+
$text3 = $textRun2->getElement(0);
290+
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text3);
291+
self::assertEquals('Another symbol: ', $text3->getText());
292+
293+
/** @var \PhpOffice\PhpWord\Element\Text $symbol2 */
294+
$symbol2 = $textRun2->getElement(1);
295+
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $symbol2);
296+
self::assertEquals('[Wingdings 2,0052]', $symbol2->getText());
297+
}
236298
}

0 commit comments

Comments
 (0)