Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: formatting resiliency #463

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading