Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/HtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<?xml encoding="utf-8" ?>' . $html, LIBXML_NOBLANKS ) ) {
//TODO Test failure
throw new Exception( 'Failed to parse HTML' );
}

/** @var LibXMLError[] $errors */
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/php/HtmlParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ public function testTwoRootNodes() {
$this->parseAndGetRootNode( '<p></p><p></p>' );
}

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

}