Skip to content

Commit 91a8dd3

Browse files
committed
add parsing of p:br and add unit test
1 parent 9cd5ab7 commit 91a8dd3

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function setRels($value)
9393
*
9494
* @param \PhpOffice\Common\XMLReader $xmlReader
9595
* @param \DOMElement $domNode
96-
* @param mixed $parent
96+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
9797
* @param string $docPart
9898
*
9999
* @todo Get font style for preserve text
@@ -177,7 +177,7 @@ protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, $pa
177177
*
178178
* @param \PhpOffice\Common\XMLReader $xmlReader
179179
* @param \DOMElement $domNode
180-
* @param mixed $parent
180+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
181181
* @param string $docPart
182182
* @param mixed $paragraphStyle
183183
*
@@ -226,7 +226,11 @@ protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, $parent,
226226
$textContent = "<Object: {$target}>";
227227
$parent->addText($textContent, $fontStyle, $paragraphStyle);
228228
}
229-
} else {
229+
}
230+
if ($xmlReader->elementExists('w:br', $domNode)) {
231+
$parent->addTextBreak();
232+
}
233+
if ($xmlReader->elementExists('w:t', $domNode)) {
230234
// TextRun
231235
$textContent = $xmlReader->getValue('w:t', $domNode);
232236
$parent->addText($textContent, $fontStyle, $paragraphStyle);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Reader\Word2007;
19+
20+
use PhpOffice\PhpWord\AbstractTestReader;
21+
22+
/**
23+
* Test class for PhpOffice\PhpWord\Reader\Word2007\Element subnamespace
24+
*/
25+
class ElementTest extends AbstractTestReader
26+
{
27+
/**
28+
* Test reading of textbreak
29+
*/
30+
public function testReadTextBreak()
31+
{
32+
$documentXml = '<w:p>
33+
<w:r>
34+
<w:br/>
35+
<w:t xml:space="preserve">test string</w:t>
36+
</w:r>
37+
</w:p>';
38+
39+
$phpWord = $this->getDocumentFromString($documentXml);
40+
41+
$elements = $this->get($phpWord->getSections(), 0)->getElements();
42+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextBreak', $elements[0]);
43+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $elements[1]);
44+
$this->assertEquals('test string', $elements[1]->getText());
45+
}
46+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord;
19+
20+
use PhpOffice\PhpWord\Reader\Word2007\Document;
21+
22+
/**
23+
* Base class for Word2007 reader tests
24+
*/
25+
abstract class AbstractTestReader extends \PHPUnit\Framework\TestCase
26+
{
27+
/**
28+
* Builds a PhpWord instance based on the xml passed
29+
*
30+
* @param string $documentXml
31+
* @return \PhpOffice\PhpWord\PhpWord
32+
*/
33+
protected function getDocumentFromString($documentXml)
34+
{
35+
$phpWord = new PhpWord();
36+
$file = __DIR__ . '/../_files/temp.docx';
37+
$zip = new \ZipArchive();
38+
$zip->open($file, \ZipArchive::CREATE);
39+
$zip->addFromString('document.xml', '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>' . $documentXml . '</w:body></w:document>');
40+
$zip->close();
41+
$documentReader = new Document($file, 'document.xml');
42+
$documentReader->read($phpWord);
43+
unlink($file);
44+
45+
return $phpWord;
46+
}
47+
48+
/**
49+
* Returns the element at position $index in the array
50+
*
51+
* @param array $array
52+
* @param number $index
53+
* @return mixed
54+
*/
55+
protected function get(array $array, $index = 0)
56+
{
57+
return $array[$index];
58+
}
59+
}

0 commit comments

Comments
 (0)