MDL-72139 core_message: Fix encoding UTF-8 in prevent unclosed tags

This commit is contained in:
cescobedo 2021-07-13 11:05:32 +02:00
parent c357779722
commit 7a6d65eedb
2 changed files with 16 additions and 11 deletions

View File

@ -690,18 +690,20 @@ class helper {
public static function prevent_unclosed_html_tags(
string $message,
bool $removebody = false
) : string
{
$html = '';
if (!empty($message)) {
$doc = new DOMDocument();
@$doc->loadHTML($message);
$html = $doc->getElementsByTagName('body')->item(0)->C14N(false, true);
if ($removebody) {
// Remove <body> element added in C14N function.
$html = preg_replace('~<(/?(?:body))[^>]*>\s*~i', '', $html);
}
) : string {
$html = '';
if (!empty($message)) {
$doc = new DOMDocument();
$olderror = libxml_use_internal_errors(true);
$doc->loadHTML('<?xml version="1.0" encoding="UTF-8" ?>' . $message);
libxml_clear_errors();
libxml_use_internal_errors($olderror);
$html = $doc->getElementsByTagName('body')->item(0)->C14N(false, true);
if ($removebody) {
// Remove <body> element added in C14N function.
$html = preg_replace('~<(/?(?:body))[^>]*>\s*~i', '', $html);
}
}
return $html;
}

View File

@ -214,6 +214,9 @@ class core_message_helper_testcase extends advanced_testcase {
'Empty html' => [
'', '', false
],
'Check encoding UTF-8 is working' => [
'<body><h1>Title</h1><p>السلام عليكم</p></body>', '<body><h1>Title</h1><p>السلام عليكم</p></body>', false
],
];
}
}