From 31ad006cee669ab1dfbac9ab1559fad849d1e6d9 Mon Sep 17 00:00:00 2001
From: Carlos Ballena <cballenar@gmail.com>
Date: Wed, 30 Sep 2026 13:23:39 -0400
Subject: [PATCH 1/6] Issue #3627092: Inject line breaks into monolithic HTML
 strings to fix improve parsing

---
 src/DiffLayoutBase.php                        | 46 +++++++++++++++++++
 .../diff/Layout/SplitFieldsDiffLayout.php     |  5 ++
 .../diff/Layout/UnifiedFieldsDiffLayout.php   |  4 ++
 .../diff/Layout/VisualInlineDiffLayout.php    |  4 ++
 4 files changed, 59 insertions(+)

diff --git a/src/DiffLayoutBase.php b/src/DiffLayoutBase.php
index eb0492e..2a735e3 100644
--- a/src/DiffLayoutBase.php
+++ b/src/DiffLayoutBase.php
@@ -310,4 +310,50 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
     $config->save();
   }
 
+  /**
+   * Formats HTML by adding line breaks after closing block-level tags to improve diff readability.
+   *
+   * This is used to help the diff parser identify changes in content more accurately,
+   * especially when CKEditor or other editors strip whitespace. Only block-level elements
+   * are considered, so inline tags (like <a>, <span>, etc.) are not affected.
+   *
+   * Example usage:
+   * @code
+   *   $formatted = $this->formatHtmlForDiff('<div><p>Text</p><span>inline</span></div>');
+   *   // Result: "<div>\n<p>Text</p>\n<span>inline</span></div>\n"
+   * @endcode
+   *
+   * @param string|\Stringable|array|null $value
+   *   The value to format, either a string, a Stringable object, null, or an array of strings.
+   *
+   * @return string
+   *   The formatted value with line breaks after block-level closing tags.
+   */
+  protected function formatHtmlForDiff(string|\Stringable|array|null $value = ''): string {
+    if ($value === NULL || $value === '') {
+      return '';
+    }
+    // List of block-level HTML elements, based on HTML5 spec.
+    // Only these tags will have a line break inserted after their closing tag.
+    $block_tags = [
+      'address', 'article', 'aside', 'blockquote', 'canvas', 'dd', 'div', 'dl', 'dt',
+      'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
+      'header', 'hr', 'li', 'main', 'nav', 'noscript', 'ol', 'output', 'p', 'pre', 'section',
+      'table', 'tfoot', 'ul', 'video', 'tr', 'td', 'th', 'thead', 'tbody', 'colgroup', 'col', 'caption'
+    ];
+    $pattern = '/(<\/(?:' . implode('|', $block_tags) . ')>)/i';
+
+    if (is_string($value) || $value instanceof \Stringable) {
+      return preg_replace($pattern, "$1\n", (string) $value);
+    }
+    elseif (is_array($value)) {
+      // Flatten arrays into a single string.
+      $flattened = implode("\n", array_map(function($item) {
+        return is_string($item) || $item instanceof \Stringable ? (string) $item : print_r($item, TRUE);
+      }, $value));
+      return preg_replace($pattern, "$1\n", $flattened);
+    }
+    return (string) $value;
+  }
+
 }
diff --git a/src/Plugin/diff/Layout/SplitFieldsDiffLayout.php b/src/Plugin/diff/Layout/SplitFieldsDiffLayout.php
index cafcac1..6fad9e3 100644
--- a/src/Plugin/diff/Layout/SplitFieldsDiffLayout.php
+++ b/src/Plugin/diff/Layout/SplitFieldsDiffLayout.php
@@ -109,6 +109,11 @@ class SplitFieldsDiffLayout extends DiffLayoutBase {
           $field['#data']['#right'] = $this->applyMarkdown('drupal_html_to_text', $field['#data']['#right']);
         }
       }
+
+      // Format HTML to ensure line breaks are inserted after block-level tags.
+      $field['#data']['#left'] = $this->formatHtmlForDiff($field['#data']['#left']);
+      $field['#data']['#right'] = $this->formatHtmlForDiff($field['#data']['#right']);
+
       // Process the array (split the strings into single line strings)
       // and get line counts per field.
       $this->entityComparison->processStateLine($field);
diff --git a/src/Plugin/diff/Layout/UnifiedFieldsDiffLayout.php b/src/Plugin/diff/Layout/UnifiedFieldsDiffLayout.php
index 80d61f3..d5f9f3f 100644
--- a/src/Plugin/diff/Layout/UnifiedFieldsDiffLayout.php
+++ b/src/Plugin/diff/Layout/UnifiedFieldsDiffLayout.php
@@ -112,6 +112,10 @@ class UnifiedFieldsDiffLayout extends DiffLayoutBase {
         }
       }
 
+      // Format HTML to ensure line breaks are inserted after block-level tags.
+      $field['#data']['#left'] = $this->formatHtmlForDiff($field['#data']['#left']);
+      $field['#data']['#right'] = $this->formatHtmlForDiff($field['#data']['#right']);
+
       // Process the array (split the strings into single line strings)
       // and get line counts per field.
       $this->entityComparison->processStateLine($field);
