From ecd9256fdc0ba2af8de5bc83b217575f7effd618 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms Date: Sat, 1 Apr 2023 06:51:20 +0000 Subject: preserve link breaks and adjust tests, fixes #2278 Closes #2278 Merge request studip/studip!1680 --- lib/classes/Markup.class.php | 7 +++ tests/unit/lib/classes/MarkupClassTest.php | 73 +++++++++++++++++------------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lib/classes/Markup.class.php b/lib/classes/Markup.class.php index 21f6e63..f329a92 100644 --- a/lib/classes/Markup.class.php +++ b/lib/classes/Markup.class.php @@ -487,6 +487,13 @@ class Markup $config->set('HTML.Allowed', 'a[href],img[alt|src],br'); $config->set('AutoFormat.Custom', ['Unlinkify']); + $html = str_replace('', '
', $html); + $html = str_replace('', '
', $html); + $html = str_replace('', '
', $html); + $html = str_replace('', '
', $html); + $html = str_replace('

', '



', $html); + $html = str_replace('', '

', $html); + $purifier = new \HTMLPurifier($config); $html = $purifier->purify($html); diff --git a/tests/unit/lib/classes/MarkupClassTest.php b/tests/unit/lib/classes/MarkupClassTest.php index 9ab835f..982d155 100644 --- a/tests/unit/lib/classes/MarkupClassTest.php +++ b/tests/unit/lib/classes/MarkupClassTest.php @@ -50,40 +50,14 @@ class Seminar_Session */ class MarkupClassTest extends \Codeception\Test\Unit { - public function testRemoveHTML() + /** + * @dataProvider removeProvider + */ + public function testRemoveHTML(string $input, string $expected): void { Config::set(new Config(['WYSIWYG' => true])); - foreach ([ - 'plain text' => 'plain text', - '

paragraph only

' => 'paragraph only', - - 'no href' => 'no href', - '' => '', - 'empty href' => 'empty href', - '' => '[ href%20only ]', - '' => '[ href%20end-tag ]', - 'and text' => '[ http://href.de ]and text', - 'before and text after' - => 'before [ http://href.de ]and text after', - - 'no src' => 'no src', - '' => '[ src%20only ]', - '' => '[ src%20end-tag ]', - 'and text' => '[ http://src.de ]and text', - 'before and text after' - => 'before [ http://src.de ]and text after', - - // some "real" urls - 'Example' - => '[ https://example.org/ ]Example', - '' - => '[ https://example.org/image.png ]', - '

link Example-Domain and picture

' - => 'link [ http://example.org ]Example-Domain and picture [ https://example.org/image.png ]' - ] as $in => $out) { - $this->assertEquals($out, Studip\Markup::removeHtml(Studip\Markup::markAsHtml($in))); - } + $this->assertEquals($expected, Studip\Markup::removeHtml(Studip\Markup::markAsHtml($input))); } public function testGetMediaUrl() @@ -223,4 +197,41 @@ class MarkupClassTest extends \Codeception\Test\Unit } } } + + public static function removeProvider(): array + { + return [ + 'plain text' => ['plain text', 'plain text'], + 'paragraph only' => ['

paragraph only

', 'paragraph only'], + + 'link: no href' => ['no href', 'no href'], + 'link: empty' => ['', ''], + 'link: empty href' => ['empty href', 'empty href'], + 'link: href only' => ['', '[ href%20only ]'], + 'link: href end-tag' => ['', '[ href%20end-tag ]'], + 'link: href and text' => ['and text', '[ http://href.de ]and text'], + 'link: before and text after' => ['before and text after', 'before [ http://href.de ]and text after'], + + 'image: no src' => ['no src', 'no src'], + 'image: src only' => ['', '[ src%20only ]'], + 'image: src end-tag' => ['', '[ src%20end-tag ]'], + 'image: src and text' => ['and text', '[ http://src.de ]and text'], + 'image: before and text after' => ['before and text after', 'before [ http://src.de ]and text after'], + + // some "real" urls + 'real link' => ['Example', '[ https://example.org/ ]Example'], + 'real image' => ['', '[ https://example.org/image.png ]'], + 'real link and image' => [ + '

link Example-Domain and picture

', + 'link [ http://example.org ]Example-Domain and picture [ https://example.org/image.png ]', + ], + + // Line breaks + 'html: ul' => [\Studip\Markup::HTML_MARKER . '

3

', "1\n2\n\n3"], + 'html: ol' => [\Studip\Markup::HTML_MARKER . '
  1. 1
  2. 2

3

', "1\n2\n\n3"], + 'html: br' => [\Studip\Markup::HTML_MARKER . '1
2
3', "1\n2\n3"], + 'html: div' => [\Studip\Markup::HTML_MARKER . '
1
2
3
', "1\n\n2\n\n3"], + 'html: p' => [\Studip\Markup::HTML_MARKER . '

1

2

3

', "1\n\n2\n\n3"], + ]; + } } -- cgit v1.0