Skip to content

Commit 7cc0a7c

Browse files
Handle empty documents slightly better
Previously, we’d crash because the documentElement is null in this case.
1 parent ae91851 commit 7cc0a7c

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/Component.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ private function parseHtml( $html ) {
9595
* @throws Exception
9696
*/
9797
private function getRootNode( DOMDocument $document ) {
98+
if ( $document->documentElement === null ) {
99+
throw new Exception( 'Empty document' );
100+
}
101+
98102
$rootNodes = $document->documentElement->childNodes->item( 0 )->childNodes;
99103

100104
if ( $rootNodes->length > 1 ) {

tests/php/TemplatingTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public function testJustASingleEmptyHtmlElement() {
1717
$this->assertSame( '<div></div>', $result );
1818
}
1919

20+
public function testTemplateHasNoRootNodes_ThrowsAnException(): void {
21+
$this->expectException( Exception::class );
22+
$this->createAndRender( '', [] );
23+
}
24+
2025
public function testTemplateHasTwoRootNodes_ThrowsAnException() {
2126
$this->expectException( Exception::class );
2227
$this->createAndRender( '<p></p><p></p>', [] );

0 commit comments

Comments
 (0)