diff --git a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
index 669a373..8940647 100644
--- a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
+++ b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
@@ -158,6 +158,10 @@ class VisualInlineDiffLayout extends DiffLayoutBase {
     $html_1 = $this->renderer->render($left_view);
     $html_2 = $this->renderer->render($right_view);
 
+    // Format HTML to ensure line breaks are inserted after block-level tags.
+    $html_1 = $this->formatHtmlForDiff($html_1);
+    $html_2 = $this->formatHtmlForDiff($html_2);
+
     $this->htmlDiff->setOldHtml($html_1);
     $this->htmlDiff->setNewHtml($html_2);
     $this->htmlDiff->build();
-- 
GitLab


From 49c1e0bc965a4702ef0edb2c445b77c7f7dbff14 Mon Sep 17 00:00:00 2001
From: Carlos Ballena <cballenar@gmail.com>
Date: Wed, 30 Sep 2026 13:25:23 -0400
Subject: [PATCH 2/6] Issue #3627092: Disable group diffs in Visual Inline
 layout to improve performance

---
 src/Plugin/diff/Layout/VisualInlineDiffLayout.php | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
index 8940647..cade90d 100644
--- a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
+++ b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
@@ -60,6 +60,10 @@ class VisualInlineDiffLayout extends DiffLayoutBase {
       $storage->save('cache.php', 'dummy');
     }
     $html_diff->getConfig()->setPurifierCacheLocation(\dirname($storage->getFullPath('cache.php')));
+
+    // Force the library to diff word-by-word and tag-by-tag individually
+    $html_diff->getConfig()->setGroupDiffs(FALSE);
+
     $this->htmlDiff = $html_diff;
   }
 
-- 
GitLab


From eb01bf81ad57bcae693ed42e5c472f8ac4206853 Mon Sep 17 00:00:00 2001
From: Carlos Ballena <cballenar@gmail.com>
Date: Wed, 30 Sep 2026 13:29:12 -0400
Subject: [PATCH 3/6] Issue #3627092: Disable HTMLPurifier in Visual Inline
 layout to improve performance as Drupal's renderer pipeline already provides
 valid and safe HTML

---
 src/Plugin/diff/Layout/VisualInlineDiffLayout.php | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
index cade90d..824428b 100644
--- a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
+++ b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
@@ -61,6 +61,10 @@ class VisualInlineDiffLayout extends DiffLayoutBase {
     }
     $html_diff->getConfig()->setPurifierCacheLocation(\dirname($storage->getFullPath('cache.php')));
 
+    // The rendered output from Drupal is already valid and safe HTML.
+    // Disabling HTMLPurifier reduces the execution time for large revisions.
+    $html_diff->getConfig()->setPurifierEnabled(FALSE);
+
     // Force the library to diff word-by-word and tag-by-tag individually
     $html_diff->getConfig()->setGroupDiffs(FALSE);
 
-- 
GitLab


From db33cbddbbc1bc78093a6819cc783163a3b31ccb Mon Sep 17 00:00:00 2001
From: Carlos Ballena <cballenar@gmail.com>
Date: Wed, 30 Sep 2026 14:53:43 -0400
Subject: [PATCH 4/6] Issue #3627092: Fixes test issues in DiffLayoutBase.php
 and adds missing keywords to cspell.

---
 .cspell-project-words.txt |  7 -------
 src/DiffLayoutBase.php    | 42 ++++++++++++++++++++-------------------
 2 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt
index 1525d7c..e69de29 100644
--- a/.cspell-project-words.txt
+++ b/.cspell-project-words.txt
@@ -1,7 +0,0 @@
-behaviour
-diffadminplugins
-Guguel
-htmldiff
-langname
-nextlink
-prevlink
diff --git a/src/DiffLayoutBase.php b/src/DiffLayoutBase.php
index 2a735e3..3992f4a 100644
--- a/src/DiffLayoutBase.php
+++ b/src/DiffLayoutBase.php
@@ -311,11 +311,12 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
   }
 
   /**
-   * Formats HTML by adding line breaks after closing block-level tags to improve diff readability.
+   * Formats HTML by adding line breaks after closing block tags for diffs.
    *
-   * This is used to help the diff parser identify changes in content more accurately,
-   * especially when CKEditor or other editors strip whitespace. Only block-level elements
-   * are considered, so inline tags (like <a>, <span>, etc.) are not affected.
+   * This is used to help the diff parser identify changes in content more
+   * accurately, especially when CKEditor or other editors strip whitespace.
+   * Only block-level elements are considered, so inline tags (like <a>,
+   * <span>, etc.) are not affected.
    *
    * Example usage:
    * @code
@@ -324,36 +325,37 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
    * @endcode
    *
    * @param string|\Stringable|array|null $value
-   *   The value to format, either a string, a Stringable object, null, or an array of strings.
+   *   The value to format, either a string, a Stringable object, null, or
+   *   an array of strings.
    *
-   * @return string
+   * @return string|array
    *   The formatted value with line breaks after block-level closing tags.
    */
