From a5c3cdbd3b1261b6ab7184c6929ce4b4eec8810e Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 11 Sep 2023 17:53:51 +0100
Subject: [PATCH] Issue #3386482 by alexpott, mondrake: DiffOpOutputBuilder
 does not correctly match the 10.0.x implementation and produces unexpected
 output

---
 core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php    | 6 +-----
 .../Tests/Component/Diff/DiffOpOutputBuilderTest.php      | 8 ++++++++
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php b/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php
index c872a92f50e8..72cc5805ae8a 100644
--- a/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php
+++ b/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php
@@ -61,13 +61,9 @@ public function toOpsArray(array $diff): array {
         if (count($hunkTarget) === 0) {
           $ops[] = $this->hunkOp(Differ::REMOVED, $hunkSource, $hunkTarget);
         }
-        elseif (count($hunkSource) === count($hunkTarget)) {
+        elseif (count($hunkSource) >= count($hunkTarget)) {
           $ops[] = $this->hunkOp(self::CHANGED, $hunkSource, $hunkTarget);
         }
-        elseif (count($hunkSource) > count($hunkTarget)) {
-          $ops[] = $this->hunkOp(self::CHANGED, array_slice($hunkSource, 0, count($hunkTarget)), $hunkTarget);
-          $ops[] = $this->hunkOp(Differ::REMOVED, array_slice($hunkSource, count($hunkTarget)), []);
-        }
         else {
           $ops[] = $this->hunkOp(self::CHANGED, $hunkSource, array_slice($hunkTarget, 0, count($hunkSource)));
           $ops[] = $this->hunkOp(Differ::ADDED, array_slice($hunkTarget, count($hunkSource)), []);
diff --git a/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php b/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php
index bb0f70de2e19..725e178a4742 100644
--- a/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php
+++ b/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php
@@ -65,6 +65,14 @@ public function provideTestDiff(): array {
         ['a', 'b', 'd'],
         ['a'],
       ],
+      'change-copy' => [
+        [
+          new DiffOpChange(['aa', 'bb', 'cc'], ['a', 'c']),
+          new DiffOpCopy(['d']),
+        ],
+        ['aa', 'bb', 'cc', 'd'],
+        ['a', 'c', 'd'],
+      ],
     ];
   }
 
-- 
GitLab