mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-72139 core_message: Fix encoding UTF-8 in prevent unclosed tags
This commit is contained in:
parent
c357779722
commit
7a6d65eedb
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user