2023-02-21 19:55:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-25 21:53:34 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe PlainTextFormatter do
|
|
|
|
describe '#to_s' do
|
|
|
|
subject { described_class.new(status.text, status.local?).to_s }
|
|
|
|
|
2023-03-08 13:56:41 -05:00
|
|
|
context 'when status is local' do
|
2022-03-25 21:53:34 -04:00
|
|
|
let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
|
|
|
|
|
|
|
|
it 'returns the raw text' do
|
2023-02-19 23:00:48 -05:00
|
|
|
expect(subject).to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
|
2022-03-25 21:53:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-08 13:56:41 -05:00
|
|
|
context 'when status is remote' do
|
2022-03-25 21:53:34 -04:00
|
|
|
let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') }
|
|
|
|
|
2023-03-08 13:56:41 -05:00
|
|
|
context 'when text contains inline HTML tags' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: '<b>Lorem</b> <em>ipsum</em>') }
|
|
|
|
|
|
|
|
it 'strips the tags' do
|
|
|
|
expect(subject).to eq 'Lorem ipsum'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text contains <p> tags' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: '<p>Lorem</p><p>ipsum</p>') }
|
|
|
|
|
|
|
|
it 'inserts a newline' do
|
|
|
|
expect(subject).to eq "Lorem\nipsum"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text contains a single <br> tag' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem<br>ipsum') }
|
|
|
|
|
|
|
|
it 'inserts a newline' do
|
|
|
|
expect(subject).to eq "Lorem\nipsum"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text contains consecutive <br> tag' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem<br><br><br>ipsum') }
|
|
|
|
|
|
|
|
it 'inserts a single newline' do
|
|
|
|
expect(subject).to eq "Lorem\nipsum"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text contains HTML entity' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem & ipsum ❤') }
|
|
|
|
|
|
|
|
it 'unescapes the entity' do
|
|
|
|
expect(subject).to eq 'Lorem & ipsum ❤'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text contains <script> tag' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem <script> alert("Booh!") </script>ipsum') }
|
|
|
|
|
|
|
|
it 'strips the tag and its contents' do
|
|
|
|
expect(subject).to eq 'Lorem ipsum'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text contains an HTML comment tags' do
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem <!-- Booh! -->ipsum') }
|
|
|
|
|
|
|
|
it 'strips the comment' do
|
|
|
|
expect(subject).to eq 'Lorem ipsum'
|
|
|
|
end
|
2022-03-25 21:53:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|