Skip to content

Commit

Permalink
Merge pull request #101 from dpushkarev/preserve-libxml-errors
Browse files Browse the repository at this point in the history
Preserve libxml errors after failed parsing
  • Loading branch information
darylldoyle authored Jun 18, 2024
2 parents f9460a4 + 9648e15 commit e95cd17
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public function sanitize($dirty)

// If we couldn't parse the XML then we go no further. Reset and return false
if (!$loaded) {
$this->xmlIssues = self::getXmlErrors();
$this->resetAfter();
return false;
}
Expand Down Expand Up @@ -698,4 +699,21 @@ protected function cleanUnsafeNodes(\DOMNode $currentElement) {
}
}
}

/**
* Retrieve array of errors
* @return array
*/
private static function getXmlErrors()
{
$errors = [];
foreach (libxml_get_errors() as $error) {
$errors[] = [
'message' => trim($error->message),
'line' => $error->line,
];
}

return $errors;
}
}
15 changes: 14 additions & 1 deletion tests/SanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ public function testBadXMLReturnsFalse()
$sanitizer = new Sanitizer();
$cleanData = $sanitizer->sanitize($initialData);

self::assertSame(false, $cleanData);
self::assertFalse($cleanData);
self::assertEquals(
[
[
'message' => 'Opening and ending tag mismatch: line line 8 and svg',
'line' => 15,
],
[
'message' => 'Premature end of data in tag svg line 4',
'line' => 16,
],
],
$sanitizer->getXmlIssues()
);
}

/**
Expand Down

0 comments on commit e95cd17

Please sign in to comment.