diff --git a/src/HtmlParser.php b/src/HtmlParser.php index acd9e89..4a88634 100644 --- a/src/HtmlParser.php +++ b/src/HtmlParser.php @@ -28,7 +28,7 @@ public function parseHtml( string $html ): DOMDocument { // LIBXML_NOBLANKS Constant excludes "ghost nodes" to avoid violating // vue's single root node constraint if ( !$document->loadHTML( '' . $html, LIBXML_NOBLANKS ) ) { - //TODO Test failure + throw new Exception( 'Failed to parse HTML' ); } /** @var LibXMLError[] $errors */ @@ -41,9 +41,15 @@ public function parseHtml( string $html ): DOMDocument { libxml_disable_entity_loader( $entityLoaderDisabled ); } + $exception = null; foreach ( $errors as $error ) { - //TODO html5 tags can fail parsing - //TODO Throw an exception + if ( strpos( $error->message, 'Tag template invalid' ) === 0 ) { + continue; + } + $exception = new Exception( $error->message, $error->code, $exception ); + } + if ( $exception !== null ) { + throw $exception; } return $document; diff --git a/tests/php/HtmlParserTest.php b/tests/php/HtmlParserTest.php index af824b4..02e43a1 100644 --- a/tests/php/HtmlParserTest.php +++ b/tests/php/HtmlParserTest.php @@ -63,4 +63,11 @@ public function testTwoRootNodes() { $this->parseAndGetRootNode( '

' ); } + public function testMalformedHtml(): void { + $htmlParser = new HtmlParser(); + $this->expectException( Exception::class ); + $this->expectExceptionMessage( 'Unexpected end tag' ); + $htmlParser->parseHtml( '

' ); + } + }