From 5028ef36ada615c49a459f44dadb98318c1a94d9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 16 May 2023 14:21:31 +0200 Subject: [PATCH] Perform preprocessing on the message contents before diffing in EditHistory 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 --- .../files/lib/page/EditHistoryPage.class.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/page/EditHistoryPage.class.php b/wcfsetup/install/files/lib/page/EditHistoryPage.class.php index e90b0f2f78..00dbf26845 100644 --- a/wcfsetup/install/files/lib/page/EditHistoryPage.class.php +++ b/wcfsetup/install/files/lib/page/EditHistoryPage.class.php @@ -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 */ -- 2.20.1