From b78597979a71d4ce4296a96fffc1fb466c7d3c6b Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 15 Oct 2024 10:12:54 -0400 Subject: [PATCH] Fix missing content warning text in rss formatter (#32406) --- app/helpers/formatting_helper.rb | 78 +++++++++++++++++--------- spec/helpers/formatting_helper_spec.rb | 54 +++++++++++++++--- 2 files changed, 99 insertions(+), 33 deletions(-) diff --git a/app/helpers/formatting_helper.rb b/app/helpers/formatting_helper.rb index 7d1423e52..2ef7d362d 100644 --- a/app/helpers/formatting_helper.rb +++ b/app/helpers/formatting_helper.rb @@ -1,6 +1,14 @@ # frozen_string_literal: true module FormattingHelper + SYNDICATED_EMOJI_STYLES = <<~CSS.squish + height: 1.1em; + margin: -.2ex .15em .2ex; + object-fit: contain; + vertical-align: middle; + width: 1.1em; + CSS + def html_aware_format(text, local, options = {}) HtmlAwareFormatter.new(text, local, options).to_s end @@ -23,33 +31,10 @@ module FormattingHelper end def rss_status_content_format(status) - html = status_content_format(status) - - before_html = if status.spoiler_text? - tag.p do - tag.strong do - I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale) - end - - status.spoiler_text - end + tag.hr - end - - after_html = if status.preloadable_poll - tag.p do - safe_join( - status.preloadable_poll.options.map do |o| - tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', o, disabled: true) - end, - tag.br - ) - end - end - prerender_custom_emojis( - safe_join([before_html, html, after_html]), + wrapped_status_content_format(status), status.emojis, - style: 'width: 1.1em; height: 1.1em; object-fit: contain; vertical-align: middle; margin: -.2ex .15em .2ex' + style: SYNDICATED_EMOJI_STYLES ).to_str end @@ -64,4 +49,47 @@ module FormattingHelper html_aware_format(field.value, field.account.local?, with_rel_me: with_rel_me, with_domains: true, multiline: false) end end + + private + + def wrapped_status_content_format(status) + safe_join [ + rss_content_preroll(status), + status_content_format(status), + rss_content_postroll(status), + ] + end + + def rss_content_preroll(status) + if status.spoiler_text? + safe_join [ + tag.p { spoiler_with_warning(status) }, + tag.hr, + ] + end + end + + def spoiler_with_warning(status) + safe_join [ + tag.strong { I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale) }, + status.spoiler_text, + ] + end + + def rss_content_postroll(status) + if status.preloadable_poll + tag.p do + poll_option_tags(status) + end + end + end + + def poll_option_tags(status) + safe_join( + status.preloadable_poll.options.map do |option| + tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', option, disabled: true) + end, + tag.br + ) + end end diff --git a/spec/helpers/formatting_helper_spec.rb b/spec/helpers/formatting_helper_spec.rb index 136a609b1..5ff534e4e 100644 --- a/spec/helpers/formatting_helper_spec.rb +++ b/spec/helpers/formatting_helper_spec.rb @@ -6,19 +6,57 @@ RSpec.describe FormattingHelper do include Devise::Test::ControllerHelpers describe '#rss_status_content_format' do - let(:status) { Fabricate(:status, text: 'Hello world<>', spoiler_text: 'This is a spoiler<>', poll: Fabricate(:poll, options: %w(Yes<> No))) } - let(:html) { helper.rss_status_content_format(status) } + subject { helper.rss_status_content_format(status) } - it 'renders the spoiler text' do - expect(html).to include('

This is a spoiler<>


') + context 'with a simple status' do + let(:status) { Fabricate.build :status, text: 'Hello world' } + + it 'renders the formatted elements' do + expect(parsed_result.css('p').first.text) + .to eq('Hello world') + end end - it 'renders the status text' do - expect(html).to include('

Hello world<>

') + context 'with a spoiler and an emoji and a poll' do + let(:status) { Fabricate(:status, text: 'Hello :world: <>', spoiler_text: 'This is a spoiler<>', poll: Fabricate(:poll, options: %w(Yes<> No))) } + + before { Fabricate :custom_emoji, shortcode: 'world' } + + it 'renders the formatted elements' do + expect(spoiler_node.css('strong').text) + .to eq('Content warning:') + expect(spoiler_node.text) + .to include('This is a spoiler<>') + expect(content_node.text) + .to eq('Hello <>') + expect(content_node.css('img').first.to_h.symbolize_keys) + .to include( + rel: 'emoji', + title: ':world:' + ) + expect(poll_node.css('radio').first.text) + .to eq('Yes<>') + expect(poll_node.css('radio').first.to_h.symbolize_keys) + .to include( + disabled: 'disabled' + ) + end + + def spoiler_node + parsed_result.css('p').first + end + + def content_node + parsed_result.css('p')[1] + end + + def poll_node + parsed_result.css('p').last + end end - it 'renders the poll' do - expect(html).to include('Yes<>
') + def parsed_result + Nokogiri::HTML.fragment(subject) end end end