Skip to content

Commit 88bbe49

Browse files
authored
Merge pull request #1217 from troosan/html_br
Added support for linebreaks <br/> in Shared\Html::addHtml()
2 parents d4c6427 + 905029a commit 88bbe49

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ This is the last version to support PHP 5.3
1818
- Support for Comments - @troosan #1067
1919
- Support for paragraph textAlignment - @troosan #1165
2020
- Add support for HTML underline tag <u> in addHtml - @zNightFalLz #1186
21-
- Allow to change cell width unit - @guillaume-ro-fr #986
21+
- Add support for HTML <br> in addHtml - @anrikun @troosan #659
22+
- Allow to change cell width unit - guillaume-ro-fr #986
2223
- Allow to change the line height rule @troosan
2324
- Implement PageBreak for odt writer @cookiekiller #863 #824
2425
- Allow to force an update of all fields on opening a document - @troosan #951

src/PhpWord/Shared/Html.php

+11
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
136136
'ul' => array('List', null, null, $styles, $data, 3, null),
137137
'ol' => array('List', null, null, $styles, $data, 7, null),
138138
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
139+
'br' => array('LineBreak', null, $element, $styles, null, null, null),
139140
);
140141

141142
$newElement = null;
@@ -523,4 +524,14 @@ private static function mapBorderStyle($cssBorderStyle)
523524
return 'single';
524525
}
525526
}
527+
528+
/**
529+
* Parse line break
530+
*
531+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
532+
*/
533+
private static function parseLineBreak($element)
534+
{
535+
$element->addTextBreak();
536+
}
526537
}

tests/PhpWord/Shared/HtmlTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,22 @@ public function testParseList()
234234
$this->assertEquals('list item1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->nodeValue);
235235
$this->assertEquals('list item2', $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:t')->nodeValue);
236236
}
237+
238+
/**
239+
* Tests parsing of br
240+
*/
241+
public function testParseLineBreak()
242+
{
243+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
244+
$section = $phpWord->addSection();
245+
$html = '<p>This is some text<br/>with a linebreak.</p>';
246+
Html::addHtml($section, $html);
247+
248+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
249+
250+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:br'));
251+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
252+
$this->assertEquals('This is some text', $doc->getElement('/w:document/w:body/w:p/w:r[1]/w:t')->nodeValue);
253+
$this->assertEquals('with a linebreak.', $doc->getElement('/w:document/w:body/w:p/w:r[2]/w:t')->nodeValue);
254+
}
237255
}

0 commit comments

Comments
 (0)