Skip to content

Commit b868c33

Browse files
authored
Merge pull request #1277 from troosan/fix_parsing_of_textbreaks
Fix parsing of textbreaks
2 parents 3f40c5e + 874c6d6 commit b868c33

File tree

4 files changed

+116
-6
lines changed

4 files changed

+116
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ v0.15.0 (?? ??? 2018)
1212
- Add support for Track changes @Cip @troosan #354 #1262
1313

1414
### Fixed
15-
- fix reading of docx default style - @troosan #1238
16-
- fix the size unit of when parsing html images - @troosan #1254
17-
- fixed HTML parsing of nested lists - @troosan #1265
15+
- Fix reading of docx default style - @troosan #1238
16+
- Fix the size unit of when parsing html images - @troosan #1254
17+
- Fixed HTML parsing of nested lists - @troosan #1265
1818
- Save PNG alpha information when using remote images. @samsullivan #779
19+
- fix parsing of `<w:br/>` tag. @troosan #1274
1920

2021

2122

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function setRels($value)
9494
*
9595
* @param \PhpOffice\Common\XMLReader $xmlReader
9696
* @param \DOMElement $domNode
97-
* @param mixed $parent
97+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
9898
* @param string $docPart
9999
*
100100
* @todo Get font style for preserve text
@@ -180,7 +180,7 @@ protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, $pa
180180
*
181181
* @param \PhpOffice\Common\XMLReader $xmlReader
182182
* @param \DOMElement $domNode
183-
* @param mixed $parent
183+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
184184
* @param string $docPart
185185
* @param mixed $paragraphStyle
186186
*
@@ -236,7 +236,11 @@ protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, $parent,
236236
$textContent = "&lt;Object: {$target}>";
237237
$parent->addText($textContent, $fontStyle, $paragraphStyle);
238238
}
239-
} else {
239+
}
240+
if ($xmlReader->elementExists('w:br', $domNode)) {
241+
$parent->addTextBreak();
242+
}
243+
if ($xmlReader->elementExists('w:t', $domNode)) {
240244
// TextRun
241245
if ($domNode->parentNode->nodeName == 'w:del') {
242246
$textContent = $xmlReader->getValue('w:delText', $domNode);
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)