mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
Merge branch 'MDL-61732-master' of https://github.com/Dagefoerde/moodle
This commit is contained in:
commit
2bffba3cb6
@ -32,6 +32,22 @@ defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
class filter_emoticon extends moodle_text_filter {
|
||||
|
||||
/**
|
||||
* Internal cache used for replacing. Multidimensional array;
|
||||
* - dimension 1: language,
|
||||
* - dimension 2: theme.
|
||||
* @var array
|
||||
*/
|
||||
protected static $emoticontexts = array();
|
||||
|
||||
/**
|
||||
* Internal cache used for replacing. Multidimensional array;
|
||||
* - dimension 1: language,
|
||||
* - dimension 2: theme.
|
||||
* @var array
|
||||
*/
|
||||
protected static $emoticonimgs = array();
|
||||
|
||||
/**
|
||||
* Apply the filter to the text
|
||||
*
|
||||
@ -49,7 +65,7 @@ class filter_emoticon extends moodle_text_filter {
|
||||
return $text;
|
||||
}
|
||||
if (in_array($options['originalformat'], explode(',', get_config('filter_emoticon', 'formats')))) {
|
||||
$this->replace_emoticons($text);
|
||||
return $this->replace_emoticons($text);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
@ -62,51 +78,73 @@ class filter_emoticon extends moodle_text_filter {
|
||||
* Replace emoticons found in the text with their images
|
||||
*
|
||||
* @param string $text to modify
|
||||
* @return void
|
||||
* @return string the modified result
|
||||
*/
|
||||
protected function replace_emoticons(&$text) {
|
||||
protected function replace_emoticons($text) {
|
||||
global $CFG, $OUTPUT, $PAGE;
|
||||
static $emoticontexts = array(); // internal cache used for replacing
|
||||
static $emoticonimgs = array(); // internal cache used for replacing
|
||||
|
||||
$lang = current_language();
|
||||
$theme = $PAGE->theme->name;
|
||||
|
||||
if (!isset($emoticontexts[$lang][$theme]) or !isset($emoticonimgs[$lang][$theme])) {
|
||||
if (!isset(self::$emoticontexts[$lang][$theme]) or !isset(self::$emoticonimgs[$lang][$theme])) {
|
||||
// prepare internal caches
|
||||
$manager = get_emoticon_manager();
|
||||
$emoticons = $manager->get_emoticons();
|
||||
$emoticontexts[$lang][$theme] = array();
|
||||
$emoticonimgs[$lang][$theme] = array();
|
||||
self::$emoticontexts[$lang][$theme] = array();
|
||||
self::$emoticonimgs[$lang][$theme] = array();
|
||||
foreach ($emoticons as $emoticon) {
|
||||
$emoticontexts[$lang][$theme][] = $emoticon->text;
|
||||
$emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
|
||||
self::$emoticontexts[$lang][$theme][] = $emoticon->text;
|
||||
self::$emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
|
||||
}
|
||||
unset($emoticons);
|
||||
}
|
||||
|
||||
if (empty($emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here
|
||||
return;
|
||||
if (empty(self::$emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here.
|
||||
return $text;
|
||||
}
|
||||
|
||||
// detect all the <script> zones to take out
|
||||
$excludes = array();
|
||||
preg_match_all('/<script language(.+?)<\/script>/is', $text, $listofexcludes);
|
||||
// Detect all zones that we should not handle (including the nested tags).
|
||||
$processing = preg_split('/(<\/?(?:span|script)[^>]*>)/is', $text, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
// take out all the <script> zones from text
|
||||
foreach (array_unique($listofexcludes[0]) as $key => $value) {
|
||||
$excludes['<+'.$key.'+>'] = $value;
|
||||
}
|
||||
if ($excludes) {
|
||||
$text = str_replace($excludes, array_keys($excludes), $text);
|
||||
// Initialize the results.
|
||||
$resulthtml = "";
|
||||
$exclude = 0;
|
||||
|
||||
// Define the patterns that mark the start of the forbidden zones.
|
||||
$excludepattern = array('/^<script/is', '/^<span[^>]+class="nolink[^"]*"/is');
|
||||
|
||||
// Loop through the fragments.
|
||||
foreach ($processing as $fragment) {
|
||||
// If we are not ignoring, we MUST test if we should.
|
||||
if ($exclude == 0) {
|
||||
foreach ($excludepattern as $exp) {
|
||||
if (preg_match($exp, $fragment)) {
|
||||
$exclude = $exclude + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($exclude > 0) {
|
||||
// If we are ignoring the fragment, then we must check if we may have reached the end of the zone.
|
||||
if (strpos($fragment, '</span') !== false || strpos($fragment, '</script') !== false) {
|
||||
$exclude -= 1;
|
||||
// This is needed because of a double increment at the first element.
|
||||
if ($exclude == 1) {
|
||||
$exclude -= 1;
|
||||
}
|
||||
} else if (strpos($fragment, '<span') !== false || strpos($fragment, '<script') !== false) {
|
||||
// If we find a nested tag we increase the exclusion level.
|
||||
$exclude = $exclude + 1;
|
||||
}
|
||||
} else if (strpos($fragment, '<span') === false ||
|
||||
strpos($fragment, '</span') === false) {
|
||||
// This is the meat of the code - this is run every time.
|
||||
// This code only runs for fragments that are not ignored (including the tags themselves).
|
||||
$fragment = str_replace(self::$emoticontexts[$lang][$theme], self::$emoticonimgs[$lang][$theme], $fragment);
|
||||
}
|
||||
$resulthtml .= $fragment;
|
||||
}
|
||||
|
||||
// this is the meat of the code - this is run every time
|
||||
$text = str_replace($emoticontexts[$lang][$theme], $emoticonimgs[$lang][$theme], $text);
|
||||
|
||||
// Recover all the <script> zones to text
|
||||
if ($excludes) {
|
||||
$text = str_replace(array_keys($excludes), $excludes, $text);
|
||||
}
|
||||
return $resulthtml;
|
||||
}
|
||||
}
|
||||
|
@ -34,29 +34,124 @@ require_once($CFG->dirroot . '/filter/emoticon/filter.php'); // Include the code
|
||||
class filter_emoticon_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Verify configured target formats are observed. Just that.
|
||||
* Tests the filter doesn't affect nolink classes.
|
||||
*
|
||||
* @dataProvider filter_emoticon_provider
|
||||
*/
|
||||
public function test_filter_emoticon_formats() {
|
||||
|
||||
$this->resetAfterTest(true); // We are modifying the config.
|
||||
public function test_filter_emoticon($input, $format, $expected) {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$filter = new testable_filter_emoticon();
|
||||
$this->assertEquals($expected, $filter->filter($input, [
|
||||
'originalformat' => $format,
|
||||
]));
|
||||
}
|
||||
|
||||
// Verify texts not matching target formats aren't filtered.
|
||||
/**
|
||||
* The data provider for filter emoticon tests.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function filter_emoticon_provider() {
|
||||
$grr = '(grr)';
|
||||
return [
|
||||
'FORMAT_MOODLE is not filtered' => [
|
||||
'input' => $grr,
|
||||
'format' => FORMAT_MOODLE,
|
||||
'expected' => $grr,
|
||||
],
|
||||
'FORMAT_MARKDOWN is not filtered' => [
|
||||
'input' => $grr,
|
||||
'format' => FORMAT_MARKDOWN,
|
||||
'expected' => $grr,
|
||||
],
|
||||
'FORMAT_PLAIN is not filtered' => [
|
||||
'input' => $grr,
|
||||
'format' => FORMAT_PLAIN,
|
||||
'expected' => $grr,
|
||||
],
|
||||
'FORMAT_HTML is filtered' => [
|
||||
'input' => $grr,
|
||||
'format' => FORMAT_HTML,
|
||||
'expected' => $this->get_converted_content_for_emoticon($grr),
|
||||
],
|
||||
'Script tag should not be processed' => [
|
||||
'input' => "<script language='javascript'>alert('{$grr}');</script>",
|
||||
'format' => FORMAT_HTML,
|
||||
'expected' => "<script language='javascript'>alert('{$grr}');</script>",
|
||||
],
|
||||
'Basic nolink should not be processed' => [
|
||||
'input' => '<span class="nolink">(n)</span>',
|
||||
'format' => FORMAT_HTML,
|
||||
'expected' => '<span class="nolink">(n)</span>',
|
||||
],
|
||||
'Nested nolink should not be processed' => [
|
||||
'input' => '<span class="nolink"><span>(n)</span>(n)</span>',
|
||||
'format' => FORMAT_HTML,
|
||||
'expected' => '<span class="nolink"><span>(n)</span>(n)</span>',
|
||||
],
|
||||
'Nested nolink should not be processed but following emoticon' => [
|
||||
'input' => '<span class="nolink"><span>(n)</span>(n)</span>(n)',
|
||||
'format' => FORMAT_HTML,
|
||||
'expected' => '<span class="nolink"><span>(n)</span>(n)</span>' . $this->get_converted_content_for_emoticon('(n)'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the text for a single emoticon into the rendered value.
|
||||
*
|
||||
* @param string $text The text to translate.
|
||||
* @return string
|
||||
*/
|
||||
public function get_converted_content_for_emoticon($text) {
|
||||
global $OUTPUT;
|
||||
$manager = get_emoticon_manager();
|
||||
$emoticons = $manager->get_emoticons();
|
||||
foreach ($emoticons as $emoticon) {
|
||||
if ($emoticon->text == $text) {
|
||||
return $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
|
||||
}
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the filter doesn't break anything if activated but invalid format passed.
|
||||
*
|
||||
*/
|
||||
public function test_filter_invalidformat() {
|
||||
global $PAGE;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$filter = new testable_filter_emoticon();
|
||||
$input = '(grr)';
|
||||
$expected = '(grr)';
|
||||
$options = array('originalformat' => FORMAT_MOODLE); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}.
|
||||
$this->assertEquals($expected, $filter->filter('(grr)', $options));
|
||||
|
||||
$options = array('originalformat' => FORMAT_MARKDOWN); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}.
|
||||
$this->assertEquals($expected, $filter->filter('(grr)', $options));
|
||||
$this->assertEquals($expected, $filter->filter($input, [
|
||||
'originalformat' => 'ILLEGALFORMAT',
|
||||
]));
|
||||
}
|
||||
|
||||
$options = array('originalformat' => FORMAT_PLAIN); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}.
|
||||
$this->assertEquals($expected, $filter->filter('(grr)', $options));
|
||||
/**
|
||||
* Tests the filter doesn't break anything if activated but no emoticons available.
|
||||
*
|
||||
*/
|
||||
public function test_filter_emptyemoticons() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest();
|
||||
// Empty the emoticons array.
|
||||
$CFG->emoticons = null;
|
||||
|
||||
// And texts matching target formats are filtered.
|
||||
$expected = '<img class="icon emoticon" alt="angry" title="angry" src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/angry" />';
|
||||
$options = array('originalformat' => FORMAT_HTML); // Only FORMAT_HTML is filtered, see {@link testable_filter_emoticon}.
|
||||
$this->assertEquals($expected, $filter->filter('(grr)', $options));
|
||||
$filter = new filter_emoticon(context_system::instance(), array('originalformat' => FORMAT_HTML));
|
||||
|
||||
$input = '(grr)';
|
||||
$expected = '(grr)';
|
||||
|
||||
$this->assertEquals($expected, $filter->filter($input, [
|
||||
'originalformat' => FORMAT_HTML,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,6 +160,9 @@ class filter_emoticon_testcase extends advanced_testcase {
|
||||
*/
|
||||
class testable_filter_emoticon extends filter_emoticon {
|
||||
public function __construct() {
|
||||
// Reset static emoticon caches.
|
||||
parent::$emoticontexts = array();
|
||||
parent::$emoticonimgs = array();
|
||||
// Use this context for filtering.
|
||||
$this->context = context_system::instance();
|
||||
// Define FORMAT_HTML as only one filtering in DB.
|
||||
|
Loading…
x
Reference in New Issue
Block a user