plain_text_formatter_spec.rb 2.8 KB

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