Skip to content

Add parsing of Shape node values #1924

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

Merged
merged 6 commits into from
Jan 12, 2021
Merged
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
13 changes: 13 additions & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
$parent->addTextBreak();
} elseif ($node->nodeName == 'w:tab') {
$parent->addText("\t");
} elseif ($node->nodeName == 'mc:AlternateContent') {
if ($node->hasChildNodes()) {
// Get fallback instead of mc:Choice to make sure it is compatible
$fallbackElements = $node->getElementsByTagName('Fallback');

if ($fallbackElements->length) {
$fallback = $fallbackElements->item(0);
// TextRun
$textContent = htmlspecialchars($fallback->nodeValue, ENT_QUOTES, 'UTF-8');

$parent->addText($textContent, $fontStyle, $paragraphStyle);
}
}
} elseif ($node->nodeName == 'w:t' || $node->nodeName == 'w:delText') {
// TextRun
$textContent = htmlspecialchars($xmlReader->getValue('.', $node), ENT_QUOTES, 'UTF-8');
Expand Down
40 changes: 40 additions & 0 deletions tests/PhpWord/Reader/Word2007/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,46 @@
*/
class ElementTest extends AbstractTestReader
{
/**
* Test reading of alternate content value
*/
public function testReadAlternateContent()
{
$documentXml = '<w:p>
<w:r>
<mc:AlternateContent>
<mc:Choice Requires="wps"></mc:Choice>
<mc:Fallback>
<w:pict>
<v:rect>
<v:textbox>
<w:txbxContent>
<w:p>
<w:pPr>
<w:jc w:val="center"/>
</w:pPr>
<w:r>
<w:t>Test node value</w:t>
</w:r>
</w:p>
</w:txbxContent>
</v:textbox>
</v:rect>
</w:pict>
</mc:Fallback>
</mc:AlternateContent>
</w:r>
</w:p>';

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

$elements = $phpWord->getSection(0)->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $elements[0]->getElement(0));
$text = $elements[0];
$this->assertEquals('Test node value', trim($text->getElement(0)->getText()));
}

/**
* Test reading of textbreak
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpWord/_includes/AbstractTestReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AbstractTestReader extends \PHPUnit\Framework\TestCase
{
private $parts = array(
'styles' => array('class' => 'PhpOffice\PhpWord\Reader\Word2007\Styles', 'xml' => '<w:styles xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:docDefaults><w:rPrDefault><w:rPr><w:sz w:val="24"/></w:rPr></w:rPrDefault></w:docDefaults>{toReplace}</w:styles>'),
'document' => array('class' => 'PhpOffice\PhpWord\Reader\Word2007\Document', 'xml' => '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>{toReplace}</w:body></w:document>'),
'document' => array('class' => 'PhpOffice\PhpWord\Reader\Word2007\Document', 'xml' => '<w:document xmlns:v="urn:schemas-microsoft-com:vml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>{toReplace}</w:body></w:document>'),
'footnotes' => array('class' => 'PhpOffice\PhpWord\Reader\Word2007\Footnotes', 'xml' => '<w:footnotes xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">{toReplace}</w:footnotes>'),
'endnotes' => array('class' => 'PhpOffice\PhpWord\Reader\Word2007\Endnotes', 'xml' => '<w:endnotes xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">{toReplace}</w:endnotes>'),
'settings' => array('class' => 'PhpOffice\PhpWord\Reader\Word2007\Settings', 'xml' => '<w:comments xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">{toReplace}</w:comments>'),
Expand Down