image_size_spec.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe HtmlSanitizer::Scrubber::ImageSize do
  4. let(:scrubber) { described_class.new }
  5. describe('#scrubber') do
  6. subject(:actual) { fragment.scrub!(scrubber).to_html }
  7. let(:fragment) { Loofah.fragment(input) }
  8. context 'when image' do
  9. let(:input) { '<img src="...">' }
  10. let(:target) { '<img src="..." style="max-width:100%;">' }
  11. it { is_expected.to eq target }
  12. end
  13. context 'when not image' do
  14. let(:input) { '<script src="..."></script>' }
  15. let(:target) { '<script src="..."></script>' }
  16. it { is_expected.to eq target }
  17. end
  18. context 'when does not have source' do
  19. let(:input) { '<img>' }
  20. let(:target) { '<img>' }
  21. it { is_expected.to eq target }
  22. end
  23. end
  24. describe '#build_style' do
  25. it 'sets max width' do
  26. input = ''
  27. expect(scrubber.send(:build_style, input)).to eq 'max-width:100%;'
  28. end
  29. it 'copies old attributes' do
  30. input = 'attr:value;another:value;'
  31. expect(scrubber.send(:build_style, input)).to eq 'max-width:100%;attr:value;another:value;'
  32. end
  33. it 'renames height to max-height' do
  34. input = ' height: 20px'
  35. expect(scrubber.send(:build_style, input)).to eq 'max-width:100%;max-height: 20px;'
  36. end
  37. end
  38. end