Skip to content
Open
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
22 changes: 13 additions & 9 deletions src/HtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,25 @@

private function createDOMDocument(string $html): \DOMDocument
{
$document = new \DOMDocument();
$document = new \DOMDocument();
$previousLibxmlErrorState = null;

if ($this->getConfig()->getOption('suppress_errors')) {
// Suppress conversion errors (from http://bit.ly/pCCRSX)
\libxml_use_internal_errors(true);
$previousLibxmlErrorState = \libxml_use_internal_errors(true);
}

// Hack to load utf-8 HTML (from http://bit.ly/pVDyCt)
$document->loadHTML('<?xml encoding="UTF-8">' . $html);
$document->encoding = 'UTF-8';
try {
// Hack to load utf-8 HTML (from http://bit.ly/pVDyCt)
$document->loadHTML('<?xml encoding="UTF-8">' . $html);
$document->encoding = 'UTF-8';

$this->replaceMisplacedComments($document);

if ($this->getConfig()->getOption('suppress_errors')) {
\libxml_clear_errors();
$this->replaceMisplacedComments($document);
} finally {
if ($this->getConfig()->getOption('suppress_errors')) {
\libxml_clear_errors();
\libxml_use_internal_errors($previousLibxmlErrorState);

Check failure on line 130 in src/HtmlConverter.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyNullArgument

src/HtmlConverter.php:130:45: PossiblyNullArgument: Argument 1 of libxml_use_internal_errors cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 130 in src/HtmlConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $use_errors of function libxml_use_internal_errors expects bool, bool|null given.
}
}

return $document;
Expand Down
28 changes: 28 additions & 0 deletions tests/HtmlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,34 @@ public function testMalformedHtml(): void
$this->assertHtmlGivesMarkdown('<strong><em>Strong italic</strong> Regular text', '***Strong italic*** Regular text'); // Missing closing </em>
}

public function testLibxmlInternalErrorsIsRestoredAfterConvert(): void
{
$previousState = \libxml_use_internal_errors(false);

try {
$converter = new HtmlConverter();
$converter->convert('<p>Hello</p>');

$this->assertFalse(\libxml_use_internal_errors());
} finally {
\libxml_use_internal_errors($previousState);
}
}

public function testLibxmlInternalErrorsIsUnchangedWhenSuppressErrorsDisabled(): void
{
$previousState = \libxml_use_internal_errors(true);

try {
$converter = new HtmlConverter(['suppress_errors' => false]);
$converter->convert('<p>Hello</p>');

$this->assertTrue(\libxml_use_internal_errors());
} finally {
\libxml_use_internal_errors($previousState);
}
}

public function testHtml5TagsArePreserved(): void
{
$this->assertHtmlGivesMarkdown('<article>Some stuff</article>', '<article>Some stuff</article>');
Expand Down
Loading