From: Tim Düsterhus Date: Tue, 16 May 2023 12:21:31 +0000 (+0200) Subject: Perform preprocessing on the message contents before diffing in EditHistory X-Git-Tag: 6.0.0_Alpha_1~91^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=5028ef36ada615c49a459f44dadb98318c1a94d9;p=GitHub%2FWoltLab%2FWCF.git 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 --- 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 */