Sha256: 8ad61ed16bd23878167f3addef74f04f23b5c333f8b5ffc6b5ac818cf981aca3

Contents?: true

Size: 1.86 KB

Versions: 13

Compression:

Stored size: 1.86 KB

Contents

require "test_helper"

class HTML::Pipeline::ImageMaxWidthFilterTest < Minitest::Test
  def filter(html)
    HTML::Pipeline::ImageMaxWidthFilter.call(html)
  end

  def test_rewrites_image_style_tags
    body = "<p>Screenshot: <img src='screenshot.png'></p>"
    doc  = Nokogiri::HTML::DocumentFragment.parse(body)

    res = filter(doc)
    assert_equal_html %q(<p>Screenshot: <a target="_blank" href="screenshot.png"><img src="screenshot.png" style="max-width:100%;"></a></p>),
      res.to_html
  end

  def test_leaves_existing_image_style_tags_alone
    body = "<p><img src='screenshot.png' style='width:100px;'></p>"
    doc  = Nokogiri::HTML::DocumentFragment.parse(body)

    res = filter(doc)
    assert_equal_html '<p><img src="screenshot.png" style="width:100px;"></p>',
      res.to_html
  end

  def test_links_to_image
    body = "<p>Screenshot: <img src='screenshot.png'></p>"
    doc  = Nokogiri::HTML::DocumentFragment.parse(body)

    res = filter(doc)
    assert_equal_html '<p>Screenshot: <a target="_blank" href="screenshot.png"><img src="screenshot.png" style="max-width:100%;"></a></p>',
      res.to_html
  end

  def test_doesnt_link_to_image_when_already_linked
    body = "<p>Screenshot: <a href='blah.png'><img src='screenshot.png'></a></p>"
    doc  = Nokogiri::HTML::DocumentFragment.parse(body)

    res = filter(doc)
    assert_equal_html %q(<p>Screenshot: <a href="blah.png"><img src="screenshot.png" style="max-width:100%;"></a></p>),
      res.to_html
  end

  def test_doesnt_screw_up_inlined_images
    body = "<p>Screenshot <img src='screenshot.png'>, yes, this is a <b>screenshot</b> indeed.</p>"
    doc  = Nokogiri::HTML::DocumentFragment.parse(body)

    assert_equal_html %q(<p>Screenshot <a target="_blank" href="screenshot.png"><img src="screenshot.png" style="max-width:100%;"></a>, yes, this is a <b>screenshot</b> indeed.</p>), filter(doc).to_html
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
html-pipeline-2.4.1 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.4.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.3.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.2.4 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.2.3 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.2.2 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.2.1 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.2.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.1.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-2.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-1.11.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-1.10.0 test/html/pipeline/image_max_width_filter_test.rb
html-pipeline-1.9.0 test/html/pipeline/image_max_width_filter_test.rb