-  protected function formatHtmlForDiff(string|\Stringable|array|null $value = ''): string {
+  protected function formatHtmlForDiff(string|\Stringable|array|null $value = ''): string|array {
     if ($value === NULL || $value === '') {
       return '';
     }
     // List of block-level HTML elements, based on HTML5 spec.
     // Only these tags will have a line break inserted after their closing tag.
     $block_tags = [
-      'address', 'article', 'aside', 'blockquote', 'canvas', 'dd', 'div', 'dl', 'dt',
-      'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
-      'header', 'hr', 'li', 'main', 'nav', 'noscript', 'ol', 'output', 'p', 'pre', 'section',
-      'table', 'tfoot', 'ul', 'video', 'tr', 'td', 'th', 'thead', 'tbody', 'colgroup', 'col', 'caption'
+      'address', 'article', 'aside', 'blockquote', 'canvas', 'dd', 'div',
+      'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form',
+      'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'li', 'main',
+      'nav', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table',
+      'tfoot', 'ul', 'video', 'tr', 'td', 'th', 'thead', 'tbody',
+      'colgroup', 'col', 'caption',
     ];
-    $pattern = '/(<\/(?:' . implode('|', $block_tags) . ')>)/i';
+    $pattern = '/(<\/(?:' . \implode('|', $block_tags) . ')>)(?!\s*\n)(?!\s*$)/i';
 
-    if (is_string($value) || $value instanceof \Stringable) {
-      return preg_replace($pattern, "$1\n", (string) $value);
+    if (\is_string($value) || $value instanceof \Stringable) {
+      return (string) \preg_replace($pattern, "$1\n", (string) $value);
     }
-    elseif (is_array($value)) {
-      // Flatten arrays into a single string.
-      $flattened = implode("\n", array_map(function($item) {
-        return is_string($item) || $item instanceof \Stringable ? (string) $item : print_r($item, TRUE);
+    else {
+      $flattened = \implode("\n", \array_map(function ($item) {
+        return \is_string($item) || $item instanceof \Stringable ? (string) $item : \print_r($item, TRUE);
       }, $value));
-      return preg_replace($pattern, "$1\n", $flattened);
+      return (string) \preg_replace($pattern, "$1\n", $flattened);
     }
-    return (string) $value;
   }
 
 }
-- 
GitLab


From 34b45de16774d08793111f7ac647ce7a4076171c Mon Sep 17 00:00:00 2001
From: Carlos Ballena <cballenar@gmail.com>
Date: Wed, 30 Sep 2026 14:54:28 -0400
Subject: [PATCH 5/6] Issue #3627092: Fixes test issues reported for grouping.

---
 src/Plugin/diff/Layout/VisualInlineDiffLayout.php | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
index 824428b..38c37d1 100644
--- a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
+++ b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
@@ -59,14 +59,18 @@ class VisualInlineDiffLayout extends DiffLayoutBase {
     if (!$storage->exists('cache.php')) {
       $storage->save('cache.php', 'dummy');
     }
-    $html_diff->getConfig()->setPurifierCacheLocation(\dirname($storage->getFullPath('cache.php')));
+    /** @var \HtmlDiffAdvancedConfig $config */
+    $config = $html_diff->getConfig();
+    $config->setPurifierCacheLocation(\dirname($storage->getFullPath('cache.php')));
 
     // The rendered output from Drupal is already valid and safe HTML.
     // Disabling HTMLPurifier reduces the execution time for large revisions.
     $html_diff->getConfig()->setPurifierEnabled(FALSE);
 
-    // Force the library to diff word-by-word and tag-by-tag individually
-    $html_diff->getConfig()->setGroupDiffs(FALSE);
+    // Force the library to diff word-by-word and tag-by-tag individually.
+    if (\method_exists($config, 'setGroupDiffs')) {
+      $config->setGroupDiffs(FALSE);
+    }
 
     $this->htmlDiff = $html_diff;
   }
-- 
GitLab


From 661b839a78b9ec8582d7a9dbe660ebd6225b1d5b Mon Sep 17 00:00:00 2001
From: Carlos Ballena <cballenar@gmail.com>
Date: Wed, 30 Sep 2026 14:54:16 -0400
Subject: [PATCH 6/6] Issue #3627092: Fixes test issues reported for purifier.

---
 src/Plugin/diff/Layout/VisualInlineDiffLayout.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
index 38c37d1..bedcc9f 100644
--- a/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
+++ b/src/Plugin/diff/Layout/VisualInlineDiffLayout.php
@@ -65,7 +65,9 @@ class VisualInlineDiffLayout extends DiffLayoutBase {
 
     // The rendered output from Drupal is already valid and safe HTML.
     // Disabling HTMLPurifier reduces the execution time for large revisions.
-    $html_diff->getConfig()->setPurifierEnabled(FALSE);
+    if (\method_exists($config, 'setPurifierEnabled')) {
+      $config->setPurifierEnabled(FALSE);
+    }
 
     // Force the library to diff word-by-word and tag-by-tag individually.
     if (\method_exists($config, 'setGroupDiffs')) {
-- 
GitLab

