Skip to content

Commit c391b90

Browse files
authored
Merge pull request #1924 from sven-ahrens/feature/parse-shapes
Feature Request: Read shape node values
2 parents 06c8248 + 166a136 commit c391b90

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
292292
$parent->addTextBreak();
293293
} elseif ($node->nodeName == 'w:tab') {
294294
$parent->addText("\t");
295+
} elseif ($node->nodeName == 'mc:AlternateContent') {
296+
if ($node->hasChildNodes()) {
297+
// Get fallback instead of mc:Choice to make sure it is compatible
298+
$fallbackElements = $node->getElementsByTagName('Fallback');
299+
300+
if ($fallbackElements->length) {
301+
$fallback = $fallbackElements->item(0);
302+
// TextRun
303+
$textContent = htmlspecialchars($fallback->nodeValue, ENT_QUOTES, 'UTF-8');
304+
305+
$parent->addText($textContent, $fontStyle, $paragraphStyle);
306+
}
307+
}
295308
} elseif ($node->nodeName == 'w:t' || $node->nodeName == 'w:delText') {
296309
// TextRun
297310
$textContent = htmlspecialchars($xmlReader->getValue('.', $node), ENT_QUOTES, 'UTF-8');

tests/PhpWord/Reader/Word2007/ElementTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,46 @@
2525
*/
2626
class ElementTest extends AbstractTestReader
2727
{
28+
/**
29+
* Test reading of alternate content value
30+
*/
31+
public function testReadAlternateContent()
32+
{
33+
$documentXml = '<w:p>
34+
<w:r>
35+
<mc:AlternateContent>
36+
<mc:Choice Requires="wps"></mc:Choice>
37+
<mc:Fallback>
38+
<w:pict>
39+
<v:rect>
40+
<v:textbox>
41+
<w:txbxContent>
42+
<w:p>
43+
<w:pPr>
44+
<w:jc w:val="center"/>
45+
</w:pPr>
46+
<w:r>
47+
<w:t>Test node value</w:t>
48+
</w:r>
49+
</w:p>
50+
</w:txbxContent>
51+
</v:textbox>
52+
</v:rect>
53+
</w:pict>
54+
</mc:Fallback>
55+
</mc:AlternateContent>
56+
</w:r>
57+
</w:p>';
58+
59+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
60+
61+
$elements = $phpWord->getSection(0)->getElements();
62+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
63+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $elements[0]->getElement(0));
64+
$text = $elements[0];
65+
$this->assertEquals('Test node value', trim($text->getElement(0)->getText()));
66+
}
67+
2868
/**
2969
* Test reading of textbreak
3070
*/

tests/PhpWord/_includes/AbstractTestReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class AbstractTestReader extends \PHPUnit\Framework\TestCase
2424
{
2525
private $parts = array(
2626
'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>'),
27-
'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>'),
27+
'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>'),
2828
'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>'),
2929
'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>'),
3030
'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>'),

0 commit comments

Comments
 (0)