From 2549f46f7ca9f78f3b0384f0dc6a4d83a14fba94 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Wed, 8 Jun 2016 15:55:56 +0800 Subject: [PATCH] MDL-54853 weblib: Properly specify encoding for blanktarget --- lib/tests/weblib_format_text_test.php | 71 ++++++++++++++++----------- lib/weblib.php | 5 +- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/lib/tests/weblib_format_text_test.php b/lib/tests/weblib_format_text_test.php index a7aa3bfdfe5ae..d0be09fc93949 100644 --- a/lib/tests/weblib_format_text_test.php +++ b/lib/tests/weblib_format_text_test.php @@ -115,36 +115,47 @@ public function test_format_text_blanktarget($link, $expected) { */ public function format_text_blanktarget_testcases() { return [ - 'Simple link' => - [ - 'Hey, that\'s pretty good!', - '
Hey, that\'s pretty good!
' - ], - 'Link with rel' => - [ - 'Hey, that\'s pretty good!', - '
Hey, that\'s pretty good!
' - ], - 'Link with rel noreferrer' => - [ - 'Hey, that\'s pretty good!', - '
Hey, that\'s pretty good!
' - ], - 'Link with target' => - [ - 'Hey, that\'s pretty good!', - '
' . - 'Hey, that\'s pretty good!
' - ], - 'Link with target blank' => - [ - 'Hey, that\'s pretty good!', - '
Hey, that\'s pretty good!
' - ] + 'Simple link' => [ + 'Hey, that\'s pretty good!', + '
Hey, that\'s pretty good!
' + ], + 'Link with rel' => [ + 'Hey, that\'s pretty good!', + '
Hey, that\'s pretty good!
' + ], + 'Link with rel noreferrer' => [ + 'Hey, that\'s pretty good!', + '
Hey, that\'s pretty good!
' + ], + 'Link with target' => [ + 'Hey, that\'s pretty good!', + '
' . + 'Hey, that\'s pretty good!
' + ], + 'Link with target blank' => [ + 'Hey, that\'s pretty good!', + '
Hey, that\'s pretty good!
' + ], + 'Link with Frank\'s casket inscription' => [ + 'ᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻ' . + 'ᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ', + '
ᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾ' . + 'ᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ
' + ], + 'No link' => [ + 'Some very boring text written with the Latin script', + '
Some very boring text written with the Latin script
' + ], + 'No link with Thror\'s map runes' => [ + 'ᛋᛏᚫᚾᛞ ᛒᚣ ᚦᛖ ᚷᚱᛖᚣ ᛋᛏᚩᚾᛖ ᚻᚹᛁᛚᛖ ᚦᛖ ᚦᚱᚢᛋᚻ ᚾᚩᚳᛋ ᚫᚾᛞ ᚦᛖ ᛋᛖᛏᛏᛁᚾᚷ ᛋᚢᚾ ᚹᛁᚦ ᚦᛖ ᛚᚫᛋᛏ ᛚᛁᚷᚻᛏ ᚩᚠ ᛞᚢᚱᛁᚾᛋ ᛞᚫᚣ ᚹᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ', + '
ᛋᛏᚫᚾᛞ ᛒᚣ ᚦᛖ ᚷᚱᛖᚣ ᛋᛏᚩᚾᛖ ᚻᚹᛁᛚᛖ ᚦᛖ ᚦᚱᚢᛋᚻ ᚾᚩᚳᛋ ᚫᚾᛞ ᚦᛖ ᛋᛖᛏᛏᛁᚾᚷ ᛋᚢᚾ ᚹᛁᚦ ᚦᛖ ᛚᚫᛋᛏ ᛚᛁᚷᚻᛏ ᚩᚠ ᛞᚢᚱᛁᚾᛋ ᛞᚫᚣ ᚹ' . + 'ᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ
' + ] ]; } } diff --git a/lib/weblib.php b/lib/weblib.php index e094828447f7c..1e24072e04788 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1320,8 +1320,9 @@ function format_text($text, $format = FORMAT_MOODLE, $options = null, $courseidd } if ($options['blanktarget']) { + $encoding = ini_get('default_charset') ? ini_get('default_charset') : 'utf-8'; $domdoc = new DOMDocument(); - $domdoc->loadHTML($text); + $domdoc->loadHTML('' . $text); foreach ($domdoc->getElementsByTagName('a') as $link) { if ($link->hasAttribute('target') && strpos($link->getAttribute('target'), '_blank') === false) { continue; @@ -1336,7 +1337,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = null, $courseidd // $domdoc->loadHTML($text, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); however it seems like the libxml // version that travis uses doesn't work properly and ends up leaving , so I'm forced to use // this regex to remove those tags. - $text = trim(preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $domdoc->saveHTML())); + $text = trim(preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $domdoc->saveHTML($domdoc->documentElement))); } return $text;