From 0f343644be2d9099cdea1aac8c265e82c8e64065 Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Thu, 2 Feb 2023 15:49:35 +0100
Subject: [PATCH] check format in `purifyFlexibleInput()`, fixes #180

---
 exercises/Exercise.php               |  6 ++++--
 js/vips.js                           | 16 +++++++---------
 lib/vips_common.inc.php              |  6 +-----
 views/exercises/rh_exercise/edit.php |  2 +-
 4 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/exercises/Exercise.php b/exercises/Exercise.php
index 83049da..1bdbc3f 100644
--- a/exercises/Exercise.php
+++ b/exercises/Exercise.php
@@ -126,10 +126,12 @@ abstract class Exercise extends SimpleORMap
     public static function purifyFlexibleInput($html)
     {
         if (Studip\Markup::isHtml($html)) {
-            if (preg_match('/<.*</s', $html)) {
+            $text = Studip\Markup::removeHtml($html);
+
+            if (preg_match('/<.*</s', $html) || kill_format($text) !== $text) {
                 $html = Studip\Markup::purifyHtml($html);
             } else {
-                $html = Studip\Markup::removeHtml($html);
+                $html = $text;
             }
         }
 
diff --git a/js/vips.js b/js/vips.js
index 35addc9..ab9887a 100644
--- a/js/vips.js
+++ b/js/vips.js
@@ -167,15 +167,13 @@ $(function() {
         var toggle = $(this).closest('.size_toggle');
         var items  = toggle.find('.character_input');
 
-        for (var i = 0; i < items.length; i += 2) {
-            var name = items[i].name;
-            items[i].name = items[i + 1].name;
-            items[i + 1].name = name;
-
-            var value = items[i].value;
-            items[i].value = items[i + 1].value;
-            items[i + 1].value = value;
-        }
+        var name = items[0].name;
+        items[0].name = items[1].name;
+        items[1].name = name;
+
+        var value = items[0].value;
+        items[0].value = items[1].value;
+        items[1].value = value;
 
         toggle.toggleClass('size_large').toggleClass('size_small');
         event.preventDefault();
diff --git a/lib/vips_common.inc.php b/lib/vips_common.inc.php
index fa6d626..27e9a66 100644
--- a/lib/vips_common.inc.php
+++ b/lib/vips_common.inc.php
@@ -426,11 +426,7 @@ function check_exercise_access($exercise_id, $assignment_id)
  */
 function vips_flexible_input_size($text)
 {
-    if (strpos($text, "\n") !== false || Studip\Markup::isHtml($text) || kill_format($text) !== $text) {
-        return 'large';
-    }
-
-    return 'small';
+    return strpos($text, "\n") === false ? 'small' : 'large';
 }
 
 /**
diff --git a/views/exercises/rh_exercise/edit.php b/views/exercises/rh_exercise/edit.php
index 46ee23c..0614455 100644
--- a/views/exercises/rh_exercise/edit.php
+++ b/views/exercises/rh_exercise/edit.php
@@ -15,7 +15,7 @@
         </tr>
     </thead>
 
-    <tbody class="dynamic_list">
+    <tbody class="dynamic_list" style="vertical-align: top;">
         <? foreach ($exercise->task['groups'] as $i => $group): ?>
             <? $size = vips_flexible_input_size($group) ?>
 
-- 
GitLab