Skip to content

Commit a006aa8

Browse files
authored
Merge pull request #2289 from Progi1984/htmlBlockPageBreak
HTML Reader : Style page-break-after in paragraph
2 parents 4dc34d4 + 2de5516 commit a006aa8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,16 @@ protected static function parseChildNodes($node, $element, $styles, $data)
257257
* @param \DOMNode $node
258258
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
259259
* @param array &$styles
260-
* @return \PhpOffice\PhpWord\Element\TextRun
260+
* @return \PhpOffice\PhpWord\Element\TextRun|\PhpOffice\PhpWord\Element\PageBreak
261261
*/
262262
protected static function parseParagraph($node, $element, &$styles)
263263
{
264264
$styles['paragraph'] = self::recursiveParseStylesInHierarchy($node, $styles['paragraph']);
265-
$newElement = $element->addTextRun($styles['paragraph']);
265+
if (isset($styles['paragraph']['isPageBreak']) && $styles['paragraph']['isPageBreak']) {
266+
return $element->addPageBreak();
267+
}
266268

267-
return $newElement;
269+
return $element->addTextRun($styles['paragraph']);
268270
}
269271

270272
/**
@@ -771,6 +773,11 @@ protected static function parseStyle($attribute, $styles)
771773
$styles['valign'] = self::mapAlignVertical($matches[0]);
772774
}
773775
break;
776+
case 'page-break-after':
777+
if ($cValue == 'always') {
778+
$styles['isPageBreak'] = true;
779+
}
780+
break;
774781
}
775782
}
776783

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,20 @@ public function testParseParagraphAndSpanStyle()
291291
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u', 'w:val'));
292292
}
293293

294+
/**
295+
* Test parsing paragraph with `page-break-after` style
296+
*/
297+
public function testParseParagraphWithPageBreak()
298+
{
299+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
300+
$section = $phpWord->addSection();
301+
Html::addHtml($section, '<p style="page-break-after:always;"></p>');
302+
303+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
304+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:br'));
305+
$this->assertEquals('page', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:br', 'w:type'));
306+
}
307+
294308
/**
295309
* Test parsing table
296310
*/
@@ -776,7 +790,7 @@ public function testParseCellspacingRowBgColor()
776790
/**
777791
* Parse horizontal rule
778792
*/
779-
public function testParseHorizRule()
793+
public function testParseHorizontalRule()
780794
{
781795
$phpWord = new \PhpOffice\PhpWord\PhpWord();
782796
$section = $phpWord->addSection();

0 commit comments

Comments
 (0)