html_aware_formatter_spec.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe HtmlAwareFormatter do
  4. describe '#to_s' do
  5. subject { described_class.new(text, local).to_s }
  6. context 'when local' do
  7. let(:local) { true }
  8. let(:text) { 'Foo bar' }
  9. it 'returns formatted text' do
  10. expect(subject).to eq '<p>Foo bar</p>'
  11. end
  12. end
  13. context 'when remote' do
  14. let(:local) { false }
  15. context 'when given plain text' do
  16. let(:text) { 'Beep boop' }
  17. it 'keeps the plain text' do
  18. expect(subject).to include 'Beep boop'
  19. end
  20. end
  21. context 'when given text containing script tags' do
  22. let(:text) { '<script>alert("Hello")</script>' }
  23. it 'strips the scripts' do
  24. expect(subject).to_not include '<script>alert("Hello")</script>'
  25. end
  26. end
  27. context 'when given text containing malicious classes' do
  28. let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
  29. it 'strips the malicious classes' do
  30. expect(subject).to_not include 'status__content__spoiler-link'
  31. end
  32. end
  33. context 'when given text containing ruby tags for east-asian languages' do
  34. let(:text) { '<ruby>明日 <rp>(</rp><rt>Ashita</rt><rp>)</rp></ruby>' }
  35. it 'keeps the ruby tags' do
  36. expect(subject).to eq '<ruby>明日 <rp>(</rp><rt>Ashita</rt><rp>)</rp></ruby>'
  37. end
  38. end
  39. end
  40. end
  41. end