Skip to content

Commit

Permalink
MDL-79360 filter: fix nolink tag regression from MDL-77525
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 15, 2023
1 parent 5058c46 commit e42eeb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/filterlib.php
Expand Up @@ -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 <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;
}

Expand Down
38 changes: 38 additions & 0 deletions lib/tests/weblib_format_text_test.php
Expand Up @@ -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
Expand Down Expand Up @@ -88,6 +90,42 @@ public function test_format_text_format_moodle_no_filters() {
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)));
Expand Down

0 comments on commit e42eeb6

Please sign in to comment.