1
0
mirror of https://github.com/mastodon/mastodon synced 2024-11-23 22:56:20 +09:00

Fix missing content warning text in rss formatter (#32406)

This commit is contained in:
Matt Jankowski 2024-10-15 10:12:54 -04:00 committed by GitHub
parent b742ce9d09
commit b78597979a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 99 additions and 33 deletions

View File

@ -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

View File

@ -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('<p>This is a spoiler&lt;&gt;</p><hr>')
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('<p>Hello world&lt;&gt;</p>')
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('<radio disabled="disabled">Yes&lt;&gt;</radio><br>')
def parsed_result
Nokogiri::HTML.fragment(subject)
end
end
end