about summary refs log tree commit diff
path: root/spec/lib/plain_text_formatter_spec.rb
blob: 80b3c331a6bfcfb204adc19b580627cc5402c9ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PlainTextFormatter do
  describe '#to_s' do
    subject { described_class.new(status.text, status.local?).to_s }

    context 'when status is local' do
      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
        expect(subject).to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
      end
    end

    context 'when status is remote' do
      let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') }

      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 &amp; ipsum &#x2764;') }

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