cleanup_spec.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe HtmlSanitizer::Scrubber::Cleanup do
  4. let(:scrubber) { described_class.new }
  5. describe('#scrubber') do
  6. subject(:actual) do
  7. # export with extra options to avoid html indentation
  8. fragment.scrub!(scrubber)
  9. .to_html save_with: Nokogiri::XML::Node::SaveOptions::DEFAULT_HTML ^ Nokogiri::XML::Node::SaveOptions::FORMAT
  10. end
  11. let(:fragment) { Loofah.fragment(input) }
  12. context 'when extra spaces' do
  13. let(:input) { "<div> \n </div>" }
  14. let(:target) { '<div> </div>' }
  15. it { is_expected.to eq target }
  16. end
  17. context 'when extra spaces in preformatted tags' do
  18. let(:input) { "<code> \n </code>" }
  19. let(:target) { "<code> \n </code>" }
  20. it { is_expected.to eq target }
  21. end
  22. context 'when has extra spaces but no siblings' do
  23. let(:input) { ' content ' }
  24. let(:target) { ' content ' }
  25. it { is_expected.to eq target }
  26. end
  27. context 'when div has extra spaces' do
  28. let(:input) { '<div>test</div><div> content </div>' }
  29. let(:target) { '<div>test</div><div>content</div>' }
  30. it { is_expected.to eq target }
  31. end
  32. context 'when span has extra spaces' do
  33. let(:input) { '<div>test</div><span> content </span>' }
  34. let(:target) { '<div>test</div><span> content </span>' }
  35. it { is_expected.to eq target }
  36. end
  37. context 'when has previous' do
  38. let(:input) { '<div>test</div> content ' }
  39. let(:target) { '<div>test</div>content' }
  40. it { is_expected.to eq target }
  41. end
  42. context 'when has next div' do
  43. let(:input) { '<div> content <div>test</div></div>' }
  44. let(:target) { '<div>content<div>test</div></div>' }
  45. it { is_expected.to eq target }
  46. end
  47. context 'when has next span' do
  48. let(:input) { '<div> content <span>test</span></div>' }
  49. let(:target) { '<div> content <span>test</span></div>' }
  50. it { is_expected.to eq target }
  51. end
  52. context 'when p has style with white-space property' do
  53. let(:input) do
  54. '<p style="white-space: pre-wrap">Hi!
  55. This is a dummy text for Zammad to test multi-line text that is wrapped in a preformatted text block.</p>'
  56. end
  57. let(:target) do
  58. '<p style="white-space: pre-wrap">Hi!
  59. This is a dummy text for Zammad to test multi-line text that is wrapped in a preformatted text block.</p>'
  60. end
  61. it { is_expected.to eq target }
  62. end
  63. end
  64. end