Skip to content

Add support for parsing w:sym tags in Word2007 reader #2805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ protected function readRunChild(XMLReader $xmlReader, DOMElement $node, Abstract
$parent->addTextBreak();
} elseif ($node->nodeName == 'w:tab') {
$parent->addText("\t");
} elseif ($node->nodeName == 'w:sym') {
// Symbol
$font = $xmlReader->getAttribute('w:font', $node);
$char = $xmlReader->getAttribute('w:char', $node);
$textContent = "[{$font},{$char}]";
$parent->addText($textContent, $fontStyle, $paragraphStyle);
} elseif ($node->nodeName == 'mc:AlternateContent') {
if ($node->hasChildNodes()) {
// Get fallback instead of mc:Choice to make sure it is compatible
Expand Down
62 changes: 62 additions & 0 deletions tests/PhpWordTests/Reader/Word2007/PartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,66 @@ public function testReadHeadingWithOverriddenStyle(): void
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
self::assertEquals(' but with parts not in bold', $text->getText());
}

public function testReadSymbol(): void
{
$documentXml = '<w:p>
<w:r>
<w:t>Text before symbol </w:t>
</w:r>
<w:r>
<w:sym w:font="Wingdings 2" w:char="00A3"/>
</w:r>
<w:r>
<w:t> text after symbol</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>Another symbol: </w:t>
</w:r>
<w:r>
<w:sym w:font="Wingdings 2" w:char="0052"/>
</w:r>
</w:p>';

$phpWord = $this->getDocumentFromString(['document' => $documentXml]);

$elements = $phpWord->getSection(0)->getElements();

// Test first paragraph with symbol
self::assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
/** @var \PhpOffice\PhpWord\Element\TextRun $textRun1 */
$textRun1 = $elements[0];

/** @var \PhpOffice\PhpWord\Element\Text $text1 */
$text1 = $textRun1->getElement(0);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text1);
self::assertEquals('Text before symbol ', $text1->getText());

/** @var \PhpOffice\PhpWord\Element\Text $symbol1 */
$symbol1 = $textRun1->getElement(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $symbol1);
self::assertEquals('[Wingdings 2,00A3]', $symbol1->getText());

/** @var \PhpOffice\PhpWord\Element\Text $text2 */
$text2 = $textRun1->getElement(2);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text2);
self::assertEquals(' text after symbol', $text2->getText());

// Test second paragraph with different symbol
self::assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[1]);
/** @var \PhpOffice\PhpWord\Element\TextRun $textRun2 */
$textRun2 = $elements[1];

/** @var \PhpOffice\PhpWord\Element\Text $text3 */
$text3 = $textRun2->getElement(0);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text3);
self::assertEquals('Another symbol: ', $text3->getText());

/** @var \PhpOffice\PhpWord\Element\Text $symbol2 */
$symbol2 = $textRun2->getElement(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $symbol2);
self::assertEquals('[Wingdings 2,0052]', $symbol2->getText());
}
}