plain_text_formatter_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. RSpec.describe PlainTextFormatter do
  3. describe '#to_s' do
  4. subject { described_class.new(status.text, status.local?).to_s }
  5. context 'when status is local' do
  6. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  7. it 'returns the raw text' do
  8. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  9. end
  10. end
  11. context 'when status is remote' do
  12. let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') }
  13. context 'when text contains inline HTML tags' do
  14. let(:status) { Fabricate(:status, account: remote_account, text: '<b>Lorem</b> <em>ipsum</em>') }
  15. it 'strips the tags' do
  16. expect(subject).to eq 'Lorem ipsum'
  17. end
  18. end
  19. context 'when text contains <p> tags' do
  20. let(:status) { Fabricate(:status, account: remote_account, text: '<p>Lorem</p><p>ipsum</p>') }
  21. it 'inserts a newline' do
  22. expect(subject).to eq "Lorem\nipsum"
  23. end
  24. end
  25. context 'when text contains a single <br> tag' do
  26. let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem<br>ipsum') }
  27. it 'inserts a newline' do
  28. expect(subject).to eq "Lorem\nipsum"
  29. end
  30. end
  31. context 'when text contains consecutive <br> tag' do
  32. let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem<br><br><br>ipsum') }
  33. it 'inserts a single newline' do
  34. expect(subject).to eq "Lorem\nipsum"
  35. end
  36. end
  37. context 'when text contains HTML entity' do
  38. let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem &amp; ipsum &#x2764;') }
  39. it 'unescapes the entity' do
  40. expect(subject).to eq 'Lorem & ipsum ❤'
  41. end
  42. end
  43. context 'when text contains <script> tag' do
  44. let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem <script> alert("Booh!") </script>ipsum') }
  45. it 'strips the tag and its contents' do
  46. expect(subject).to eq 'Lorem ipsum'
  47. end
  48. end
  49. context 'when text contains an HTML comment tags' do
  50. let(:status) { Fabricate(:status, account: remote_account, text: 'Lorem <!-- Booh! -->ipsum') }
  51. it 'strips the comment' do
  52. expect(subject).to eq 'Lorem ipsum'
  53. end
  54. end
  55. end
  56. end
  57. end