Skip to content

Commit a5c7951

Browse files
authored
Merge pull request #3728 from projectblacklight/3727-link-to-facet-renders-html-for-non-html-formats
3727 link to facet renders html for non html formats
2 parents a48d5e6 + 9a93945 commit a5c7951

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

app/presenters/blacklight/rendering/abstract_step.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ def next_step(output_values)
2222
end
2323

2424
def html?
25-
options[:format].nil? || options[:format].to_s == 'html'
25+
format.nil? || format.to_s == 'html'
26+
end
27+
28+
def format
29+
return options[:format] unless context.respond_to?(:search_state)
30+
31+
options[:format] || context.search_state&.params&.dig(:format)
2632
end
2733
end
2834
end

spec/presenters/blacklight/rendering/pipeline_spec.rb

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,55 @@
3737
end
3838

3939
context 'outside of an HTML context' do
40-
let(:options) { { format: 'text' } }
40+
context 'when options determines format' do
41+
let(:options) { { format: 'text' } }
4142

42-
let(:values) { ['"blah"', "<notatag>"] }
43-
let(:field_config) { Blacklight::Configuration::NullField.new itemprop: 'some-prop' }
43+
let(:values) { ['"blah"', "<notatag>"] }
44+
let(:field_config) { Blacklight::Configuration::NullField.new itemprop: 'some-prop' }
4445

45-
it 'does not HTML escape values or inject HTML tags' do
46-
expect(rendered).to eq '"blah" and <notatag>'
46+
it 'does not HTML escape values or inject HTML tags' do
47+
expect(rendered).to eq '"blah" and <notatag>'
48+
end
49+
end
50+
51+
context 'when context determines format' do
52+
let(:values) { ['"blah"', "<notatag>"] }
53+
let(:field_config) { Blacklight::Configuration::NullField.new itemprop: 'some-prop' }
54+
let(:controller) { CatalogController.new }
55+
let(:search_state) { Blacklight::SearchState.new({ format: 'text' }, controller.blacklight_config, controller) }
56+
57+
before { allow(context).to receive(:search_state).and_return(search_state) }
58+
59+
it 'does not HTML escape values or inject HTML tags' do
60+
expect(rendered).to eq '"blah" and <notatag>'
61+
end
62+
end
63+
end
64+
65+
context 'when link_to_facet is in the config' do
66+
let(:values) { %w[book manuscript] }
67+
let(:field_config) { Blacklight::Configuration::Field.new(field: 'format', key: 'format', link_to_facet: true) }
68+
let(:controller) { CatalogController.new }
69+
let(:search_state) { Blacklight::SearchState.new({}, controller.blacklight_config, controller) }
70+
71+
before do
72+
allow(context).to receive(:search_state).and_return(search_state)
73+
allow(context).to receive(:search_action_path) { |f| "/catalog?f[format][]=#{f['f']['format'].first}" }
74+
allow(context).to receive(:link_to) { |value, link| ActiveSupport::SafeBuffer.new("<a href=#{link}>#{value}</a>") }
75+
end
76+
77+
it 'renders html' do
78+
expect(rendered).to eq "<a href=/catalog?f[format][]=book>book</a> and <a href=/catalog?f[format][]=manuscript>manuscript</a>"
79+
end
80+
81+
context 'outside html context' do
82+
let(:values) { %w[book manuscript] }
83+
let(:field_config) { Blacklight::Configuration::Field.new(field: 'format', link_to_facet: true) }
84+
let(:search_state) { Blacklight::SearchState.new({ format: 'json' }, CatalogController.blacklight_config, CatalogController.new) }
85+
86+
it 'does not render html' do
87+
expect(rendered).to eq "book and manuscript"
88+
end
4789
end
4890
end
4991
end

0 commit comments

Comments
 (0)