Perform preprocessing on the message contents before diffing in EditHistory
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 16 May 2023 12:21:31 +0000 (14:21 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 16 May 2023 12:24:47 +0000 (14:24 +0200)
The insertion of additional newlines by `EditHistory::formatHtml()` should make
the Diff more useful by giving the diff algorithms more lines to work with.

see #3955

wcfsetup/install/files/lib/page/EditHistoryPage.class.php

index e90b0f2f786405458a7fba079ba0f8cff5b83aaa..00dbf26845b2d0ea2712d60b17b2af7834cf19c0 100644 (file)
@@ -179,8 +179,8 @@ class EditHistoryPage extends AbstractPage
 
         // valid IDs were given, calculate diff
         if ($this->old && $this->new) {
-            $a = \explode("\n", StringUtil::unifyNewlines(StringUtil::trim($this->old->getMessage())));
-            $b = \explode("\n", StringUtil::unifyNewlines(StringUtil::trim($this->new->getMessage())));
+            $a = \explode("\n", $this->prepareMessage($this->old->getMessage()));
+            $b = \explode("\n", $this->prepareMessage($this->new->getMessage()));
             $this->diff = Diff::rawDiffFromSebastianDiff($differ->diffToArray($a, $b));
 
             // create word diff for small changes (only one consecutive paragraph modified)
@@ -227,6 +227,24 @@ class EditHistoryPage extends AbstractPage
         }
     }
 
+    private function prepareMessage(string $message): string
+    {
+        $message = $this->formatHtml($message);
+        $message = StringUtil::trim($message);
+        $message = StringUtil::unifyNewlines($message);
+
+        return \preg_replace('/\n{2,}/', "\n", $message);
+    }
+
+    private function formatHtml(string $html): string
+    {
+        $bothTags = ['ol', 'pre', 'table', 'tr', 'ul', 'woltlab-quote', 'woltlab-spoiler'];
+        $openingTag = \implode('|', ['br', ...$bothTags]);
+        $closingTag = \implode('|', ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'li', 'td', 'th', ...$bothTags]);
+
+        return \preg_replace("/(<(?:{$openingTag})>|<\\/(?:{$closingTag})>)/", "\\0\n", $html);
+    }
+
     /**
      * @inheritDoc
      */