MDL-79360 filter: fix nolink tag regression from MDL-77525

This commit is contained in:
Petr Skoda 2023-09-14 18:44:18 +02:00
parent 5058c46813
commit e42eeb62b0
2 changed files with 42 additions and 2 deletions

View File

@ -228,8 +228,10 @@ class filter_manager {
public function filter_text($text, $context, array $options = array(),
array $skipfilters = null) {
$text = $this->apply_filter_chain($text, $this->get_text_filters($context), $options, $skipfilters);
// Remove <nolink> tags for XHTML compatibility.
$text = str_replace(array('<nolink>', '</nolink>'), '', $text);
if (!isset($options['stage']) || $options['stage'] === 'post_clean') {
// Remove <nolink> tags for XHTML compatibility after the last filtering stage.
$text = str_replace(array('<nolink>', '</nolink>'), '', $text);
}
return $text;
}

View File

@ -19,6 +19,8 @@ namespace core;
/**
* Unit tests for format_text defined in weblib.php.
*
* @covers ::format_text
*
* @package core
* @category test
* @copyright 2015 The Open University
@ -88,6 +90,42 @@ class weblib_format_text_test extends \advanced_testcase {
format_text('<p>:-)</p>', FORMAT_MOODLE, array('filter' => false)));
}
/**
* Make sure that nolink tags and spans prevent linking in filters that support it.
*/
public function test_format_text_nolink() {
global $CFG;
$this->resetAfterTest();
filter_set_global_state('activitynames', TEXTFILTER_ON);
$course = $this->getDataGenerator()->create_course();
$context = \context_course::instance($course->id);
$page = $this->getDataGenerator()->create_module('page',
['course' => $course->id, 'name' => 'Test 1']);
$cm = get_coursemodule_from_instance('page', $page->id, $page->course, false, MUST_EXIST);
$pageurl = $CFG->wwwroot. '/mod/page/view.php?id=' . $cm->id;
$this->assertSame(
'<p>Read <a class="autolink" title="Test 1" href="' . $pageurl . '">Test 1</a>.</p>',
format_text('<p>Read Test 1.</p>', FORMAT_HTML, ['context' => $context]));
$this->assertSame(
'<p>Read <a class="autolink" title="Test 1" href="' . $pageurl . '">Test 1</a>.</p>',
format_text('<p>Read Test 1.</p>', FORMAT_HTML, ['context' => $context, 'noclean' => true]));
$this->assertSame(
'<p>Read Test 1.</p>',
format_text('<p><nolink>Read Test 1.</nolink></p>', FORMAT_HTML, ['context' => $context, 'noclean' => false]));
$this->assertSame(
'<p>Read Test 1.</p>',
format_text('<p><nolink>Read Test 1.</nolink></p>', FORMAT_HTML, ['context' => $context, 'noclean' => true]));
$this->assertSame(
'<p><span class="nolink">Read Test 1.</span></p>',
format_text('<p><span class="nolink">Read Test 1.</span></p>', FORMAT_HTML, ['context' => $context]));
}
public function test_format_text_overflowdiv() {
$this->assertEquals('<div class="no-overflow"><p>Hello world</p></div>',
format_text('<p>Hello world</p>', FORMAT_HTML, array('overflowdiv' => true)));