Skip to content

Commit

Permalink
Merge pull request #463 from nla/fix/blac-673_formatting-resiliency
Browse files Browse the repository at this point in the history
  • Loading branch information
yetti authored Jun 20, 2024
2 parents 6bdfadc + effca62 commit 910aa73
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/models/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/presenters/rendering/chronlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions app/presenters/subnotes_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def render
if valid_json?(value)
render_subnote(value)
else
# Otherwise, render it normally
value
end
end
Expand All @@ -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
Expand Down
18 changes: 17 additions & 1 deletion spec/models/solr_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <em>HTML</em> 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 ["<p>Testing <em>HTML</em> inside a paragraph.</p>"]
end
end

context "when chronlist is outside of the Text subnote" do
subject(:notes_value) do
document.extract_notes_by_header("biog")
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/rendering/chronlist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 910aa73

Please sign in to comment.