diff --git a/app/models/solr_document.rb b/app/models/solr_document.rb index 0535d5bf..726e57ea 100644 --- a/app/models/solr_document.rb +++ b/app/models/solr_document.rb @@ -100,7 +100,9 @@ def wrap_in_paragraph(value) if value.start_with?("<") value else - ActionController::Base.helpers.content_tag(:p, value) + # rubocop:disable Rails/OutputSafety + ActionController::Base.helpers.content_tag(:p, value.html_safe) + # rubocop:enable Rails/OutputSafety end end end diff --git a/app/presenters/rendering/chronlist.rb b/app/presenters/rendering/chronlist.rb index c859e827..34fef415 100644 --- a/app/presenters/rendering/chronlist.rb +++ b/app/presenters/rendering/chronlist.rb @@ -22,7 +22,7 @@ def render_chronlist Nokogiri::HTML::Builder.with(fragment) do |doc| if values["head"].present? - doc.h3 { + doc.h4 { doc.text values["head"] } end diff --git a/app/presenters/subnotes_presenter.rb b/app/presenters/subnotes_presenter.rb index 0a027ab6..2a6589f8 100644 --- a/app/presenters/subnotes_presenter.rb +++ b/app/presenters/subnotes_presenter.rb @@ -16,7 +16,6 @@ def render if valid_json?(value) render_subnote(value) else - # Otherwise, render it normally value end end @@ -36,16 +35,22 @@ def render_subnote(value) # rendered in order, since the generated HTML from each subnote is concatenated together to # form the whole note. - if subnote_value.key? TABLE_ELEMENT - rendered_subnote << Blacklight::Rendering::Pipeline.new(subnote_value[TABLE_ELEMENT], field_config, document, view_context, [Rendering::Table], options).render - end + begin + if subnote_value.key? TABLE_ELEMENT + rendered_subnote << Blacklight::Rendering::Pipeline.new(subnote_value[TABLE_ELEMENT], field_config, document, view_context, [Rendering::Table], options).render + end - if subnote_value.key? CHRONLIST_ELEMENT - rendered_subnote << Blacklight::Rendering::Pipeline.new(subnote_value[CHRONLIST_ELEMENT], field_config, document, view_context, [Rendering::Chronlist], options).render - end + if subnote_value.key? CHRONLIST_ELEMENT + rendered_subnote << Blacklight::Rendering::Pipeline.new(subnote_value[CHRONLIST_ELEMENT], field_config, document, view_context, [Rendering::Chronlist], options).render + end - if subnote_value.key? BIBREF_ELEMENT - rendered_subnote << Blacklight::Rendering::Pipeline.new(subnote_value[BIBREF_ELEMENT], field_config, document, view_context, [Rendering::Bibref], options).render + if subnote_value.key? BIBREF_ELEMENT + rendered_subnote << Blacklight::Rendering::Pipeline.new(subnote_value[BIBREF_ELEMENT], field_config, document, view_context, [Rendering::Bibref], options).render + end + rescue + # If there's an error rendering a subnote, render the original value instead to avoid + # breaking the whole page. + subnote_value << value end # everything else that's not a known subnote element will not be rendered diff --git a/spec/models/solr_document_spec.rb b/spec/models/solr_document_spec.rb index df6adc1a..a435b35f 100644 --- a/spec/models/solr_document_spec.rb +++ b/spec/models/solr_document_spec.rb @@ -193,7 +193,7 @@ end end - context "when note already contains HTML tags" do + context "when note starts with an HTML tag" do subject(:notes_value) do document.extract_notes_by_header("references") end @@ -209,6 +209,22 @@ end end + context "when note doesn't start with an HTML tag and contains HTML" do + subject(:notes_value) do + document.extract_notes_by_header("references") + end + + let(:document) { + described_class.new( + note_json_ssm: ["{\"head\":\"References\",\"p\":\"Testing HTML inside a paragraph.\",\"audience\":\"internal\"}"] + ) + } + + it "wraps the note in a paragraph and doesn't escape the existing markup" do + expect(notes_value).to eq ["
Testing HTML inside a paragraph.
"] + end + end + context "when chronlist is outside of the Text subnote" do subject(:notes_value) do document.extract_notes_by_header("biog") diff --git a/spec/presenters/rendering/chronlist_spec.rb b/spec/presenters/rendering/chronlist_spec.rb index f6a1c02b..84b7419f 100644 --- a/spec/presenters/rendering/chronlist_spec.rb +++ b/spec/presenters/rendering/chronlist_spec.rb @@ -55,7 +55,7 @@ let(:note) { JSON.parse(IO.read("spec/files/notes/chronlist_no-header_eventgrp.json")) } it "renders the chronlist with just the date" do - expect(rendered).to have_css("h3", text: "Honours and Awards") + expect(rendered).to have_css("h4", text: "Honours and Awards") expect(rendered).to have_table(class: %w[table-light table-responsive table-striped table-hover]) expect(rendered).to have_css("thead.table-purple") expect(rendered).to have_css("tbody.table-group-divider")