From 712aa8f40c8c78ec2693879097bdb0f7d49acd7f Mon Sep 17 00:00:00 2001
From: Aaron Collier <aaron.collier@stanford.edu>
Date: Fri, 3 Nov 2023 13:43:27 -0700
Subject: [PATCH] Encode html &#13 as \r for display

---
 app/helpers/record_helper.rb                   |  7 +++++++
 .../catalog/mastheads/_collection.html.erb     |  2 +-
 spec/helpers/record_helper_spec.rb             | 18 ++++++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/app/helpers/record_helper.rb b/app/helpers/record_helper.rb
index d9a16f71e..652ebe61e 100644
--- a/app/helpers/record_helper.rb
+++ b/app/helpers/record_helper.rb
@@ -34,4 +34,11 @@ def linked_mods_genres(genres)
       end
     end.join('').html_safe
   end
+
+  ##
+  # Expose format_mods_html from mods_display to properly encode content as needed
+  # RecordHelper behavior
+  def format_record_html(...)
+    format_mods_html(...)
+  end
 end
diff --git a/app/views/catalog/mastheads/_collection.html.erb b/app/views/catalog/mastheads/_collection.html.erb
index a221b1ac2..c30527da9 100644
--- a/app/views/catalog/mastheads/_collection.html.erb
+++ b/app/views/catalog/mastheads/_collection.html.erb
@@ -7,7 +7,7 @@
         <% if @parent[:summary_display] %>
           <div class="collection-summary">
             <div data-behavior='truncate'>
-              <%= @parent[:summary_display].join(', ') %>
+              <%= format_record_html(@parent[:summary_display].join(', ')) %>
             </div>
           </div>
         <% end %>
diff --git a/spec/helpers/record_helper_spec.rb b/spec/helpers/record_helper_spec.rb
index c38bc843b..d3c09a85e 100644
--- a/spec/helpers/record_helper_spec.rb
+++ b/spec/helpers/record_helper_spec.rb
@@ -20,4 +20,22 @@
       end
     end
   end
+
+  describe 'record html' do
+    context 'when the record has line feeds in the string' do
+      let(:html) { "some text\r\nsome other text" }
+
+      it 'returns the string without changing the encoding' do
+        expect(helper.format_record_html(html)).to eq "some text\r\nsome other text"
+      end
+    end
+
+    context 'when the record has encoded line feeds in the string' do
+      let(:html) { "some text&#13\nsome other text" }
+
+      it 'returns the string with proper encoding' do
+        expect(helper.format_record_html(html)).to eq "some text\r\nsome other text"
+      end
+    end
+  end
 end