2023-02-21 16:55:31 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-25 18:53:34 -07:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe HtmlAwareFormatter do
|
|
|
|
describe '#to_s' do
|
|
|
|
subject { described_class.new(text, local).to_s }
|
|
|
|
|
|
|
|
context 'when local' do
|
|
|
|
let(:local) { true }
|
|
|
|
let(:text) { 'Foo bar' }
|
|
|
|
|
|
|
|
it 'returns formatted text' do
|
2023-02-19 20:00:48 -08:00
|
|
|
expect(subject).to eq '<p>Foo bar</p>'
|
2022-03-25 18:53:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when remote' do
|
|
|
|
let(:local) { false }
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'when given plain text' do
|
2022-03-25 18:53:34 -07:00
|
|
|
let(:text) { 'Beep boop' }
|
|
|
|
|
|
|
|
it 'keeps the plain text' do
|
2023-02-19 20:00:48 -08:00
|
|
|
expect(subject).to include 'Beep boop'
|
2022-03-25 18:53:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'when given text containing script tags' do
|
2022-03-25 18:53:34 -07:00
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
|
|
|
|
|
|
|
it 'strips the scripts' do
|
2023-02-19 20:00:48 -08:00
|
|
|
expect(subject).to_not include '<script>alert("Hello")</script>'
|
2022-03-25 18:53:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'when given text containing malicious classes' do
|
2022-03-25 18:53:34 -07:00
|
|
|
let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
|
|
|
|
|
|
|
|
it 'strips the malicious classes' do
|
2023-02-19 20:00:48 -08:00
|
|
|
expect(subject).to_not include 'status__content__spoiler-link'
|
2022-03-25 18:53:34 -07:00
|
|
|
end
|
|
|
|
end
|
2024-07-03 13:05:59 -07:00
|
|
|
|
|
|
|
context 'when given text containing ruby tags for east-asian languages' do
|
|
|
|
let(:text) { '<ruby>明日 <rp>(</rp><rt>Ashita</rt><rp>)</rp></ruby>' }
|
|
|
|
|
|
|
|
it 'keeps the ruby tags' do
|
|
|
|
expect(subject).to eq '<ruby>明日 <rp>(</rp><rt>Ashita</rt><rp>)</rp></ruby>'
|
|
|
|
end
|
|
|
|
end
|
2022-03-25 18:53:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|