From e42eeb62b05b2fa48e56d922bfdad2840a8c3678 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Thu, 14 Sep 2023 18:44:18 +0200 Subject: [PATCH] MDL-79360 filter: fix nolink tag regression from MDL-77525 --- lib/filterlib.php | 6 +++-- lib/tests/weblib_format_text_test.php | 38 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/filterlib.php b/lib/filterlib.php index 58aeaff6d2a06..efdf68f8036b8 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -228,8 +228,10 @@ protected function get_string_filters($context) { 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 tags for XHTML compatibility. - $text = str_replace(array('', ''), '', $text); + if (!isset($options['stage']) || $options['stage'] === 'post_clean') { + // Remove tags for XHTML compatibility after the last filtering stage. + $text = str_replace(array('', ''), '', $text); + } return $text; } diff --git a/lib/tests/weblib_format_text_test.php b/lib/tests/weblib_format_text_test.php index 6aab808bedcda..3593dcf20bacc 100644 --- a/lib/tests/weblib_format_text_test.php +++ b/lib/tests/weblib_format_text_test.php @@ -19,6 +19,8 @@ /** * 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 @@ public function test_format_text_format_moodle_no_filters() { format_text('

:-)

', 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( + '

Read Test 1.

', + format_text('

Read Test 1.

', FORMAT_HTML, ['context' => $context])); + + $this->assertSame( + '

Read Test 1.

', + format_text('

Read Test 1.

', FORMAT_HTML, ['context' => $context, 'noclean' => true])); + + $this->assertSame( + '

Read Test 1.

', + format_text('

Read Test 1.

', FORMAT_HTML, ['context' => $context, 'noclean' => false])); + + $this->assertSame( + '

Read Test 1.

', + format_text('

Read Test 1.

', FORMAT_HTML, ['context' => $context, 'noclean' => true])); + + $this->assertSame( + '

Read Test 1.

', + format_text('

Read Test 1.

', FORMAT_HTML, ['context' => $context])); + } + public function test_format_text_overflowdiv() { $this->assertEquals('

Hello world

', format_text('

Hello world

', FORMAT_HTML, array('overflowdiv' => true)));